Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #21150 -- select_related + annotate join promotion failure

Added tests for a .annotate().select_related() join promotion failure.
This happened to work on master but was currently untested.
  • Loading branch information...
commit ed0d720b78c8f1c655ead0057d767a0712f1a6a8 1 parent 20472aa
Anssi Kääriäinen authored September 24, 2013
11  tests/aggregation_regress/models.py
@@ -87,3 +87,14 @@ class HardbackBook(Book):
87 87
 
88 88
     def __str__(self):
89 89
         return "%s (hardback): %s" % (self.name, self.weight)
  90
+
  91
+# Models for ticket #21150
  92
+class Alfa(models.Model):
  93
+    pass
  94
+
  95
+class Bravo(models.Model):
  96
+    pass
  97
+
  98
+class Charlie(models.Model):
  99
+    alfa = models.ForeignKey(Alfa, null=True)
  100
+    bravo = models.ForeignKey(Bravo, null=True)
21  tests/aggregation_regress/tests.py
@@ -12,8 +12,9 @@
12 12
 from django.test.utils import Approximate
13 13
 from django.utils import six
14 14
 
15  
-from .models import (Author, Book, Publisher, Clues, Entries, HardbackBook,
16  
-        ItemTag, WithManualPK)
  15
+from .models import (
  16
+    Author, Book, Publisher, Clues, Entries, HardbackBook, ItemTag,
  17
+    WithManualPK, Alfa, Bravo, Charlie)
17 18
 
18 19
 
19 20
 class AggregationTests(TestCase):
@@ -1135,3 +1136,19 @@ def test_annotate_reserved_word(self):
1135 1136
             'select__sum': 10,
1136 1137
             'select__avg': Approximate(1.666, places=2),
1137 1138
         })
  1139
+
  1140
+    def test_ticket_21150(self):
  1141
+        b = Bravo.objects.create()
  1142
+        c = Charlie.objects.create(bravo=b)
  1143
+        qs = Charlie.objects.select_related('alfa').annotate(Count('bravo__charlie'))
  1144
+        self.assertQuerysetEqual(
  1145
+            qs, [c], lambda x: x)
  1146
+        self.assertIs(qs[0].alfa, None)
  1147
+        a = Alfa.objects.create()
  1148
+        c.alfa = a
  1149
+        c.save()
  1150
+        # Force re-evaluation
  1151
+        qs = qs.all()
  1152
+        self.assertQuerysetEqual(
  1153
+            qs, [c], lambda x: x)
  1154
+        self.assertEqual(qs[0].alfa, a)

0 notes on commit ed0d720

Please sign in to comment.
Something went wrong with that request. Please try again.