Ticket #13781: select_related_tests.diff

File select_related_tests.diff, 2.6 KB (added by David Bennett, 13 years ago)

test demonstrating error

  • tests/modeltests/select_related/models.py

    diff --git a/tests/modeltests/select_related/models.py b/tests/modeltests/select_related/models.py
    index 3c2e772..ef0d3e4 100644
    a b class Species(models.Model):  
    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
     61class Parent1(models.Model):
     62    name = models.CharField(max_length=50)
     63    def __unicode__(self):
     64        return self.name
     65
     66class Parent2(models.Model):
     67    subname = models.CharField(max_length=50)
     68    def __unicode__(self):
     69        return self.subname
     70
     71class Child1(Parent2):
     72    parent1 = models.OneToOneField(Parent1)
     73    def __unicode__(self):
     74        return self.subname
  • tests/modeltests/select_related/tests.py

    diff --git a/tests/modeltests/select_related/tests.py b/tests/modeltests/select_related/tests.py
    index 1b3715a..adc8351 100644
    a b from __future__ import with_statement, absolute_import  
    22
    33from django.test import TestCase
    44
    5 from .models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species
     5from .models import (Domain, Kingdom, Phylum, Klass, Order, Family, Genus,
     6                     Species, Parent1, Child1)
    67
    78
    89class SelectRelatedTests(TestCase):
    class SelectRelatedTests(TestCase):  
    3132        self.create_tree("Eukaryota Animalia Chordata Mammalia Primates Hominidae Homo sapiens")
    3233        self.create_tree("Eukaryota Plantae Magnoliophyta Magnoliopsida Fabales Fabaceae Pisum sativum")
    3334        self.create_tree("Eukaryota Fungi Basidiomycota Homobasidiomycatae Agaricales Amanitacae Amanita muscaria")
     35        parent = Parent1(name="Parent1")
     36        parent.save()
     37        child = Child1(subname="Child1", parent1=parent)
     38        child.save()
    3439
    3540    def setUp(self):
    3641        # The test runner sets settings.DEBUG to False, but we want to gather
    class SelectRelatedTests(TestCase):  
    160165            Species.objects.select_related,
    161166            'genus__family__order', depth=4
    162167        )
     168
     169    def test_subclass_with_select_related(self):
     170        with self.assertNumQueries(1):
     171            p = Parent1.objects.select_related('child1').get(name="Parent1")
     172            self.assertEqual(p.child1.subname, u'Child1')
     173
     174    def test_subclass_without_select_related(self):
     175        with self.assertNumQueries(2):
     176            p = Parent1.objects.get(name="Parent1")
     177            self.assertEqual(p.child1.subname, u'Child1')
Back to Top