Beispiel #1
0
    def setUp(self):
        self.validator = Mock()
        self.template_dao = Mock()

        self.user_dao = Mock()
        self.user_dao.find_all_by_template_id.return_value = []

        self.notifier = Mock()
        self.device_updater = Mock(DeviceUpdater)

        self.template = FuncKeyTemplate(id=sentinel.template_id, name=sentinel.name)
        self.service = TemplateService(self.validator,
                                       self.template_dao,
                                       self.user_dao,
                                       self.notifier,
                                       self.device_updater)
Beispiel #2
0
class TestTemplateService(unittest.TestCase):

    def setUp(self):
        self.validator = Mock()
        self.template_dao = Mock()

        self.user_dao = Mock()
        self.user_dao.find_all_by_template_id.return_value = []

        self.notifier = Mock()
        self.device_updater = Mock(DeviceUpdater)

        self.template = FuncKeyTemplate(id=sentinel.template_id, name=sentinel.name)
        self.service = TemplateService(self.validator,
                                       self.template_dao,
                                       self.user_dao,
                                       self.notifier,
                                       self.device_updater)

    def test_when_searching_then_returns_search_from_database(self):
        expected_search = self.template_dao.search.return_value

        result = self.service.search({'search': 'search'})

        assert_that(expected_search, equal_to(result))
        self.template_dao.search.assert_called_once_with(search='search')

    def test_when_getting_then_returns_template_from_database(self):
        expected_template = self.template_dao.get.return_value

        result = self.service.get(sentinel.template_id)

        assert_that(expected_template, equal_to(result))
        self.template_dao.get.assert_called_once_with(sentinel.template_id)

    def test_when_creating_then_validates_template(self):
        self.service.create(self.template)

        self.validator.validate_create.assert_called_once_with(self.template)

    def test_when_creating_then_creates_template_in_database(self):
        created_template = self.template_dao.create.return_value

        result = self.service.create(self.template)

        assert_that(result, equal_to(created_template))
        self.template_dao.create.assert_called_once_with(self.template)

    def test_when_creating_then_sends_notification(self):
        created_template = self.template_dao.create.return_value

        self.service.create(self.template)

        self.notifier.created.assert_called_once_with(created_template)

    def test_when_editing_then_validates_template(self):
        self.service.edit(self.template)

        self.validator.validate_edit.assert_called_once_with(self.template)

    def test_when_editing_then_updates_template_in_database(self):
        self.service.edit(self.template)

        self.template_dao.edit.assert_called_once_with(self.template)

    def test_when_editing_then_sends_notification(self):
        self.service.edit(self.template)

        self.notifier.edited.assert_called_once_with(self.template)

    def test_when_editing_then_updates_func_keys_for_device(self):
        self.service.edit(self.template)

        self.device_updater.update_for_template.assert_called_once_with(self.template)

    def test_when_deleting_then_validates_template(self):
        self.service.delete(self.template)

        self.validator.validate_delete.assert_called_once_with(self.template)

    def test_when_deleting_then_updates_devices_associated_to_users(self):
        expected_user = Mock()
        self.user_dao.find_all_by_template_id.return_value = [expected_user]

        self.service.delete(self.template)

        self.device_updater.update_for_user.assert_called_once_with(expected_user)
        self.user_dao.find_all_by_template_id.assert_called_once_with(self.template.id)

    def test_when_deleting_then_deletes_template_in_database(self):
        self.service.delete(self.template)

        self.template_dao.delete.assert_called_once_with(self.template)

    def test_when_deleting_then_sends_notification(self):
        self.service.delete(self.template)

        self.notifier.deleted.assert_called_once_with(self.template)