Code

Ticket #20528: 20528-test-master.diff

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

Test that applies cleanly to master

Line 
1diff --git a/tests/select_related_regress/models.py b/tests/select_related_regress/models.py
2index 620b8eb..a243b3b 100644
3--- a/tests/select_related_regress/models.py
4+++ b/tests/select_related_regress/models.py
5@@ -95,6 +95,18 @@ class Item(models.Model):
6     def __str__(self):
7         return self.name
8 
9+class ChildA(models.Model):
10+    parent = models.ForeignKey(Parent)
11+
12+@python_2_unicode_compatible
13+class ChildB(models.Model):
14+    name = models.CharField(max_length=10)
15+    parent = models.ForeignKey(Parent, null=True)
16+    child = models.ForeignKey(ChildA, null=True)
17+
18+    def __str__(self):
19+        return self.name
20+
21 # Models for testing bug #19870.
22 @python_2_unicode_compatible
23 class Fowl(models.Model):
24diff --git a/tests/select_related_regress/tests.py b/tests/select_related_regress/tests.py
25index f6d21b2..078b9d7 100644
26--- a/tests/select_related_regress/tests.py
27+++ b/tests/select_related_regress/tests.py
28@@ -1,11 +1,12 @@
29 from __future__ import absolute_import, unicode_literals
30 
31+from django.db import models
32 from django.test import TestCase
33 from django.utils import six
34 
35 from .models import (Building, Child, Device, Port, Item, Country, Connection,
36     ClientStatus, State, Client, SpecialClient, TUser, Person, Student,
37-    Organizer, Class, Enrollment, Hen, Chick)
38+    Organizer, Class, Enrollment, Hen, Chick, Parent, ChildA, ChildB)
39 
40 
41 class SelectRelatedRegressTests(TestCase):
42@@ -173,3 +174,26 @@ class SelectRelatedRegressTests(TestCase):
43 
44         self.assertEqual(Chick.objects.all()[0].mother.name, 'Hen')
45         self.assertEqual(Chick.objects.select_related()[0].mother.name, 'Hen')
46+
47+    def test_ticket_20528(self):
48+        """
49+        Regression for #20528
50+
51+        Using Qs and select_related with a certain config of ForeignKeys
52+        resulted in incorrect joins.
53+        """
54+        parent = Parent.objects.create(name="foo")
55+        childA = ChildA.objects.create(parent=parent)
56+        childB = ChildB.objects.create(child=childA, name="Bobby")
57+        queryset = ChildB.objects.filter(
58+            models.Q(parent=parent) | models.Q(child__parent=parent)
59+        )
60+        expected_queryset = ['<ChildB: Bobby>']
61+        # this always works
62+        self.assertQuerysetEqual(queryset, expected_queryset)
63+
64+        # this started failing after 3fef304ff237fe692459c1f5b840acf7886c50bb
65+        queryset = queryset.select_related('parent')
66+        import sys
67+        sys.stderr.write(str(queryset.query))
68+        self.assertQuerysetEqual(queryset, expected_queryset)