summaryrefslogtreecommitdiff
path: root/tests/aggregation
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-08-19 16:14:31 +0300
committerAnssi Kääriäinen <akaariai@gmail.com>2013-08-19 16:15:09 +0300
commit7bc57a6d71dd4d00bb09cfa67be547591fd759ce (patch)
treeb440cb5b4430bc9883293a63988b0e3d56c99a29 /tests/aggregation
parent7737305a4f5dc5006d92dac3a61523ad6c2a523a (diff)
Fixed #11881 -- removed junk from aggregation subqueries
There were clauses that weren't needed in the subqueries. These were ORDER BY, SELECT FOR UPDATE and related selections.
Diffstat (limited to 'tests/aggregation')
-rw-r--r--tests/aggregation/tests.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 339ccd63be..7d2490a77c 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -3,8 +3,10 @@ from __future__ import unicode_literals
import datetime
from decimal import Decimal
+from django.db import connection
from django.db.models import Avg, Sum, Count, Max, Min
from django.test import TestCase, Approximate
+from django.test.utils import CaptureQueriesContext
from .models import Author, Publisher, Book, Store
@@ -625,3 +627,18 @@ class BaseAggregateTestCase(TestCase):
qs = Book.objects.all().order_by('-rating')[0:3]
vals = qs.aggregate(average_top3_rating=Avg('rating'))['average_top3_rating']
self.assertAlmostEqual(vals, 4.5, places=2)
+
+ def test_ticket11881(self):
+ """
+ Check that subqueries do not needlessly contain ORDER BY, SELECT FOR UPDATE
+ or select_related() stuff.
+ """
+ qs = Book.objects.all().select_for_update().order_by(
+ 'pk').select_related('publisher').annotate(max_pk=Max('pk'))
+ with CaptureQueriesContext(connection) as captured_queries:
+ qs.aggregate(avg_pk=Avg('max_pk'))
+ self.assertEqual(len(captured_queries), 1)
+ qstr = captured_queries[0]['sql'].lower()
+ self.assertNotIn('for update', qstr)
+ self.assertNotIn('order by', qstr)
+ self.assertEqual(qstr.count(' join '), 0)