| 1 |
from django.test import SimpleTestCase |
| 2 |
from django.utils.deconstruct import deconstructible |
| 3 |
from django.utils.version import get_docs_version |
| 4 |
|
| 5 |
|
| 6 |
@deconstructible() |
| 7 |
class DeconstructibleClass: |
| 8 |
pass |
| 9 |
|
| 10 |
|
| 11 |
class DeconstructibleChildClass(DeconstructibleClass): |
| 12 |
pass |
| 13 |
|
| 14 |
|
| 15 |
@deconstructible( |
| 16 |
path="utils_tests.deconstructible_classes.DeconstructibleWithPathClass" |
| 17 |
) |
| 18 |
class DeconstructibleWithPathClass: |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
class DeconstructibleWithPathChildClass(DeconstructibleWithPathClass): |
| 23 |
pass |
| 24 |
|
| 25 |
|
| 26 |
@deconstructible( |
| 27 |
path="utils_tests.deconstructible_classes.DeconstructibleInvalidPathClass", |
| 28 |
) |
| 29 |
class DeconstructibleInvalidPathClass: |
| 30 |
pass |
| 31 |
|
| 32 |
|
| 33 |
class DeconstructibleInvalidPathChildClass(DeconstructibleInvalidPathClass): |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
class DeconstructibleTests(SimpleTestCase): |
| 38 |
def test_deconstruct(self): |
| 39 |
obj = DeconstructibleClass("arg", key="value") |
| 40 |
path, args, kwargs = obj.deconstruct() |
| 41 |
self.assertEqual(path, "utils_tests.test_deconstruct.DeconstructibleClass") |
| 42 |
self.assertEqual(args, ("arg",)) |
| 43 |
self.assertEqual(kwargs, {"key": "value"}) |
| 44 |
|
| 45 |
def test_deconstruct_with_path(self): |
| 46 |
obj = DeconstructibleWithPathClass("arg", key="value") |
| 47 |
path, args, kwargs = obj.deconstruct() |
| 48 |
self.assertEqual( |
| 49 |
path, |
| 50 |
"utils_tests.deconstructible_classes.DeconstructibleWithPathClass", |
| 51 |
) |
| 52 |
self.assertEqual(args, ("arg",)) |
| 53 |
self.assertEqual(kwargs, {"key": "value"}) |
| 54 |
|
| 55 |
def test_deconstruct_child(self): |
| 56 |
obj = DeconstructibleChildClass("arg", key="value") |
| 57 |
path, args, kwargs = obj.deconstruct() |
| 58 |
self.assertEqual(path, "utils_tests.test_deconstruct.DeconstructibleChildClass") |
| 59 |
self.assertEqual(args, ("arg",)) |
| 60 |
self.assertEqual(kwargs, {"key": "value"}) |
| 61 |
|
| 62 |
def test_deconstruct_child_with_path(self): |
| 63 |
obj = DeconstructibleWithPathChildClass("arg", key="value") |
| 64 |
path, args, kwargs = obj.deconstruct() |
| 65 |
self.assertEqual( |
| 66 |
path, |
| 67 |
"utils_tests.test_deconstruct.DeconstructibleWithPathChildClass", |
| 68 |
) |
| 69 |
self.assertEqual(args, ("arg",)) |
| 70 |
self.assertEqual(kwargs, {"key": "value"}) |
| 71 |
|
| 72 |
def test_invalid_path(self): |
| 73 |
obj = DeconstructibleInvalidPathClass() |
| 74 |
docs_version = get_docs_version() |
| 75 |
msg = ( |
| 76 |
f"Could not find object DeconstructibleInvalidPathClass in " |
| 77 |
f"utils_tests.deconstructible_classes.\n" |
| 78 |
f"Please note that you cannot serialize things like inner " |
| 79 |
f"classes. Please move the object into the main module body to " |
| 80 |
f"use migrations.\n" |
| 81 |
f"For more information, see " |
| 82 |
f"https://docs.djangoproject.com/en/{docs_version}/topics/" |
| 83 |
f"migrations/#serializing-values" |
| 84 |
) |
| 85 |
with self.assertRaisesMessage(ValueError, msg): |
| 86 |
obj.deconstruct() |
| 87 |
|
| 88 |
def test_parent_invalid_path(self): |
| 89 |
obj = DeconstructibleInvalidPathChildClass("arg", key="value") |
| 90 |
path, args, kwargs = obj.deconstruct() |
| 91 |
self.assertEqual( |
| 92 |
path, |
| 93 |
"utils_tests.test_deconstruct.DeconstructibleInvalidPathChildClass", |
| 94 |
) |
| 95 |
self.assertEqual(args, ("arg",)) |
| 96 |
self.assertEqual(kwargs, {"key": "value"}) |