<commit_before>from mock import patch
from nose.tools import istest

from provy.more.debian import AptitudeRole, NginxRole
from tests.unit.tools.helpers import ProvyTestCase


class NginxRoleTest(ProvyTestCase):
    def setUp(self):
        super(NginxRoleTest, self).setUp()
        self.role = NginxRole(prov=None, context={'owner': 'some-owner'})

    @istest
    def installs_necessary_packages_to_provision(self):
        with self.using_stub(AptitudeRole) as aptitude:
            self.role.provision()

            aptitude.ensure_up_to_date.assert_called_once_with()
            aptitude.ensure_package_installed.assert_called_once_with('nginx')

    @istest
    def doesnt_restart_if_not_necessary_upon_cleanup(self):
        with patch.object(self.role, 'restart'):
            self.role.cleanup()

            self.assertFalse(self.role.restart.called)

    @istest
    def restart_if_necessary_upon_cleanup(self):
        self.role.context['must-restart-nginx'] = True
        with patch.object(self.role, 'restart'):
            self.role.cleanup()

            self.assertTrue(self.role.restart.called)
<commit_msg>Using mock_role_method instead of patch()<commit_after>from nose.tools import istest

from provy.more.debian import AptitudeRole, NginxRole
from tests.unit.tools.helpers import ProvyTestCase


class NginxRoleTest(ProvyTestCase):
    def setUp(self):
        super(NginxRoleTest, self).setUp()
        self.role = NginxRole(prov=None, context={'owner': 'some-owner'})

    @istest
    def installs_necessary_packages_to_provision(self):
        with self.using_stub(AptitudeRole) as aptitude:
            self.role.provision()

            aptitude.ensure_up_to_date.assert_called_once_with()
            aptitude.ensure_package_installed.assert_called_once_with('nginx')

    @istest
    def doesnt_restart_if_not_necessary_upon_cleanup(self):
        with self.mock_role_method('restart'):
            self.role.cleanup()

            self.assertFalse(self.role.restart.called)

    @istest
    def restart_if_necessary_upon_cleanup(self):
        self.role.context['must-restart-nginx'] = True
        with self.mock_role_method('restart'):
            self.role.cleanup()

            self.assertTrue(self.role.restart.called)

{'fraction_non_alphanumeric': 0.09827357237715803, 'fraction_numerical': 0.0, 'mean_word_length': 3.3378119001919386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3212228', 'n_tokens_mistral': 719, 'n_tokens_neox': 694, 'n_words': 108}