diff options
| author | Adam Johnson <me@adamj.eu> | 2025-04-14 15:46:37 +0100 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-09-18 19:44:16 -0400 |
| commit | 74a9c2711cd81708ae754c4d92432511efa96149 (patch) | |
| tree | ed87d70582ad712a93feb4f4d23edf676c566e92 /tests/contenttypes_tests | |
| parent | 762d3be8c559b0abf415be8d6117f04fb6347983 (diff) | |
Refs #28586 -- Split descriptor from GenericForeignKey.
This makes GenericForeignKey more similar to other fields which act as
descriptors, preparing it to add “fetcher protocol” support in a clear and
consistent way.
Diffstat (limited to 'tests/contenttypes_tests')
| -rw-r--r-- | tests/contenttypes_tests/test_checks.py | 30 | ||||
| -rw-r--r-- | tests/contenttypes_tests/test_fields.py | 7 |
2 files changed, 25 insertions, 12 deletions
diff --git a/tests/contenttypes_tests/test_checks.py b/tests/contenttypes_tests/test_checks.py index 50730c5a1d..c33920f6b7 100644 --- a/tests/contenttypes_tests/test_checks.py +++ b/tests/contenttypes_tests/test_checks.py @@ -19,15 +19,17 @@ class GenericForeignKeyTests(SimpleTestCase): object_id = models.PositiveIntegerField() content_object = GenericForeignKey() + field = TaggedItem._meta.get_field("content_object") + expected = [ checks.Error( "The GenericForeignKey content type references the nonexistent " "field 'TaggedItem.content_type'.", - obj=TaggedItem.content_object, + obj=field, id="contenttypes.E002", ) ] - self.assertEqual(TaggedItem.content_object.check(), expected) + self.assertEqual(field.check(), expected) def test_invalid_content_type_field(self): class Model(models.Model): @@ -35,8 +37,10 @@ class GenericForeignKeyTests(SimpleTestCase): object_id = models.PositiveIntegerField() content_object = GenericForeignKey("content_type", "object_id") + field = Model._meta.get_field("content_object") + self.assertEqual( - Model.content_object.check(), + field.check(), [ checks.Error( "'Model.content_type' is not a ForeignKey.", @@ -44,7 +48,7 @@ class GenericForeignKeyTests(SimpleTestCase): "GenericForeignKeys must use a ForeignKey to " "'contenttypes.ContentType' as the 'content_type' field." ), - obj=Model.content_object, + obj=field, id="contenttypes.E003", ) ], @@ -58,8 +62,10 @@ class GenericForeignKeyTests(SimpleTestCase): object_id = models.PositiveIntegerField() content_object = GenericForeignKey("content_type", "object_id") + field = Model._meta.get_field("content_object") + self.assertEqual( - Model.content_object.check(), + field.check(), [ checks.Error( "'Model.content_type' is not a ForeignKey to " @@ -68,7 +74,7 @@ class GenericForeignKeyTests(SimpleTestCase): "GenericForeignKeys must use a ForeignKey to " "'contenttypes.ContentType' as the 'content_type' field." ), - obj=Model.content_object, + obj=field, id="contenttypes.E004", ) ], @@ -80,13 +86,15 @@ class GenericForeignKeyTests(SimpleTestCase): # missing object_id field content_object = GenericForeignKey() + field = TaggedItem._meta.get_field("content_object") + self.assertEqual( - TaggedItem.content_object.check(), + field.check(), [ checks.Error( "The GenericForeignKey object ID references the nonexistent " "field 'object_id'.", - obj=TaggedItem.content_object, + obj=field, id="contenttypes.E001", ) ], @@ -98,12 +106,14 @@ class GenericForeignKeyTests(SimpleTestCase): object_id = models.PositiveIntegerField() content_object_ = GenericForeignKey("content_type", "object_id") + field = Model._meta.get_field("content_object_") + self.assertEqual( - Model.content_object_.check(), + field.check(), [ checks.Error( "Field names must not end with an underscore.", - obj=Model.content_object_, + obj=field, id="fields.E001", ) ], diff --git a/tests/contenttypes_tests/test_fields.py b/tests/contenttypes_tests/test_fields.py index c2b12b58bc..76830c3af3 100644 --- a/tests/contenttypes_tests/test_fields.py +++ b/tests/contenttypes_tests/test_fields.py @@ -15,13 +15,16 @@ class GenericForeignKeyTests(TestCase): class Model(models.Model): field = GenericForeignKey() - self.assertEqual(str(Model.field), "contenttypes_tests.Model.field") + field = Model._meta.get_field("field") + + self.assertEqual(str(field), "contenttypes_tests.Model.field") def test_get_content_type_no_arguments(self): + field = Answer._meta.get_field("question") with self.assertRaisesMessage( Exception, "Impossible arguments to GFK.get_content_type!" ): - Answer.question.get_content_type() + field.get_content_type() def test_get_object_cache_respects_deleted_objects(self): question = Question.objects.create(text="Who?") |
