Ticket #16043: 16043-testcase.patch

File 16043-testcase.patch, 2.1 KB (added by Aymeric Augustin, 13 years ago)
  • modeltests/select_related/tests.py

     
     1from __future__ import with_statement
     2
    13from django.test import TestCase
    24
    35from models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species
     6from models import KingdomOnPlanet
    47
    58class SelectRelatedTests(TestCase):
    69
     
    166169            Species.objects.select_related,
    167170            'genus__family__order', depth=4
    168171        )
     172
     173    def test_issue_16043(self):
     174        """
     175        Regression test for #16043.
     176        If the parent class has a foreign key to another object, make sure
     177        that the target of the foreign key is cached for both the parent and
     178        the child.
     179        """
     180        related = Domain.objects.create(name='Eukaryota')
     181        child = KingdomOnPlanet.objects.create(name='Protista', planet='Earth',
     182                domain=related)
     183        # 1 SQL request needed
     184        with self.assertNumQueries(1):
     185            parent = Kingdom.objects.select_related('domain').get(name='Protista')
     186        # no SQL needed, thanks to select_related
     187        with self.assertNumQueries(0):
     188            parent.domain
     189        # 1 SQL request needed to obtain the specialization
     190        with self.assertNumQueries(1):
     191            child = parent.kingdomonplanet
     192        # no SQL needed, the related object was cached at step 2
     193        with self.assertNumQueries(0):
     194            child.domain
  • modeltests/select_related/models.py

     
    5656    name = models.CharField(max_length=50)
    5757    genus = models.ForeignKey(Genus)
    5858    def __unicode__(self):
    59         return self.name
    60  No newline at end of file
     59        return self.name
     60
     61# Sorry, we need some science-fiction too.
     62
     63class KingdomOnPlanet(Kingdom):
     64    planet = models.CharField(max_length=50)
Back to Top