diff options
| author | Paulo <commonzenpython@gmail.com> | 2016-06-02 17:44:47 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-06-03 10:18:24 -0400 |
| commit | 38575b007a722d6af510ea46d46393a4cda9ca29 (patch) | |
| tree | 4b7c33608e1fbf0209e8230f7c9c1a010dda795f /tests | |
| parent | 9899347641b2d3b4457cc99203a2b06504b32a16 (diff) | |
Fixed #15250 -- Avoided extra query on some multi-table inheritance queries.
Thanks marekw2143 for the initial patch and carljm for support.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/model_inheritance_regress/tests.py | 32 | ||||
| -rw-r--r-- | tests/proxy_models/tests.py | 2 |
2 files changed, 33 insertions, 1 deletions
diff --git a/tests/model_inheritance_regress/tests.py b/tests/model_inheritance_regress/tests.py index 7499e4e42f..d2a1fc29fd 100644 --- a/tests/model_inheritance_regress/tests.py +++ b/tests/model_inheritance_regress/tests.py @@ -496,3 +496,35 @@ class ModelInheritanceTest(TestCase): r.supplier_set.all(), [s], lambda x: x, ) + + def test_queries_on_parent_access(self): + italian_restaurant = ItalianRestaurant.objects.create( + name="Guido's House of Pasta", + address='944 W. Fullerton', + serves_hot_dogs=True, + serves_pizza=False, + serves_gnocchi=True, + ) + + # No queries are made when accessing the parent objects. + italian_restaurant = ItalianRestaurant.objects.get(pk=italian_restaurant.pk) + with self.assertNumQueries(0): + restaurant = italian_restaurant.restaurant_ptr + self.assertEqual(restaurant.place_ptr.restaurant, restaurant) + self.assertEqual(restaurant.italianrestaurant, italian_restaurant) + + # One query is made when accessing the parent objects when the instance + # is deferred. + italian_restaurant = ItalianRestaurant.objects.only('serves_gnocchi').get(pk=italian_restaurant.pk) + with self.assertNumQueries(1): + restaurant = italian_restaurant.restaurant_ptr + self.assertEqual(restaurant.place_ptr.restaurant, restaurant) + self.assertEqual(restaurant.italianrestaurant, italian_restaurant) + + # No queries are made when accessing the parent objects when the + # instance has deferred a field not present in the parent table. + italian_restaurant = ItalianRestaurant.objects.defer('serves_gnocchi').get(pk=italian_restaurant.pk) + with self.assertNumQueries(0): + restaurant = italian_restaurant.restaurant_ptr + self.assertEqual(restaurant.place_ptr.restaurant, restaurant) + self.assertEqual(restaurant.italianrestaurant, italian_restaurant) diff --git a/tests/proxy_models/tests.py b/tests/proxy_models/tests.py index ecbb70c7c4..833cdf3956 100644 --- a/tests/proxy_models/tests.py +++ b/tests/proxy_models/tests.py @@ -384,7 +384,7 @@ class ProxyModelAdminTests(TestCase): tracker_user = TrackerUser.objects.all()[0] base_user = BaseUser.objects.all()[0] issue = Issue.objects.all()[0] - with self.assertNumQueries(7): + with self.assertNumQueries(6): collector = admin.utils.NestedObjects('default') collector.collect(ProxyTrackerUser.objects.all()) self.assertIn(tracker_user, collector.edges.get(None, ())) |
