summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2014-11-20 14:30:25 +0200
committerTim Graham <timograham@gmail.com>2014-11-27 06:50:08 -0500
commitbd337184f1b4901abdfd07b0219d6b9b45d0de2f (patch)
tree6cdc735f15e15e944777392f5b242837c01d1232 /tests
parentc7fd9b242d2d63406f1de6cc3204e35aaa025233 (diff)
Fixed #23877 -- aggregation's subquery missed target col
Aggregation over subquery produced syntactically incorrect queries in some cases as Django didn't ensure that source expressions of the aggregation were present in the subquery.
Diffstat (limited to 'tests')
-rw-r--r--tests/aggregation_regress/tests.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py
index b9daa63f1b..de18372f9c 100644
--- a/tests/aggregation_regress/tests.py
+++ b/tests/aggregation_regress/tests.py
@@ -1168,3 +1168,30 @@ class JoinPromotionTests(TestCase):
def test_non_nullable_fk_not_promoted(self):
qs = Book.objects.annotate(Count('contact__name'))
self.assertIn(' INNER JOIN ', str(qs.query))
+
+
+class AggregationOnRelationTest(TestCase):
+ def setUp(self):
+ self.a = Author.objects.create(name='Anssi', age=33)
+ self.p = Publisher.objects.create(name='Manning', num_awards=3)
+ Book.objects.create(isbn='asdf', name='Foo', pages=10, rating=0.1, price="0.0",
+ contact=self.a, publisher=self.p, pubdate=datetime.date.today())
+
+ def test_annotate_on_relation(self):
+ qs = Book.objects.annotate(avg_price=Avg('price'), publisher_name=F('publisher__name'))
+ self.assertEqual(qs[0].avg_price, 0.0)
+ self.assertEqual(qs[0].publisher_name, "Manning")
+
+ def test_aggregate_on_relation(self):
+ # A query with an existing annotation aggregation on a relation should
+ # succeed.
+ qs = Book.objects.annotate(avg_price=Avg('price')).aggregate(
+ publisher_awards=Sum('publisher__num_awards')
+ )
+ self.assertEqual(qs['publisher_awards'], 3)
+ Book.objects.create(isbn='asdf', name='Foo', pages=10, rating=0.1, price="0.0",
+ contact=self.a, publisher=self.p, pubdate=datetime.date.today())
+ qs = Book.objects.annotate(avg_price=Avg('price')).aggregate(
+ publisher_awards=Sum('publisher__num_awards')
+ )
+ self.assertEqual(qs['publisher_awards'], 6)