test_squash_versions.py 2.25 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from backend_app.models.abstract.my_model import ForTestingVersioning
Florent Chehab's avatar
Florent Chehab committed
2
from reversion.models import Version
Florent Chehab's avatar
Florent Chehab committed
3
from backend_app.signals.__squash_revision_by_user import squash_revision_by_user
Florent Chehab's avatar
Florent Chehab committed
4
5
6
7
8
9
10
import reversion
from .withUserTestCase import WithUserTestCase
import json


class SquashVersionsTestCase(WithUserTestCase):
    def setUp(self):
11
        self.obj = ForTestingVersioning(bbb="v1")
Florent Chehab's avatar
Florent Chehab committed
12
13
14
15
16
        with reversion.create_revision():
            self.obj.save()
            reversion.set_user(self.authenticated_user)

    def get_versions(self, obj):
17
18
19
20
21
        return (
            Version.objects.get_for_object(obj)
            .select_related("revision")
            .order_by("-revision__date_created")
        )
Florent Chehab's avatar
Florent Chehab committed
22
23

    def get_version_data(self, version):
24
        return json.loads(version.serialized_data)[0]["fields"]
Florent Chehab's avatar
Florent Chehab committed
25
26
27
28
29

    def test_no_squashing(self):
        """
        Test to check that when two different users successively
        Modify a model, no squashing is performed
30
        nb_versions is incremented
Florent Chehab's avatar
Florent Chehab committed
31
32
33
34
35
36
        """
        with reversion.create_revision():
            self.obj.bbb = "v2 other user"
            self.obj.save()
            reversion.set_user(self.authenticated_user_2)

37
        squash_revision_by_user(None, self.obj)
Florent Chehab's avatar
Florent Chehab committed
38
39
40

        versions = self.get_versions(self.obj)
        self.assertEqual(len(versions), 2)
41
        self.assertEqual(len(versions), self.obj.nb_versions)
Florent Chehab's avatar
Florent Chehab committed
42
43
44
        first_edit = self.get_version_data(versions[1])
        second_edit = self.get_version_data(versions[0])

45
46
        self.assertEqual(first_edit["bbb"], "v1")
        self.assertEqual(second_edit["bbb"], "v2 other user")
Florent Chehab's avatar
Florent Chehab committed
47
48
49
50

    def test_squashing(self):
        """
        Test to check that when a user save two different
51
52
        versions of a same model, squashing is performed
        nb_versions is not incremented
Florent Chehab's avatar
Florent Chehab committed
53
54
55
56
57
58
        """
        with reversion.create_revision():
            self.obj.bbb = "v2"
            self.obj.save()
            reversion.set_user(self.authenticated_user)

59
        squash_revision_by_user(None, self.obj)
Florent Chehab's avatar
Florent Chehab committed
60
61
62

        versions = self.get_versions(self.obj)
        self.assertEqual(len(versions), 1)
63
        self.assertEqual(len(versions), self.obj.nb_versions)
Florent Chehab's avatar
Florent Chehab committed
64
        version_data = self.get_version_data(versions[0])
65
        self.assertEqual(version_data["bbb"], "v2")