Ticket #20528: 20528-test-master.diff

File 20528-test-master.diff, 2.5 KB (added by timo, 21 months ago)

Test that applies cleanly to master

  • tests/select_related_regress/models.py

    diff --git a/tests/select_related_regress/models.py b/tests/select_related_regress/models.py
    index 620b8eb..a243b3b 100644
    a b class Item(models.Model): 
    9595    def __str__(self):
    9696        return self.name
    9797
     98class ChildA(models.Model):
     99    parent = models.ForeignKey(Parent)
     100
     101@python_2_unicode_compatible
     102class ChildB(models.Model):
     103    name = models.CharField(max_length=10)
     104    parent = models.ForeignKey(Parent, null=True)
     105    child = models.ForeignKey(ChildA, null=True)
     106
     107    def __str__(self):
     108        return self.name
     109
    98110# Models for testing bug #19870.
    99111@python_2_unicode_compatible
    100112class Fowl(models.Model):
  • tests/select_related_regress/tests.py

    diff --git a/tests/select_related_regress/tests.py b/tests/select_related_regress/tests.py
    index f6d21b2..078b9d7 100644
    a b  
    11from __future__ import absolute_import, unicode_literals
    22
     3from django.db import models
    34from django.test import TestCase
    45from django.utils import six
    56
    67from .models import (Building, Child, Device, Port, Item, Country, Connection,
    78    ClientStatus, State, Client, SpecialClient, TUser, Person, Student,
    8     Organizer, Class, Enrollment, Hen, Chick)
     9    Organizer, Class, Enrollment, Hen, Chick, Parent, ChildA, ChildB)
    910
    1011
    1112class SelectRelatedRegressTests(TestCase):
    class SelectRelatedRegressTests(TestCase): 
    173174
    174175        self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen')
    175176        self.assertEqual(Chick.objects.select_related()[0].mother.name, 'Hen')
     177
     178    def test_ticket_20528(self):
     179        """
     180        Regression for #20528
     181
     182        Using Qs and select_related with a certain config of ForeignKeys
     183        resulted in incorrect joins.
     184        """
     185        parent = Parent.objects.create(name="foo")
     186        childA = ChildA.objects.create(parent=parent)
     187        childB = ChildB.objects.create(child=childA, name="Bobby")
     188        queryset = ChildB.objects.filter(
     189            models.Q(parent=parent) | models.Q(child__parent=parent)
     190        )
     191        expected_queryset = ['<ChildB: Bobby>']
     192        # this always works
     193        self.assertQuerysetEqual(queryset, expected_queryset)
     194
     195        # this started failing after 3fef304ff237fe692459c1f5b840acf7886c50bb
     196        queryset = queryset.select_related('parent')
     197        import sys
     198        sys.stderr.write(str(queryset.query))
     199        self.assertQuerysetEqual(queryset, expected_queryset)
Back to Top