File: test_use_json_for_changes.py

package info (click to toggle)
django-auditlog 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 620 kB
  • sloc: python: 5,005; makefile: 21
file content (150 lines) | stat: -rw-r--r-- 5,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from django.test import TestCase, override_settings
from test_app.models import JSONModel, RelatedModel, SimpleModel

from auditlog.models import LogEntry
from auditlog.registry import AuditlogModelRegistry


class JSONForChangesTest(TestCase):

    def setUp(self):
        self.test_auditlog = AuditlogModelRegistry()

    @override_settings(AUDITLOG_STORE_JSON_CHANGES="str")
    def test_wrong_setting_type(self):
        with self.assertRaisesMessage(
            TypeError, "Setting 'AUDITLOG_STORE_JSON_CHANGES' must be a boolean"
        ):
            self.test_auditlog.register_from_settings()

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_with_simplemodel(self):
        self.test_auditlog.register_from_settings()

        smm = SimpleModel()
        smm.save()
        changes_dict = smm.history.latest().changes_dict

        # compare the id, text, boolean and datetime fields
        id_field_changes = changes_dict["id"]
        self.assertIsNone(id_field_changes[0])
        self.assertIsInstance(
            id_field_changes[1], int
        )  # the id depends on state of the database

        text_field_changes = changes_dict["text"]
        self.assertEqual(text_field_changes, [None, ""])

        boolean_field_changes = changes_dict["boolean"]
        self.assertEqual(boolean_field_changes, [None, False])

        # datetime should be serialized to string
        datetime_field_changes = changes_dict["datetime"]
        self.assertIsNone(datetime_field_changes[0])
        self.assertIsInstance(datetime_field_changes[1], str)

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_with_jsonmodel(self):
        self.test_auditlog.register_from_settings()

        json_model = JSONModel()
        json_model.json = {"test_key": "test_value"}
        json_model.save()
        changes_dict = json_model.history.latest().changes_dict

        id_field_changes = changes_dict["json"]
        self.assertEqual(id_field_changes, [None, {"test_key": "test_value"}])

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_with_jsonmodel_with_empty_list(self):
        self.test_auditlog.register_from_settings()

        json_model = JSONModel()
        json_model.json = []
        json_model.save()
        changes_dict = json_model.history.latest().changes_dict

        id_field_changes = changes_dict["json"]
        self.assertEqual(id_field_changes, [None, []])

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_with_jsonmodel_with_complex_data(self):
        self.test_auditlog.register_from_settings()

        json_model = JSONModel()
        json_model.json = {
            "key": "test_value",
            "key_dict": {"inner_key": "inner_value"},
            "key_tuple": ("item1", "item2", "item3"),
        }
        json_model.save()
        changes_dict = json_model.history.latest().changes_dict

        id_field_changes = changes_dict["json"]
        self.assertEqual(
            id_field_changes,
            [
                None,
                {
                    "key": "test_value",
                    "key_dict": {"inner_key": "inner_value"},
                    "key_tuple": [
                        "item1",
                        "item2",
                        "item3",
                    ],  # tuple is converted to list, that's ok
                },
            ],
        )

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_with_jsonmodel_with_related_model(self):
        self.test_auditlog.register_from_settings()

        simple = SimpleModel.objects.create()
        one_simple = SimpleModel.objects.create()
        related_model = RelatedModel.objects.create(
            one_to_one=simple, related=one_simple
        )
        related_model.save()
        changes_dict = related_model.history.latest().changes_dict

        field_related_changes = changes_dict["related"]
        self.assertEqual(field_related_changes, [None, one_simple.id])

        field_one_to_one_changes = changes_dict["one_to_one"]
        self.assertEqual(field_one_to_one_changes, [None, simple.id])

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_update(self):
        self.test_auditlog.register_from_settings()

        simple = SimpleModel(text="original")
        simple.save()
        simple.text = "new"
        simple.save()

        changes_dict = simple.history.latest().changes_dict

        text_changes = changes_dict["text"]
        self.assertEqual(text_changes, ["original", "new"])

    @override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
    def test_use_json_for_changes_delete(self):
        return
        self.test_auditlog.register_from_settings()

        simple = SimpleModel()
        simple.save()
        simple.delete()

        history = LogEntry.objects.all()

        self.assertEqual(history.count(), 1, '"DELETE" record is always retained')

        changes_dict = history.first().changes_dict

        self.assertTrue(
            all(v[1] is None for k, v in changes_dict.items()),
            'all values in the changes dict should None, not "None"',
        )