diff --git a/tests/defer_regress/tests.py b/tests/defer_regress/tests.py index d4d722035f..fc77793288 100644 --- a/tests/defer_regress/tests.py +++ b/tests/defer_regress/tests.py @@ -98,29 +98,36 @@ class DeferRegressionTest(TestCase): i2 = s["item"] self.assertFalse(i2._deferred) + # Regression for #16409 - make sure defer() and only() work with annotate() + self.assertIsInstance( + list(SimpleItem.objects.annotate(Count('feature')).defer('name')), + list) + self.assertIsInstance( + list(SimpleItem.objects.annotate(Count('feature')).only('name')), + list) + + def test_ticket_11936(self): # Regression for #11936 - loading.get_models should not return deferred # models by default. - klasses = sorted( - cache.get_models(cache.get_app("defer_regress")), - key=lambda klass: klass.__name__ - ) - self.assertEqual( - klasses, [ - Child, - Feature, - Item, - ItemAndSimpleItem, - Leaf, - OneToOneItem, - Proxy, - RelatedItem, - ResolveThis, - SimpleItem, - SpecialFeature, - ] + # Run a couple of defer queries so that app-cache must contain some + # deferred classes. It might contain a lot more classes depending on + # the order the tests are ran. + list(Item.objects.defer("name")) + list(Child.objects.defer("value")) + klasses = set( + map( + attrgetter("__name__"), + cache.get_models(cache.get_app("defer_regress")) + ) ) + self.assertIn("Child", klasses) + self.assertIn("Item", klasses) + self.assertNotIn("Child_Deferred_value", klasses) + self.assertNotIn("Item_Deferred_name", klasses) + self.assertFalse(any( + k._deferred for k in cache.get_models(cache.get_app("defer_regress")))) - klasses = sorted( + klasses_with_deferred = set( map( attrgetter("__name__"), cache.get_models( @@ -128,41 +135,15 @@ class DeferRegressionTest(TestCase): ), ) ) - # FIXME: This is dependent on the order in which tests are run -- - # this test case has to be the first, otherwise a LOT more classes - # appear. - self.assertEqual( - klasses, [ - "Child", - "Child_Deferred_value", - "Feature", - "Item", - "ItemAndSimpleItem", - "Item_Deferred_name", - "Item_Deferred_name_other_value_text", - "Item_Deferred_name_other_value_value", - "Item_Deferred_other_value_text_value", - "Item_Deferred_text_value", - "Leaf", - "Leaf_Deferred_child_id_second_child_id_value", - "Leaf_Deferred_name_value", - "Leaf_Deferred_second_child_id_value", - "Leaf_Deferred_value", - "OneToOneItem", - "Proxy", - "RelatedItem", - "RelatedItem_Deferred_", - "RelatedItem_Deferred_item_id", - "ResolveThis", - "SimpleItem", - "SpecialFeature", - ] + self.assertIn("Child", klasses_with_deferred) + self.assertIn("Item", klasses_with_deferred) + self.assertIn("Child_Deferred_value", klasses_with_deferred) + self.assertIn("Item_Deferred_name", klasses_with_deferred) + self.assertTrue(any( + k._deferred for k in cache.get_models( + cache.get_app("defer_regress"), include_deferred=True)) ) - # Regression for #16409 - make sure defer() and only() work with annotate() - self.assertIsInstance(list(SimpleItem.objects.annotate(Count('feature')).defer('name')), list) - self.assertIsInstance(list(SimpleItem.objects.annotate(Count('feature')).only('name')), list) - def test_only_and_defer_usage_on_proxy_models(self): # Regression for #15790 - only() broken for proxy models proxy = Proxy.objects.create(name="proxy", value=42)