diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/model_inheritance | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/model_inheritance')
-rw-r--r-- | tests/model_inheritance/models.py | 33 | ||||
-rw-r--r-- | tests/model_inheritance/test_abstract_inheritance.py | 152 | ||||
-rw-r--r-- | tests/model_inheritance/tests.py | 240 |
3 files changed, 253 insertions, 172 deletions
diff --git a/tests/model_inheritance/models.py b/tests/model_inheritance/models.py index fa37e121ea..a479faeda5 100644 --- a/tests/model_inheritance/models.py +++ b/tests/model_inheritance/models.py @@ -24,10 +24,10 @@ class CommonInfo(models.Model): class Meta: abstract = True - ordering = ['name'] + ordering = ["name"] def __str__(self): - return '%s %s' % (self.__class__.__name__, self.name) + return "%s %s" % (self.__class__.__name__, self.name) class Worker(CommonInfo): @@ -45,6 +45,7 @@ class Student(CommonInfo): # Abstract base classes with related models # + class Post(models.Model): title = models.CharField(max_length=50) @@ -53,8 +54,8 @@ class Attachment(models.Model): post = models.ForeignKey( Post, models.CASCADE, - related_name='attached_%(class)s_set', - related_query_name='attached_%(app_label)s_%(class)ss', + related_name="attached_%(class)s_set", + related_query_name="attached_%(app_label)s_%(class)ss", ) content = models.TextField() @@ -74,6 +75,7 @@ class Link(Attachment): # Multi-table inheritance # + class Chef(models.Model): name = models.CharField(max_length=50) @@ -88,7 +90,7 @@ class Rating(models.Model): class Meta: abstract = True - ordering = ['-rating'] + ordering = ["-rating"] class Restaurant(Place, Rating): @@ -97,7 +99,7 @@ class Restaurant(Place, Rating): chef = models.ForeignKey(Chef, models.SET_NULL, null=True, blank=True) class Meta(Rating.Meta): - db_table = 'my_restaurant' + db_table = "my_restaurant" class ItalianRestaurant(Restaurant): @@ -105,13 +107,15 @@ class ItalianRestaurant(Restaurant): class Supplier(Place): - customers = models.ManyToManyField(Restaurant, related_name='provider') + customers = models.ManyToManyField(Restaurant, related_name="provider") class ParkingLot(Place): # An explicit link to the parent (we can control the attribute name). - parent = models.OneToOneField(Place, models.CASCADE, primary_key=True, parent_link=True) - main_site = models.ForeignKey(Place, models.CASCADE, related_name='lot') + parent = models.OneToOneField( + Place, models.CASCADE, primary_key=True, parent_link=True + ) + main_site = models.ForeignKey(Place, models.CASCADE, related_name="lot") # @@ -123,12 +127,15 @@ class ParkingLot(Place): # here in order to have the name conflict between apps # + class Title(models.Model): title = models.CharField(max_length=50) class NamedURL(models.Model): - title = models.ForeignKey(Title, models.CASCADE, related_name='attached_%(app_label)s_%(class)s_set') + title = models.ForeignKey( + Title, models.CASCADE, related_name="attached_%(app_label)s_%(class)s_set" + ) url = models.URLField() class Meta: @@ -157,12 +164,12 @@ class GrandParent(models.Model): first_name = models.CharField(max_length=80) last_name = models.CharField(max_length=80) email = models.EmailField(unique=True) - place = models.ForeignKey(Place, models.CASCADE, null=True, related_name='+') + place = models.ForeignKey(Place, models.CASCADE, null=True, related_name="+") class Meta: # Ordering used by test_inherited_ordering_pk_desc. - ordering = ['-pk'] - unique_together = ('first_name', 'last_name') + ordering = ["-pk"] + unique_together = ("first_name", "last_name") class Parent(GrandParent): diff --git a/tests/model_inheritance/test_abstract_inheritance.py b/tests/model_inheritance/test_abstract_inheritance.py index 80da06aeff..24362292a1 100644 --- a/tests/model_inheritance/test_abstract_inheritance.py +++ b/tests/model_inheritance/test_abstract_inheritance.py @@ -1,6 +1,4 @@ -from django.contrib.contenttypes.fields import ( - GenericForeignKey, GenericRelation, -) +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation from django.contrib.contenttypes.models import ContentType from django.core.checks import Error from django.core.exceptions import FieldDoesNotExist, FieldError @@ -9,7 +7,7 @@ from django.test import SimpleTestCase from django.test.utils import isolate_apps -@isolate_apps('model_inheritance') +@isolate_apps("model_inheritance") class AbstractInheritanceTests(SimpleTestCase): def test_single_parent(self): class AbstractBase(models.Model): @@ -30,9 +28,9 @@ class AbstractInheritanceTests(SimpleTestCase): class DerivedGrandChild(AbstractDescendant): pass - self.assertEqual(AbstractDescendant._meta.get_field('name').max_length, 50) - self.assertEqual(DerivedChild._meta.get_field('name').max_length, 50) - self.assertEqual(DerivedGrandChild._meta.get_field('name').max_length, 50) + self.assertEqual(AbstractDescendant._meta.get_field("name").max_length, 50) + self.assertEqual(DerivedChild._meta.get_field("name").max_length, 50) + self.assertEqual(DerivedGrandChild._meta.get_field("name").max_length, 50) def test_multiple_inheritance_allows_inherited_field(self): """ @@ -56,7 +54,7 @@ class AbstractInheritanceTests(SimpleTestCase): pass self.assertEqual(Child.check(), []) - inherited_field = Child._meta.get_field('name') + inherited_field = Child._meta.get_field("name") self.assertIsInstance(inherited_field, models.CharField) self.assertEqual(inherited_field.max_length, 255) @@ -92,7 +90,7 @@ class AbstractInheritanceTests(SimpleTestCase): pass self.assertEqual(Child.check(), []) - inherited_field = Child._meta.get_field('name') + inherited_field = Child._meta.get_field("name") self.assertIsInstance(inherited_field, models.CharField) self.assertEqual(inherited_field.max_length, 255) @@ -123,7 +121,7 @@ class AbstractInheritanceTests(SimpleTestCase): name = models.IntegerField() self.assertEqual(Child.check(), []) - inherited_field = Child._meta.get_field('name') + inherited_field = Child._meta.get_field("name") self.assertIsInstance(inherited_field, models.IntegerField) def test_multiple_inheritance_cannot_shadow_concrete_inherited_field(self): @@ -142,22 +140,24 @@ class AbstractInheritanceTests(SimpleTestCase): class AnotherChild(AbstractParent, ConcreteParent): pass - self.assertIsInstance(FirstChild._meta.get_field('name'), models.CharField) + self.assertIsInstance(FirstChild._meta.get_field("name"), models.CharField) self.assertEqual( AnotherChild.check(), - [Error( - "The field 'name' clashes with the field 'name' " - "from model 'model_inheritance.concreteparent'.", - obj=AnotherChild._meta.get_field('name'), - id="models.E006", - )] + [ + Error( + "The field 'name' clashes with the field 'name' " + "from model 'model_inheritance.concreteparent'.", + obj=AnotherChild._meta.get_field("name"), + id="models.E006", + ) + ], ) def test_virtual_field(self): class RelationModel(models.Model): content_type = models.ForeignKey(ContentType, models.CASCADE) object_id = models.PositiveIntegerField() - content_object = GenericForeignKey('content_type', 'object_id') + content_object = GenericForeignKey("content_type", "object_id") class RelatedModelAbstract(models.Model): field = GenericRelation(RelationModel) @@ -177,8 +177,12 @@ class AbstractInheritanceTests(SimpleTestCase): class ExtendModelAbstract(ModelAbstract): field = GenericRelation(RelationModel) - self.assertIsInstance(OverrideRelatedModelAbstract._meta.get_field('field'), models.CharField) - self.assertIsInstance(ExtendModelAbstract._meta.get_field('field'), GenericRelation) + self.assertIsInstance( + OverrideRelatedModelAbstract._meta.get_field("field"), models.CharField + ) + self.assertIsInstance( + ExtendModelAbstract._meta.get_field("field"), GenericRelation + ) def test_cannot_override_indirect_abstract_field(self): class AbstractBase(models.Model): @@ -195,6 +199,7 @@ class AbstractInheritanceTests(SimpleTestCase): "the same name from base class 'ConcreteDescendant'." ) with self.assertRaisesMessage(FieldError, msg): + class Descendant(ConcreteDescendant): name = models.IntegerField() @@ -215,11 +220,11 @@ class AbstractInheritanceTests(SimpleTestCase): return self.first_name + self.last_name msg = "Descendant has no field named %r" - with self.assertRaisesMessage(FieldDoesNotExist, msg % 'middle_name'): - Descendant._meta.get_field('middle_name') + with self.assertRaisesMessage(FieldDoesNotExist, msg % "middle_name"): + Descendant._meta.get_field("middle_name") - with self.assertRaisesMessage(FieldDoesNotExist, msg % 'full_name'): - Descendant._meta.get_field('full_name') + with self.assertRaisesMessage(FieldDoesNotExist, msg % "full_name"): + Descendant._meta.get_field("full_name") def test_overriding_field_removed_by_concrete_model(self): class AbstractModel(models.Model): @@ -234,7 +239,9 @@ class AbstractInheritanceTests(SimpleTestCase): class OverrideRemovedFieldByConcreteModel(RemovedAbstractModelField): foo = models.CharField(max_length=50) - self.assertEqual(OverrideRemovedFieldByConcreteModel._meta.get_field('foo').max_length, 50) + self.assertEqual( + OverrideRemovedFieldByConcreteModel._meta.get_field("foo").max_length, 50 + ) def test_shadowed_fkey_id(self): class Foo(models.Model): @@ -251,12 +258,14 @@ class AbstractInheritanceTests(SimpleTestCase): self.assertEqual( Descendant.check(), - [Error( - "The field 'foo_id' clashes with the field 'foo' " - "from model 'model_inheritance.descendant'.", - obj=Descendant._meta.get_field('foo_id'), - id='models.E006', - )] + [ + Error( + "The field 'foo_id' clashes with the field 'foo' " + "from model 'model_inheritance.descendant'.", + obj=Descendant._meta.get_field("foo_id"), + id="models.E006", + ) + ], ) def test_shadow_related_name_when_set_to_none(self): @@ -271,7 +280,7 @@ class AbstractInheritanceTests(SimpleTestCase): foo = models.IntegerField() class Bar(models.Model): - bar = models.ForeignKey(Foo, models.CASCADE, related_name='bar') + bar = models.ForeignKey(Foo, models.CASCADE, related_name="bar") self.assertEqual(Bar.check(), []) @@ -286,10 +295,10 @@ class AbstractInheritanceTests(SimpleTestCase): pass class Foo(models.Model): - foo = models.ForeignKey(Descendant, models.CASCADE, related_name='foo') + foo = models.ForeignKey(Descendant, models.CASCADE, related_name="foo") self.assertEqual( - Foo._meta.get_field('foo').check(), + Foo._meta.get_field("foo").check(), [ Error( "Reverse accessor 'Descendant.foo' for " @@ -300,8 +309,8 @@ class AbstractInheritanceTests(SimpleTestCase): "add/change a related_name argument to the definition " "for field 'model_inheritance.Foo.foo'." ), - obj=Foo._meta.get_field('foo'), - id='fields.E302', + obj=Foo._meta.get_field("foo"), + id="fields.E302", ), Error( "Reverse query name for 'model_inheritance.Foo.foo' " @@ -312,10 +321,10 @@ class AbstractInheritanceTests(SimpleTestCase): "add/change a related_name argument to the definition " "for field 'model_inheritance.Foo.foo'." ), - obj=Foo._meta.get_field('foo'), - id='fields.E303', + obj=Foo._meta.get_field("foo"), + id="fields.E303", ), - ] + ], ) def test_multi_inheritance_field_clashes(self): @@ -337,12 +346,14 @@ class AbstractInheritanceTests(SimpleTestCase): self.assertEqual( ConcreteDescendant.check(), - [Error( - "The field 'name' clashes with the field 'name' from " - "model 'model_inheritance.concretebase'.", - obj=ConcreteDescendant._meta.get_field('name'), - id="models.E006", - )] + [ + Error( + "The field 'name' clashes with the field 'name' from " + "model 'model_inheritance.concretebase'.", + obj=ConcreteDescendant._meta.get_field("name"), + id="models.E006", + ) + ], ) def test_override_one2one_relation_auto_field_clashes(self): @@ -361,6 +372,7 @@ class AbstractInheritanceTests(SimpleTestCase): "declared field of the same name." ) with self.assertRaisesMessage(FieldError, msg): + class Descendant(ConcreteParent, AbstractParent): concreteparent_ptr = models.CharField(max_length=30) @@ -388,36 +400,50 @@ class AbstractInheritanceTests(SimpleTestCase): age = models.SmallIntegerField() def fields(model): - if not hasattr(model, '_meta'): + if not hasattr(model, "_meta"): return [] return [(f.name, f.__class__) for f in model._meta.get_fields()] - model_dict = {'__module__': 'model_inheritance'} - model1 = type('Model1', (AbstractModel, Mixin), model_dict.copy()) - model2 = type('Model2', (Mixin2, AbstractModel), model_dict.copy()) - model3 = type('Model3', (DescendantMixin, AbstractModel), model_dict.copy()) - model4 = type('Model4', (Mixin2, Mixin, AbstractModel), model_dict.copy()) - model5 = type('Model5', (Mixin2, ConcreteModel2, Mixin, AbstractModel), model_dict.copy()) + model_dict = {"__module__": "model_inheritance"} + model1 = type("Model1", (AbstractModel, Mixin), model_dict.copy()) + model2 = type("Model2", (Mixin2, AbstractModel), model_dict.copy()) + model3 = type("Model3", (DescendantMixin, AbstractModel), model_dict.copy()) + model4 = type("Model4", (Mixin2, Mixin, AbstractModel), model_dict.copy()) + model5 = type( + "Model5", (Mixin2, ConcreteModel2, Mixin, AbstractModel), model_dict.copy() + ) self.assertEqual( fields(model1), - [('id', models.AutoField), ('name', models.CharField), ('age', models.IntegerField)] + [ + ("id", models.AutoField), + ("name", models.CharField), + ("age", models.IntegerField), + ], ) - self.assertEqual(fields(model2), [('id', models.AutoField), ('name', models.CharField)]) - self.assertEqual(getattr(model2, 'age'), 2) + self.assertEqual( + fields(model2), [("id", models.AutoField), ("name", models.CharField)] + ) + self.assertEqual(getattr(model2, "age"), 2) - self.assertEqual(fields(model3), [('id', models.AutoField), ('name', models.CharField)]) + self.assertEqual( + fields(model3), [("id", models.AutoField), ("name", models.CharField)] + ) - self.assertEqual(fields(model4), [('id', models.AutoField), ('name', models.CharField)]) - self.assertEqual(getattr(model4, 'age'), 2) + self.assertEqual( + fields(model4), [("id", models.AutoField), ("name", models.CharField)] + ) + self.assertEqual(getattr(model4, "age"), 2) self.assertEqual( fields(model5), [ - ('id', models.AutoField), ('foo', models.IntegerField), - ('concretemodel_ptr', models.OneToOneField), - ('age', models.SmallIntegerField), ('concretemodel2_ptr', models.OneToOneField), - ('name', models.CharField), - ] + ("id", models.AutoField), + ("foo", models.IntegerField), + ("concretemodel_ptr", models.OneToOneField), + ("age", models.SmallIntegerField), + ("concretemodel2_ptr", models.OneToOneField), + ("name", models.CharField), + ], ) diff --git a/tests/model_inheritance/tests.py b/tests/model_inheritance/tests.py index 550a297fb3..882f2999e2 100644 --- a/tests/model_inheritance/tests.py +++ b/tests/model_inheritance/tests.py @@ -7,9 +7,23 @@ from django.test import SimpleTestCase, TestCase from django.test.utils import CaptureQueriesContext, isolate_apps from .models import ( - Base, Chef, CommonInfo, GrandChild, GrandParent, ItalianRestaurant, - MixinModel, Parent, ParkingLot, Place, Post, Restaurant, Student, SubBase, - Supplier, Title, Worker, + Base, + Chef, + CommonInfo, + GrandChild, + GrandParent, + ItalianRestaurant, + MixinModel, + Parent, + ParkingLot, + Place, + Post, + Restaurant, + Student, + SubBase, + Supplier, + Title, + Worker, ) @@ -31,7 +45,8 @@ class ModelInheritanceTests(TestCase): # The children inherit the Meta class of their parents (if they don't # specify their own). self.assertSequenceEqual( - Worker.objects.values("name"), [ + Worker.objects.values("name"), + [ {"name": "Barney"}, {"name": "Fred"}, ], @@ -44,7 +59,9 @@ class ModelInheritanceTests(TestCase): # However, the CommonInfo class cannot be used as a normal model (it # doesn't exist as a model). - with self.assertRaisesMessage(AttributeError, "'CommonInfo' has no attribute 'objects'"): + with self.assertRaisesMessage( + AttributeError, "'CommonInfo' has no attribute 'objects'" + ): CommonInfo.objects.all() def test_reverse_relation_for_different_hierarchy_tree(self): @@ -66,7 +83,7 @@ class ModelInheritanceTests(TestCase): post.attached_comment_set.create(content="Save $ on V1agr@", is_spam=True) post.attached_link_set.create( content="The web framework for perfections with deadlines.", - url="http://www.djangoproject.com/" + url="http://www.djangoproject.com/", ) # The Post model doesn't have an attribute called @@ -76,7 +93,9 @@ class ModelInheritanceTests(TestCase): getattr(post, "attached_%(class)s_set") def test_model_with_distinct_related_query_name(self): - self.assertQuerysetEqual(Post.objects.filter(attached_model_inheritance_comments__is_spam=True), []) + self.assertQuerysetEqual( + Post.objects.filter(attached_model_inheritance_comments__is_spam=True), [] + ) # The Post model doesn't have a related query accessor based on # related_name (attached_comment_set). @@ -89,13 +108,31 @@ class ModelInheritanceTests(TestCase): # the right order. self.assertEqual( [f.name for f in Restaurant._meta.fields], - ["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs", - "serves_pizza", "chef"] + [ + "id", + "name", + "address", + "place_ptr", + "rating", + "serves_hot_dogs", + "serves_pizza", + "chef", + ], ) self.assertEqual( [f.name for f in ItalianRestaurant._meta.fields], - ["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs", - "serves_pizza", "chef", "restaurant_ptr", "serves_gnocchi"], + [ + "id", + "name", + "address", + "place_ptr", + "rating", + "serves_hot_dogs", + "serves_pizza", + "chef", + "restaurant_ptr", + "serves_gnocchi", + ], ) self.assertEqual(Restaurant._meta.ordering, ["-rating"]) @@ -117,38 +154,42 @@ class ModelInheritanceTests(TestCase): query constrained by an inner query (#10399). """ supplier = Supplier.objects.create( - name='Central market', - address='610 some street', + name="Central market", + address="610 some street", ) # Capture the expected query in a database agnostic way with CaptureQueriesContext(connection) as captured_queries: Place.objects.filter(pk=supplier.pk).update(name=supplier.name) - expected_sql = captured_queries[0]['sql'] + expected_sql = captured_queries[0]["sql"] # Capture the queries executed when a subclassed model instance is saved. with CaptureQueriesContext(connection) as captured_queries: - supplier.save(update_fields=('name',)) + supplier.save(update_fields=("name",)) for query in captured_queries: - sql = query['sql'] - if 'UPDATE' in sql: + sql = query["sql"] + if "UPDATE" in sql: self.assertEqual(expected_sql, sql) def test_create_child_no_update(self): """Creating a child with non-abstract parents only issues INSERTs.""" + def a(): GrandChild.objects.create( - email='grand_parent@example.com', - first_name='grand', - last_name='parent', + email="grand_parent@example.com", + first_name="grand", + last_name="parent", ) def b(): GrandChild().save() + for i, test in enumerate([a, b]): - with self.subTest(i=i), self.assertNumQueries(4), CaptureQueriesContext(connection) as queries: + with self.subTest(i=i), self.assertNumQueries(4), CaptureQueriesContext( + connection + ) as queries: test() for query in queries: - sql = query['sql'] - self.assertIn('INSERT INTO', sql, sql) + sql = query["sql"] + self.assertIn("INSERT INTO", sql, sql) def test_eq(self): # Equality doesn't transfer in multitable inheritance. @@ -159,13 +200,13 @@ class ModelInheritanceTests(TestCase): m = MixinModel() self.assertEqual(m.other_attr, 1) - @isolate_apps('model_inheritance') + @isolate_apps("model_inheritance") def test_abstract_parent_link(self): class A(models.Model): pass class B(A): - a = models.OneToOneField('A', parent_link=True, on_delete=models.CASCADE) + a = models.OneToOneField("A", parent_link=True, on_delete=models.CASCADE) class Meta: abstract = True @@ -173,9 +214,9 @@ class ModelInheritanceTests(TestCase): class C(B): pass - self.assertIs(C._meta.parents[A], C._meta.get_field('a')) + self.assertIs(C._meta.parents[A], C._meta.get_field("a")) - @isolate_apps('model_inheritance') + @isolate_apps("model_inheritance") def test_init_subclass(self): saved_kwargs = {} @@ -184,14 +225,14 @@ class ModelInheritanceTests(TestCase): super().__init_subclass__() saved_kwargs.update(kwargs) - kwargs = {'x': 1, 'y': 2, 'z': 3} + kwargs = {"x": 1, "y": 2, "z": 3} class B(A, **kwargs): pass self.assertEqual(saved_kwargs, kwargs) - @isolate_apps('model_inheritance') + @isolate_apps("model_inheritance") def test_set_name(self): class ClassAttr: called = None @@ -203,16 +244,14 @@ class ModelInheritanceTests(TestCase): class A(models.Model): attr = ClassAttr() - self.assertEqual(A.attr.called, (A, 'attr')) + self.assertEqual(A.attr.called, (A, "attr")) def test_inherited_ordering_pk_desc(self): - p1 = Parent.objects.create(first_name='Joe', email='joe@email.com') - p2 = Parent.objects.create(first_name='Jon', email='jon@email.com') - expected_order_by_sql = 'ORDER BY %s.%s DESC' % ( + p1 = Parent.objects.create(first_name="Joe", email="joe@email.com") + p2 = Parent.objects.create(first_name="Jon", email="jon@email.com") + expected_order_by_sql = "ORDER BY %s.%s DESC" % ( connection.ops.quote_name(Parent._meta.db_table), - connection.ops.quote_name( - Parent._meta.get_field('grandparent_ptr').column - ), + connection.ops.quote_name(Parent._meta.get_field("grandparent_ptr").column), ) qs = Parent.objects.all() self.assertSequenceEqual(qs, [p2, p1]) @@ -278,35 +317,39 @@ class ModelInheritanceDataTests(TestCase): def test_filter_inherited_model(self): self.assertQuerysetEqual( - ItalianRestaurant.objects.filter(address="1234 W. Ash"), [ + ItalianRestaurant.objects.filter(address="1234 W. Ash"), + [ "Ristorante Miron", ], - attrgetter("name") + attrgetter("name"), ) def test_update_inherited_model(self): self.italian_restaurant.address = "1234 W. Elm" self.italian_restaurant.save() self.assertQuerysetEqual( - ItalianRestaurant.objects.filter(address="1234 W. Elm"), [ + ItalianRestaurant.objects.filter(address="1234 W. Elm"), + [ "Ristorante Miron", ], - attrgetter("name") + attrgetter("name"), ) def test_parent_fields_available_for_filtering_in_child_model(self): # Parent fields can be used directly in filters on the child model. self.assertQuerysetEqual( - Restaurant.objects.filter(name="Demon Dogs"), [ + Restaurant.objects.filter(name="Demon Dogs"), + [ "Demon Dogs", ], - attrgetter("name") + attrgetter("name"), ) self.assertQuerysetEqual( - ItalianRestaurant.objects.filter(address="1234 W. Ash"), [ + ItalianRestaurant.objects.filter(address="1234 W. Ash"), + [ "Ristorante Miron", ], - attrgetter("name") + attrgetter("name"), ) def test_filter_on_parent_returns_object_of_parent_type(self): @@ -320,15 +363,15 @@ class ModelInheritanceDataTests(TestCase): # child's name. self.assertEqual( Place.objects.get(name="Demon Dogs").restaurant, - Restaurant.objects.get(name="Demon Dogs") + Restaurant.objects.get(name="Demon Dogs"), ) self.assertEqual( Place.objects.get(name="Ristorante Miron").restaurant.italianrestaurant, - ItalianRestaurant.objects.get(name="Ristorante Miron") + ItalianRestaurant.objects.get(name="Ristorante Miron"), ) self.assertEqual( Restaurant.objects.get(name="Ristorante Miron").italianrestaurant, - ItalianRestaurant.objects.get(name="Ristorante Miron") + ItalianRestaurant.objects.get(name="Ristorante Miron"), ) def test_parent_child_one_to_one_link_on_nonrelated_objects(self): @@ -363,36 +406,33 @@ class ModelInheritanceDataTests(TestCase): self.assertEqual(p.supplier, s1) self.assertQuerysetEqual( - self.italian_restaurant.provider.order_by("-name"), [ - "Luigi's Pasta", - "Joe's Chickens" - ], - attrgetter("name") + self.italian_restaurant.provider.order_by("-name"), + ["Luigi's Pasta", "Joe's Chickens"], + attrgetter("name"), ) self.assertQuerysetEqual( - Restaurant.objects.filter(provider__name__contains="Chickens"), [ + Restaurant.objects.filter(provider__name__contains="Chickens"), + [ "Ristorante Miron", "Demon Dogs", ], - attrgetter("name") + attrgetter("name"), ) self.assertQuerysetEqual( - ItalianRestaurant.objects.filter(provider__name__contains="Chickens"), [ + ItalianRestaurant.objects.filter(provider__name__contains="Chickens"), + [ "Ristorante Miron", ], attrgetter("name"), ) - ParkingLot.objects.create( - name="Main St", address="111 Main St", main_site=s1 - ) + ParkingLot.objects.create(name="Main St", address="111 Main St", main_site=s1) ParkingLot.objects.create( name="Well Lit", address="124 Sesame St", main_site=self.italian_restaurant ) self.assertEqual( - Restaurant.objects.get(lot__name="Well Lit").name, - "Ristorante Miron" + Restaurant.objects.get(lot__name="Well Lit").name, "Ristorante Miron" ) def test_update_works_on_parent_and_child_models_at_once(self): @@ -400,9 +440,7 @@ class ModelInheritanceDataTests(TestCase): # once (although it executed multiple SQL queries to do so). rows = Restaurant.objects.filter( serves_hot_dogs=True, name__contains="D" - ).update( - name="Demon Puppies", serves_hot_dogs=False - ) + ).update(name="Demon Puppies", serves_hot_dogs=False) self.assertEqual(rows, 1) r1 = Restaurant.objects.get(pk=self.restaurant.pk) @@ -412,7 +450,8 @@ class ModelInheritanceDataTests(TestCase): def test_values_works_on_parent_model_fields(self): # The values() command also works on fields from parent models. self.assertSequenceEqual( - ItalianRestaurant.objects.values("name", "rating"), [ + ItalianRestaurant.objects.values("name", "rating"), + [ {"rating": 4, "name": "Ristorante Miron"}, ], ) @@ -420,9 +459,7 @@ class ModelInheritanceDataTests(TestCase): def test_select_related_works_on_parent_model_fields(self): # select_related works with fields from the parent object as if they # were a normal part of the model. - self.assertNumQueries( - 2, lambda: ItalianRestaurant.objects.all()[0].chef - ) + self.assertNumQueries(2, lambda: ItalianRestaurant.objects.all()[0].chef) self.assertNumQueries( 1, lambda: ItalianRestaurant.objects.select_related("chef")[0].chef ) @@ -432,8 +469,11 @@ class ModelInheritanceDataTests(TestCase): #23370 - Should be able to defer child fields when using select_related() from parent to child. """ - qs = (Restaurant.objects.select_related("italianrestaurant") - .defer("italianrestaurant__serves_gnocchi").order_by("rating")) + qs = ( + Restaurant.objects.select_related("italianrestaurant") + .defer("italianrestaurant__serves_gnocchi") + .order_by("rating") + ) # The field was actually deferred with self.assertNumQueries(2): @@ -441,15 +481,15 @@ class ModelInheritanceDataTests(TestCase): self.assertTrue(objs[1].italianrestaurant.serves_gnocchi) # Model fields where assigned correct values - self.assertEqual(qs[0].name, 'Demon Dogs') + self.assertEqual(qs[0].name, "Demon Dogs") self.assertEqual(qs[0].rating, 2) - self.assertEqual(qs[1].italianrestaurant.name, 'Ristorante Miron') + self.assertEqual(qs[1].italianrestaurant.name, "Ristorante Miron") self.assertEqual(qs[1].italianrestaurant.rating, 4) def test_parent_cache_reuse(self): place = Place.objects.create() GrandChild.objects.create(place=place) - grand_parent = GrandParent.objects.latest('pk') + grand_parent = GrandParent.objects.latest("pk") with self.assertNumQueries(1): self.assertEqual(grand_parent.place, place) parent = grand_parent.parent @@ -476,17 +516,19 @@ class ModelInheritanceDataTests(TestCase): address="610 some street", ) self.assertQuerysetEqual( - Place.objects.filter(supplier__isnull=False), [ + Place.objects.filter(supplier__isnull=False), + [ "Central market", ], - attrgetter("name") + attrgetter("name"), ) self.assertQuerysetEqual( - Place.objects.filter(supplier__isnull=True).order_by("name"), [ + Place.objects.filter(supplier__isnull=True).order_by("name"), + [ "Demon Dogs", "Ristorante Miron", ], - attrgetter("name") + attrgetter("name"), ) def test_exclude_inherited_on_null(self): @@ -496,50 +538,56 @@ class ModelInheritanceDataTests(TestCase): address="610 some street", ) self.assertQuerysetEqual( - Place.objects.exclude(supplier__isnull=False).order_by("name"), [ + Place.objects.exclude(supplier__isnull=False).order_by("name"), + [ "Demon Dogs", "Ristorante Miron", ], - attrgetter("name") + attrgetter("name"), ) self.assertQuerysetEqual( - Place.objects.exclude(supplier__isnull=True), [ + Place.objects.exclude(supplier__isnull=True), + [ "Central market", ], - attrgetter("name") + attrgetter("name"), ) -@isolate_apps('model_inheritance', 'model_inheritance.tests') +@isolate_apps("model_inheritance", "model_inheritance.tests") class InheritanceSameModelNameTests(SimpleTestCase): def test_abstract_fk_related_name(self): - related_name = '%(app_label)s_%(class)s_references' + related_name = "%(app_label)s_%(class)s_references" class Referenced(models.Model): class Meta: - app_label = 'model_inheritance' + app_label = "model_inheritance" class AbstractReferent(models.Model): - reference = models.ForeignKey(Referenced, models.CASCADE, related_name=related_name) + reference = models.ForeignKey( + Referenced, models.CASCADE, related_name=related_name + ) class Meta: - app_label = 'model_inheritance' + app_label = "model_inheritance" abstract = True class Referent(AbstractReferent): class Meta: - app_label = 'model_inheritance' + app_label = "model_inheritance" LocalReferent = Referent class Referent(AbstractReferent): class Meta: - app_label = 'tests' + app_label = "tests" ForeignReferent = Referent self.assertFalse(hasattr(Referenced, related_name)) - self.assertIs(Referenced.model_inheritance_referent_references.field.model, LocalReferent) + self.assertIs( + Referenced.model_inheritance_referent_references.field.model, LocalReferent + ) self.assertIs(Referenced.tests_referent_references.field.model, ForeignReferent) @@ -547,27 +595,27 @@ class InheritanceUniqueTests(TestCase): @classmethod def setUpTestData(cls): cls.grand_parent = GrandParent.objects.create( - email='grand_parent@example.com', - first_name='grand', - last_name='parent', + email="grand_parent@example.com", + first_name="grand", + last_name="parent", ) def test_unique(self): grand_child = GrandChild( email=self.grand_parent.email, - first_name='grand', - last_name='child', + first_name="grand", + last_name="child", ) - msg = 'Grand parent with this Email already exists.' + msg = "Grand parent with this Email already exists." with self.assertRaisesMessage(ValidationError, msg): grand_child.validate_unique() def test_unique_together(self): grand_child = GrandChild( - email='grand_child@example.com', + email="grand_child@example.com", first_name=self.grand_parent.first_name, last_name=self.grand_parent.last_name, ) - msg = 'Grand parent with this First name and Last name already exists.' + msg = "Grand parent with this First name and Last name already exists." with self.assertRaisesMessage(ValidationError, msg): grand_child.validate_unique() |