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


class SquashVersionsTestCase(WithUserTestCase):

    def setUp(self):
12
        self.obj = ForTestingVersioning(bbb="v1")
Florent Chehab's avatar
Florent Chehab committed
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
        with reversion.create_revision():
            self.obj.save()
            reversion.set_user(self.authenticated_user)

    def get_versions(self, obj):
        return Version.objects\
            .get_for_object(obj)\
            .select_related('revision')\
            .order_by('-revision__date_created')

    def get_version_data(self, version):
        return json.loads(version.serialized_data)[0]['fields']

    def test_no_squashing(self):
        """
        Test to check that when two different users successively
        Modify a model, no squashing is performed
        """
        with reversion.create_revision():
            self.obj.bbb = "v2 other user"
            self.obj.save()
            reversion.set_user(self.authenticated_user_2)

        squashRevisionByUser(None, self.obj)

        versions = self.get_versions(self.obj)
        self.assertEqual(len(versions), 2)
        first_edit = self.get_version_data(versions[1])
        second_edit = self.get_version_data(versions[0])

        self.assertEqual(first_edit['bbb'], "v1")
        self.assertEqual(second_edit['bbb'], "v2 other user")

    def test_squashing(self):
        """
        Test to check that when a user save two different
        versions of a same model, no squashing is performed
        """
        with reversion.create_revision():
            self.obj.bbb = "v2"
            self.obj.save()
            reversion.set_user(self.authenticated_user)

        squashRevisionByUser(None, self.obj)

        versions = self.get_versions(self.obj)
        self.assertEqual(len(versions), 1)
        version_data = self.get_version_data(versions[0])
        self.assertEqual(version_data['bbb'], "v2")