From 194ca77092a2507a57c288c35b279eb2cb4a64fb Mon Sep 17 00:00:00 2001 From: Allen Jonathan David Date: Mon, 3 Jan 2022 20:10:14 +0530 Subject: [PATCH] Refs #21275 -- Added more tests for @deconstructible decorator. --- tests/utils_tests/deconstructible_classes.py | 1 + tests/utils_tests/test_deconstruct.py | 68 ++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/utils_tests/deconstructible_classes.py create mode 100644 tests/utils_tests/test_deconstruct.py 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()