From ed0d720b78c8f1c655ead0057d767a0712f1a6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Tue, 24 Sep 2013 18:23:02 +0300 Subject: [PATCH] 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. --- tests/aggregation_regress/models.py | 11 +++++++++++ tests/aggregation_regress/tests.py | 21 +++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/aggregation_regress/models.py b/tests/aggregation_regress/models.py index dfef691882..24859f4ce4 100644 --- a/tests/aggregation_regress/models.py +++ b/tests/aggregation_regress/models.py @@ -87,3 +87,14 @@ class HardbackBook(Book): def __str__(self): return "%s (hardback): %s" % (self.name, self.weight) + +# Models for ticket #21150 +class Alfa(models.Model): + pass + +class Bravo(models.Model): + pass + +class Charlie(models.Model): + alfa = models.ForeignKey(Alfa, null=True) + bravo = models.ForeignKey(Bravo, null=True) diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py index a5107d4444..82be5afab2 100644 --- a/tests/aggregation_regress/tests.py +++ b/tests/aggregation_regress/tests.py @@ -12,8 +12,9 @@ from django.test import TestCase, skipUnlessDBFeature from django.test.utils import Approximate from django.utils import six -from .models import (Author, Book, Publisher, Clues, Entries, HardbackBook, - ItemTag, WithManualPK) +from .models import ( + Author, Book, Publisher, Clues, Entries, HardbackBook, ItemTag, + WithManualPK, Alfa, Bravo, Charlie) class AggregationTests(TestCase): @@ -1135,3 +1136,19 @@ class AggregationTests(TestCase): 'select__sum': 10, 'select__avg': Approximate(1.666, places=2), }) + + def test_ticket_21150(self): + b = Bravo.objects.create() + c = Charlie.objects.create(bravo=b) + qs = Charlie.objects.select_related('alfa').annotate(Count('bravo__charlie')) + self.assertQuerysetEqual( + qs, [c], lambda x: x) + self.assertIs(qs[0].alfa, None) + a = Alfa.objects.create() + c.alfa = a + c.save() + # Force re-evaluation + qs = qs.all() + self.assertQuerysetEqual( + qs, [c], lambda x: x) + self.assertEqual(qs[0].alfa, a)