test_versionning.py 2.21 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
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
from .withUserTestCase import WithUserTestCase
from backend.models.my_model import ForTestingVersionning
from django.conf import settings
from reversion.models import Version
from backend.signals import new_revision_saved


class VersionningTestCase(WithUserTestCase):

    @classmethod
    def setUpMoreTestData(cls):
        cls.testing_model = ForTestingVersionning
        cls.api_versionning = '/api/test/versionning/'

    def reset_signal_called(self):
        self.signal_was_called = False

    def test_setting_ok(self):
        self.assertTrue(settings.TESTING)

    #####

    def _submit_put_test(self, client, data, pk):
        response = client.put(
            self.api_versionning + str(pk) + '/',
            data,
            format='json'
        )
        self.assertEqual(response.status_code, 200)
        return response

    def _test_retreive_instance(self, data):
        matching = self.testing_model.objects.filter(
            bbb=data['bbb']
        )
        self.assertTrue(matching.exists())

        return matching[0]

    ####

    def test_versionning(self):
        """
        Test to check that versionning is working
        We also check that new_revision_saved is called
        """

        def _test_signal_sent(sender, obj, **kwargs):
            self.signal_was_called = True

        new_revision_saved.connect(_test_signal_sent)

        data_1 = {'bbb': "Test 1"}
        response = self.authenticated_client.post(
            self.api_versionning,
            data_1,
            format='json'
        )
        self.assertEqual(response.status_code, 201)
        instance = self._test_retreive_instance(data_1)

        versions = Version.objects.get_for_object(instance)
        self.assertEqual(len(versions), 1)
        self.assertTrue(self.signal_was_called)
        self.reset_signal_called()

        data_2 = {'bbb': "Test 2"}
        response = self.authenticated_client_2.put(
            self.api_versionning + str(instance.pk) + '/',
            data_2,
            format='json'
        )
        self.assertEqual(response.status_code, 200)

        versions = Version.objects.get_for_object(instance)
        self.assertEqual(len(versions), 2)
        self.assertTrue(self.signal_was_called)