Ticket #11890: 11890-extra-select-related-r16522.diff
File 11890-extra-select-related-r16522.diff, 3.3 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/defer_regress/tests.py
4 4 from django.contrib.contenttypes.models import ContentType 5 5 from django.contrib.sessions.backends.db import SessionStore 6 6 from django.db import connection 7 from django.db.models import Count 7 from django.db.models import Count, Sum 8 8 from django.db.models.loading import cache 9 9 from django.test import TestCase 10 10 11 from models import ResolveThis, Item, RelatedItem, Child, Leaf, Proxy 11 from models import ResolveThis, Item, RelatedItem, Child, Leaf, Proxy, Candy, Person 12 12 13 13 14 14 class DeferRegressionTest(TestCase): 15 maxDiff = None 16 15 17 def test_basic(self): 16 18 # Deferred fields should really be deferred and not accidentally use 17 19 # the field's default value just because they aren't passed to __init__ … … 109 111 ) 110 112 self.assertEqual( 111 113 klasses, [ 114 Candy, 112 115 Child, 113 116 Item, 114 117 Leaf, 118 Person, 115 119 Proxy, 116 120 RelatedItem, 117 121 ResolveThis, … … 128 132 ) 129 133 self.assertEqual( 130 134 klasses, [ 135 "Candy", 131 136 "Child", 132 137 "Child_Deferred_value", 133 138 "Item", … … 141 146 "Leaf_Deferred_name_value", 142 147 "Leaf_Deferred_second_child_value", 143 148 "Leaf_Deferred_value", 149 "Person", 144 150 "Proxy", 145 151 "RelatedItem", 146 152 "RelatedItem_Deferred_", … … 173 179 qs = ResolveThis.objects.defer('num') 174 180 self.assertEqual(1, qs.count()) 175 181 self.assertEqual('Foobar', qs[0].name) 182 183 def test_11890(self): 184 # Rather interestingly, all those select_related and defer are required to 185 # trigger some edge case behaviour 186 p = Person.objects.create(first_name='Mr', last_name='Egotistic') 187 p.best_friend = p 188 p.save() 189 for n in xrange(10): 190 Candy.objects.create(owner=p, qty=42) 191 self.assertEqual(Candy.objects.aggregate(Sum('qty'))['qty__sum'], 378) 192 self.assertEqual(Person.objects.annotate(Sum('candies__qty')).select_related('best_friend').defer('first_name','best_friend__nick_name').get(pk=p.pk).candies__qty__sum, 378) -
tests/regressiontests/defer_regress/models.py
38 38 class Proxy(Item): 39 39 class Meta: 40 40 proxy = True 41 42 # Models for 11890 -- do not change since bug includes a really 43 # esoteric edge case 44 class Person(models.Model): 45 first_name = models.CharField(max_length=200) 46 last_name = models.CharField(max_length=50) 47 best_friend = models.ForeignKey('self', blank=True, null=True, editable=False) 48 49 nick_name = models.CharField(max_length=50, blank=True, null=True) 50 51 class Candy(models.Model): 52 owner = models.ForeignKey(Person, related_name="candies") 53 qty = models.PositiveIntegerField(default=0)