summaryrefslogtreecommitdiff
path: root/tests/utils_tests
diff options
context:
space:
mode:
authorAllen Jonathan David <allenajdjonathan@gmail.com>2022-01-03 20:10:14 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-01-04 13:08:36 +0100
commit194ca77092a2507a57c288c35b279eb2cb4a64fb (patch)
tree8588fe8a8e86634bd6aaf352d8248ada6d989667 /tests/utils_tests
parent63869ab1f191ab5781cde8b813b838300455f6d6 (diff)
downloaddjango-194ca77092a2507a57c288c35b279eb2cb4a64fb.tar.gz
Refs #21275 -- Added more tests for @deconstructible decorator.
Diffstat (limited to 'tests/utils_tests')
-rw-r--r--tests/utils_tests/deconstructible_classes.py1
-rw-r--r--tests/utils_tests/test_deconstruct.py68
2 files changed, 69 insertions, 0 deletions
diff --git a/tests/utils_tests/deconstructible_classes.py b/tests/utils_tests/deconstructible_classes.py
new file mode 100644
index 0000000000..d1fb8fffc1
--- /dev/null
+++ b/tests/utils_tests/deconstructible_classes.py
@@ -0,0 +1 @@
+from .test_deconstruct import DeconstructibleWithPathClass # NOQA
diff --git a/tests/utils_tests/test_deconstruct.py b/tests/utils_tests/test_deconstruct.py
new file mode 100644
index 0000000000..590e9b3811
--- /dev/null
+++ b/tests/utils_tests/test_deconstruct.py
@@ -0,0 +1,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()