summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
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)