Ticket #11890: 11890-extra-select-related-r16522.diff

File 11890-extra-select-related-r16522.diff, 3.3 KB (added by Tai Lee, 13 years ago)

Removed explicit PKs from test. Still bogus.

  • tests/regressiontests/defer_regress/tests.py

     
    44from django.contrib.contenttypes.models import ContentType
    55from django.contrib.sessions.backends.db import SessionStore
    66from django.db import connection
    7 from django.db.models import Count
     7from django.db.models import Count, Sum
    88from django.db.models.loading import cache
    99from django.test import TestCase
    1010
    11 from models import ResolveThis, Item, RelatedItem, Child, Leaf, Proxy
     11from models import ResolveThis, Item, RelatedItem, Child, Leaf, Proxy, Candy, Person
    1212
    1313
    1414class DeferRegressionTest(TestCase):
     15    maxDiff = None
     16
    1517    def test_basic(self):
    1618        # Deferred fields should really be deferred and not accidentally use
    1719        # the field's default value just because they aren't passed to __init__
     
    109111        )
    110112        self.assertEqual(
    111113            klasses, [
     114                Candy,
    112115                Child,
    113116                Item,
    114117                Leaf,
     118                Person,
    115119                Proxy,
    116120                RelatedItem,
    117121                ResolveThis,
     
    128132        )
    129133        self.assertEqual(
    130134            klasses, [
     135                "Candy",
    131136                "Child",
    132137                "Child_Deferred_value",
    133138                "Item",
     
    141146                "Leaf_Deferred_name_value",
    142147                "Leaf_Deferred_second_child_value",
    143148                "Leaf_Deferred_value",
     149                "Person",
    144150                "Proxy",
    145151                "RelatedItem",
    146152                "RelatedItem_Deferred_",
     
    173179        qs = ResolveThis.objects.defer('num')
    174180        self.assertEqual(1, qs.count())
    175181        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

     
    3838class Proxy(Item):
    3939    class Meta:
    4040        proxy = True
     41
     42# Models for 11890 -- do not change since bug includes a really
     43# esoteric edge case
     44class 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
     51class Candy(models.Model):
     52    owner = models.ForeignKey(Person, related_name="candies")
     53    qty = models.PositiveIntegerField(default=0)
Back to Top