Ticket #16715: wrong-join-test-r16910.patch

File wrong-join-test-r16910.patch, 3.3 KB (added by Sebastian Goll, 13 years ago)
  • tests/regressiontests/model_inheritance_select_related/tests.py

     
    22
    33from django.test import TestCase
    44
    5 from models import Restaurant, Person
     5from models import Chain, Place, Restaurant, Franchise, FranchiseChainOptional, Person
    66
    77
    88class ModelInheritanceSelectRelatedTests(TestCase):
     
    2727
    2828        jane = Person.objects.order_by("name").select_related("favorite_restaurant")[0]
    2929        self.assertEqual(jane.favorite_restaurant.name, "Craft")
     30
     31    def test_select_related_foreign_key(self):
     32        # Regression test for #16715 (1/2)
     33        c = Chain.objects.create()
     34        p = Place.objects.create(name="SomePlace")
     35        f = Franchise.objects.create(name="SomeFranchise", chain=c)
     36
     37        self.assertEqual(len(Place.objects.all()), 2)
     38        self.assertEqual(len(Place.objects.select_related("franchise")), 2)
     39        # In 16715 the following query silently dropped the place that
     40        # was not a franchise due to selecting an INNER JOIN instead
     41        # of LEFT OUTER JOIN when joining the Franchise and Chain
     42        # tables.
     43        self.assertEqual(len(Place.objects.select_related("franchise__chain")), 2)
     44
     45    def test_select_related_foreign_key_null(self):
     46        # Regression test for #16715 (2/2)
     47        c = Chain.objects.create()
     48        p = Place.objects.create(name="SomePlace")
     49        f = FranchiseChainOptional.objects.create(name="SomeFranchise", chain=c)
     50
     51        self.assertEqual(len(Place.objects.all()), 2)
     52        self.assertEqual(len(Place.objects.select_related("franchisechainoptional")), 2)
     53        # In 16715 the following query works as expected as attribute
     54        # null=True on the chain foreign key forces the join back to
     55        # LEFT OUTER JOIN in any case.
     56        self.assertEqual(len(Place.objects.select_related("franchisechainoptional__chain")), 2)
  • tests/regressiontests/model_inheritance_select_related/models.py

     
    55
    66from django.db import models
    77
     8
     9class Chain(models.Model):
     10    pass
     11
     12
    813class Place(models.Model):
    914    name = models.CharField(max_length=50)
    1015
     
    1419    def __unicode__(self):
    1520        return u"%s the place" % self.name
    1621
     22
    1723class Restaurant(Place):
    1824    serves_sushi = models.BooleanField()
    1925    serves_steak = models.BooleanField()
     
    2127    def __unicode__(self):
    2228        return u"%s the restaurant" % self.name
    2329
     30
     31class Franchise(Place):
     32    chain = models.ForeignKey(Chain)
     33
     34    def __unicode__(self):
     35        return u"%s the franchise" % self.name
     36
     37class FranchiseChainOptional(Place):
     38    chain = models.ForeignKey(Chain, null=True)
     39
     40    def __unicode__(self):
     41        return u"%s the franchise (chain optional)" % self.name
     42
     43
    2444class Person(models.Model):
    2545    name = models.CharField(max_length=50)
    2646    favorite_restaurant = models.ForeignKey(Restaurant)
Back to Top