def test_changes(self):
        model_action = ModelAction(
                model=None,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        # Test getter
        self.assertEqual(self.content_changes, model_action.changes)

        # Test setter
        changes = {}
        model_action.changes = changes
        self.assertEqual(changes, model_action.changes)

        # Test deleter
        del model_action.changes
        self.assertEqual({}, model_action.changes)

        # Let AccessRequest in his old status
        model_action.changes = self.content_changes
    def test_get_model(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        # Test getter
        self.assertEqual(Foo, model_action.get_model())
    def test_verbose_str(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=self.ma_instance,
                timestamp=self.timestamp,
                access=None,
                process=None,
        )

        expected = '{} instance {} of model {} from app {} ({})'.format(
                self.action, self.instance_id, self.model_name, self.model_app, self.timestamp)
        self.assertEqual(expected, model_action.verbose_str())
    def test_get_instance_fail(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        with patch('audit_tools.tests.audit.models.test_model_action.Foo') as model_mock:
            model_mock.objects.get.side_effect = Exception
            self.assertIsNone(model_action.get_instance())
    def test_get_instance(self):
        model_action = ModelAction(
                model=self.ma_model,
                action=self.action,
                content=self.ma_content,
                instance=self.ma_instance,
                timestamp=None,
                access=None,
                process=None,
        )

        foo = Foo()
        with patch('audit_tools.tests.audit.models.test_model_action.Foo') as model_mock:
            model_mock.objects.get.return_value = foo
            self.assertEqual(foo, model_action.get_instance())
    def test_items(self):
        items = [('foo', 'bar')]
        model_action = ModelAction(
                model=None,
                action=self.action,
                content=self.ma_content,
                instance=None,
                timestamp=None,
                access=None,
                process=None,
        )

        items_mock = MagicMock()
        items_mock.items.return_value = items
        with patch.object(ModelAction, 'to_mongo', return_value=items_mock) as mongo_mock:
            result = model_action.items()

            self.assertEqual(items, result)
            self.assertEqual(mongo_mock.call_count, 1)
            self.assertEqual(items_mock.items.call_count, 1)