def test_queryset_changes_reset_if_same_value(self):
        model = TestModelTrackAllFields.objects.create(char='a')
        self.assertEqual(AuditTrail.objects.all().count(), 1)
        trail = AuditTrail.objects.all()[0]
        self.assertEqual(trail.action, AuditTrail.ACTIONS.CREATED)
        self.assertEqual(trail.get_changes(), {
            'char': {
                'old_value': None,
                'old_value_string': None,
                'new_value': 'a',
                'new_value_string': 'a',
                'field_label': 'Char'
            }
        })

        model.char2 = 'b'
        model.save()
        self.assertEqual(AuditTrail.objects.all().count(), 2)
        trail = AuditTrail.objects.all()[0]
        self.assertEqual(trail.action, AuditTrail.ACTIONS.UPDATED)
        self.assertEqual(trail.get_changes(), {
            'char2': {
                'old_value': None,
                'old_value_string': None,
                'new_value': 'b',
                'new_value_string': 'b',
                'field_label': 'Char2'
            }
        })

        model.char2 = None
        model.save()
        self.assertEqual(AuditTrail.objects.all().count(), 3)
        trail = AuditTrail.objects.all()[0]
        self.assertEqual(trail.action, AuditTrail.ACTIONS.UPDATED)
        self.assertEqual(trail.get_changes(), {
            'char2': {
                'old_value': 'b',
                'old_value_string': 'b',
                'new_value': None,
                'new_value_string': None,
                'field_label': 'Char2'
            }
        })

        trails = audit_trail.get_for_object(model)

        self.assertEqual(len(trails.get_changes().keys()), 1)
        self.assertEqual(trails.get_changes(), {
            'char': {
                'old_value': None,
                'old_value_string': None,
                'new_value': 'a',
                'new_value_string': 'a',
                'field_label': 'Char',
                'field_name': 'char',
            }
        })
    def test_queryset_get_related_changes(self):
        """
        1. Create post
        2. Create comment 1
        3. Create comment 2

        --- Testing changes
        4. Delete comment 1
        5. Change comment 3
        6. Create comment 3
        """
        author = User.objects.create()
        post = Post.objects.create(author=author)
        comment1 = Comment.objects.create(post=post, text='comment 1 text')
        comment2 = Comment.objects.create(post=post, text='comment 2 text')

        time_from = datetime.datetime.now()

        comment1.delete()
        comment2.text = 'comment 2 text change'
        comment2.save()
        comment3 = Comment.objects.create(post=post, text='comment 3 text')

        # Do not display created and deleted object during period
        comment4 = Comment.objects.create(post=post, text='comment 3 text')
        comment4.delete()

        trails = audit_trail.get_for_object(post).filter(action_time__gt=time_from).order_by()
        related_objects_changes = trails.get_related_changes()
        self.assertEqual(len(related_objects_changes), 3)

        comment1_changes = related_objects_changes[0]
        self.assertEqual(comment1_changes['representation'], 'Comment 1')
        self.assertEqual(comment1_changes['action'], 'Deleted')
        self.assertEqual(comment1_changes['model'], 'test_app.comment')
        self.assertEqual(comment1_changes['changes']['text'], {
            'old_value': u'comment 1 text',
            'old_value_string': u'comment 1 text',
            'new_value': None,
            'new_value_string': None,
            'field_name': 'text',
            'field_label': u'Text'
        })

        comment2_changes = related_objects_changes[1]
        self.assertEqual(comment2_changes['representation'], 'Comment 2')
        self.assertEqual(comment2_changes['action'], 'Updated')
        self.assertEqual(comment1_changes['model'], 'test_app.comment')
        self.assertEqual(comment2_changes['changes']['text'], {
            'field_label': u'Text',
            'new_value': u'comment 2 text change',
            'new_value_string': u'comment 2 text change',
            'old_value': u'comment 2 text',
            'old_value_string': u'comment 2 text',
            'field_name': 'text',
        })

        comment3_changes = related_objects_changes[2]
        self.assertEqual(comment3_changes['representation'], 'Comment 3')
        self.assertEqual(comment3_changes['action'], 'Created')
        self.assertEqual(comment1_changes['model'], 'test_app.comment')
        self.assertEqual(comment3_changes['changes']['text'], {
            'old_value': None,
            'old_value_string': None,
            'new_value': u'comment 3 text',
            'new_value_string': u'comment 3 text',
            'field_label': u'Text',
            'field_name': 'text'
        })
    def test_queryset_get_related_changes(self):
        """
        1. Create post
        2. Create comment 1
        3. Create comment 2

        --- Testing changes
        4. Delete comment 1
        5. Change comment 3
        6. Create comment 3
        """
        author = User.objects.create()
        post = Post.objects.create(author=author)
        comment1 = Comment.objects.create(post=post, text="comment 1 text")
        comment2 = Comment.objects.create(post=post, text="comment 2 text")

        time_from = datetime.datetime.now()

        comment1.delete()
        comment2.text = "comment 2 text change"
        comment2.save()
        comment3 = Comment.objects.create(post=post, text="comment 3 text")

        # Do not display created and deleted object during period
        comment4 = Comment.objects.create(post=post, text="comment 3 text")
        comment4.delete()

        trails = audit_trail.get_for_object(post).filter(action_time__gt=time_from).order_by()
        related_objects_changes = trails.get_related_changes()
        self.assertEqual(len(related_objects_changes), 3)

        comment1_changes = related_objects_changes[0]
        self.assertEqual(comment1_changes["representation"], "Comment 1")
        self.assertEqual(comment1_changes["action"], "Deleted")
        self.assertEqual(comment1_changes["model"], "test_project.comment")
        self.assertEqual(
            comment1_changes["changes"]["text"],
            {
                "old_value": u"comment 1 text",
                "old_value_string": u"comment 1 text",
                "new_value": None,
                "new_value_string": None,
                "field_name": "text",
                "field_label": u"Text",
            },
        )

        comment2_changes = related_objects_changes[1]
        self.assertEqual(comment2_changes["representation"], "Comment 2")
        self.assertEqual(comment2_changes["action"], "Updated")
        self.assertEqual(comment1_changes["model"], "test_project.comment")
        self.assertEqual(
            comment2_changes["changes"]["text"],
            {
                "field_label": u"Text",
                "new_value": u"comment 2 text change",
                "new_value_string": u"comment 2 text change",
                "old_value": u"comment 2 text",
                "old_value_string": u"comment 2 text",
                "field_name": "text",
            },
        )

        comment3_changes = related_objects_changes[2]
        self.assertEqual(comment3_changes["representation"], "Comment 3")
        self.assertEqual(comment3_changes["action"], "Created")
        self.assertEqual(comment1_changes["model"], "test_project.comment")
        self.assertEqual(
            comment3_changes["changes"]["text"],
            {
                "old_value": None,
                "old_value_string": None,
                "new_value": u"comment 3 text",
                "new_value_string": u"comment 3 text",
                "field_label": u"Text",
                "field_name": "text",
            },
        )
    def test_queryset_changes_reset_if_same_value(self):
        model = TestModelTrackAllFields.objects.create(char="a")
        self.assertEqual(AuditTrail.objects.all().count(), 1)
        trail = AuditTrail.objects.all()[0]
        self.assertEqual(trail.action, AuditTrail.ACTIONS.CREATED)
        self.assertEqual(
            trail.get_changes(),
            {
                "char": {
                    "old_value": None,
                    "old_value_string": None,
                    "new_value": "a",
                    "new_value_string": "a",
                    "field_label": "Char",
                }
            },
        )

        model.char2 = "b"
        model.save()
        self.assertEqual(AuditTrail.objects.all().count(), 2)
        trail = AuditTrail.objects.all()[0]
        self.assertEqual(trail.action, AuditTrail.ACTIONS.UPDATED)
        self.assertEqual(
            trail.get_changes(),
            {
                "char2": {
                    "old_value": None,
                    "old_value_string": None,
                    "new_value": "b",
                    "new_value_string": "b",
                    "field_label": "Char2",
                }
            },
        )

        model.char2 = None
        model.save()
        self.assertEqual(AuditTrail.objects.all().count(), 3)
        trail = AuditTrail.objects.all()[0]
        self.assertEqual(trail.action, AuditTrail.ACTIONS.UPDATED)
        self.assertEqual(
            trail.get_changes(),
            {
                "char2": {
                    "old_value": "b",
                    "old_value_string": "b",
                    "new_value": None,
                    "new_value_string": None,
                    "field_label": "Char2",
                }
            },
        )

        trails = audit_trail.get_for_object(model)

        self.assertEqual(len(trails.get_changes().keys()), 1)
        self.assertEqual(
            trails.get_changes(),
            {
                "char": {
                    "old_value": None,
                    "old_value_string": None,
                    "new_value": "a",
                    "new_value_string": "a",
                    "field_label": "Char",
                    "field_name": "char",
                }
            },
        )