diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py index 8bf705b1ad..408fca158a 100644 --- a/tests/field_deconstruction/tests.py +++ b/tests/field_deconstruction/tests.py @@ -214,6 +214,15 @@ class FieldDeconstructionTests(SimpleTestCase): self.assertEqual(path, "django.db.models.ForeignKey") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission", "related_name": "foobar", "on_delete": models.CASCADE}) + # Test related_query_name + field = models.ForeignKey("auth.Permission", models.CASCADE, related_query_name="foobar") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.ForeignKey") + self.assertEqual(args, []) + self.assertEqual( + kwargs, + {"to": "auth.Permission", "related_query_name": "foobar", "on_delete": models.CASCADE} + ) # Test limit_choices_to field = models.ForeignKey("auth.Permission", models.CASCADE, limit_choices_to={'foo': 'bar'}) name, path, args, kwargs = field.deconstruct() @@ -223,6 +232,12 @@ class FieldDeconstructionTests(SimpleTestCase): kwargs, {"to": "auth.Permission", "limit_choices_to": {'foo': 'bar'}, "on_delete": models.CASCADE} ) + # Test unique + field = models.ForeignKey("auth.Permission", models.CASCADE, unique=True) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.ForeignKey") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "unique": True, "on_delete": models.CASCADE}) @override_settings(AUTH_USER_MODEL="auth.Permission") def test_foreign_key_swapped(self): @@ -238,6 +253,56 @@ class FieldDeconstructionTests(SimpleTestCase): self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL") def test_one_to_one(self): + # Test basic pointing + from django.contrib.auth.models import Permission + field = models.OneToOneField("auth.Permission", models.CASCADE) + field.remote_field.model = Permission + field.remote_field.field_name = "id" + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "on_delete": models.CASCADE}) + self.assertFalse(hasattr(kwargs['to'], "setting_name")) + # Test swap detection for swappable model + field = models.OneToOneField("auth.User", models.CASCADE) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.User", "on_delete": models.CASCADE}) + self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL") + # Test nonexistent (for now) model + field = models.OneToOneField("something.Else", models.CASCADE) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "something.Else", "on_delete": models.CASCADE}) + # Test on_delete + field = models.OneToOneField("auth.User", models.SET_NULL) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.User", "on_delete": models.SET_NULL}) + # Test to_field + field = models.OneToOneField("auth.Permission", models.CASCADE, to_field="foobar") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "to_field": "foobar", "on_delete": models.CASCADE}) + # Test related_name + field = models.OneToOneField("auth.Permission", models.CASCADE, related_name="foobar") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "related_name": "foobar", "on_delete": models.CASCADE}) + # Test related_query_name + field = models.OneToOneField("auth.Permission", models.CASCADE, related_query_name="foobar") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual( + kwargs, + {"to": "auth.Permission", "related_query_name": "foobar", "on_delete": models.CASCADE} + ) # Test limit_choices_to field = models.OneToOneField("auth.Permission", models.CASCADE, limit_choices_to={'foo': 'bar'}) name, path, args, kwargs = field.deconstruct() @@ -247,6 +312,12 @@ class FieldDeconstructionTests(SimpleTestCase): kwargs, {"to": "auth.Permission", "limit_choices_to": {'foo': 'bar'}, "on_delete": models.CASCADE} ) + # Test unique + field = models.OneToOneField("auth.Permission", models.CASCADE, unique=True) + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.OneToOneField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "on_delete": models.CASCADE}) def test_image_field(self): field = models.ImageField(upload_to="foo/barness", width_field="width", height_field="height") @@ -314,6 +385,12 @@ class FieldDeconstructionTests(SimpleTestCase): self.assertEqual(path, "django.db.models.ManyToManyField") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission", "related_name": "custom_table"}) + # Test related_query_name + field = models.ManyToManyField("auth.Permission", related_query_name="foobar") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.ManyToManyField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "related_query_name": "foobar"}) # Test limit_choices_to field = models.ManyToManyField("auth.Permission", limit_choices_to={'foo': 'bar'}) name, path, args, kwargs = field.deconstruct()