summaryrefslogtreecommitdiff
path: root/tests/utils_tests/test_deconstruct.py
blob: 590e9b3811ca5b78dd03872f0274d0f5e4b94421 (plain)
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
from django.test import SimpleTestCase
from django.utils.deconstruct import deconstructible
from django.utils.version import get_docs_version


@deconstructible()
class DeconstructibleClass:
    pass


class DeconstructibleChildClass(DeconstructibleClass):
    pass


@deconstructible(
    path='utils_tests.deconstructible_classes.DeconstructibleWithPathClass'
)
class DeconstructibleWithPathClass:
    pass


@deconstructible(
    path='utils_tests.deconstructible_classes.DeconstructibleInvalidPathClass',
)
class DeconstructibleInvalidPathClass:
    pass


class DeconstructibleTests(SimpleTestCase):
    def test_deconstruct(self):
        obj = DeconstructibleClass('arg', key='value')
        path, args, kwargs = obj.deconstruct()
        self.assertEqual(path, 'utils_tests.test_deconstruct.DeconstructibleClass')
        self.assertEqual(args, ('arg',))
        self.assertEqual(kwargs, {'key': 'value'})

    def test_deconstruct_with_path(self):
        obj = DeconstructibleWithPathClass('arg', key='value')
        path, args, kwargs = obj.deconstruct()
        self.assertEqual(
            path,
            'utils_tests.deconstructible_classes.DeconstructibleWithPathClass',
        )
        self.assertEqual(args, ('arg',))
        self.assertEqual(kwargs, {'key': 'value'})

    def test_deconstruct_child(self):
        obj = DeconstructibleChildClass('arg', key='value')
        path, args, kwargs = obj.deconstruct()
        self.assertEqual(path, 'utils_tests.test_deconstruct.DeconstructibleChildClass')
        self.assertEqual(args, ('arg',))
        self.assertEqual(kwargs, {'key': 'value'})

    def test_invalid_path(self):
        obj = DeconstructibleInvalidPathClass()
        docs_version = get_docs_version()
        msg = (
            f'Could not find object DeconstructibleInvalidPathClass in '
            f'utils_tests.deconstructible_classes.\n'
            f'Please note that you cannot serialize things like inner '
            f'classes. Please move the object into the main module body to '
            f'use migrations.\n'
            f'For more information, see '
            f'https://docs.djangoproject.com/en/{docs_version}/topics/'
            f'migrations/#serializing-values'
        )
        with self.assertRaisesMessage(ValueError, msg):
            obj.deconstruct()