| | 1 | from decimal import Decimal |
| | 2 | from datetime import date |
| | 3 | |
| | 4 | from django.test import TestCase |
| | 5 | from django.db.models import Max |
| | 6 | |
| | 7 | from regressiontests.aggregation_regress.models import * |
| | 8 | |
| | 9 | |
| | 10 | class AggregationTests(TestCase): |
| | 11 | |
| | 12 | def test_aggregates_in_where_clause(self): |
| | 13 | """ |
| | 14 | Regression test for #12822: DatabaseError: aggregates not allowed in |
| | 15 | WHERE clause |
| | 16 | |
| | 17 | Tests that the subselect works and returns results equivalent to a |
| | 18 | query with the IDs listed. |
| | 19 | """ |
| | 20 | qs = Book.objects.values('contact').annotate(Max('id')) |
| | 21 | qs = qs.order_by('contact').values_list('id__max', flat=True) |
| | 22 | # don't do anything with the queryset (qs) before including it as a |
| | 23 | # subquery |
| | 24 | books = Book.objects.order_by('id') |
| | 25 | qs1 = books.filter(id__in=qs) |
| | 26 | qs2 = books.filter(id__in=list(qs)) |
| | 27 | self.assertEqual(list(qs1), list(qs2)) |
| | 28 | |
| | 29 | def test_aggregates_in_where_clause_pre_eval(self): |
| | 30 | """ |
| | 31 | Regression test for #12822: DatabaseError: aggregates not allowed in |
| | 32 | WHERE clause |
| | 33 | |
| | 34 | Same as the above test, but evaluates the queryset for the subquery |
| | 35 | before it's used as a subquery. |
| | 36 | """ |
| | 37 | qs = Book.objects.values('contact').annotate(Max('id')) |
| | 38 | qs = qs.order_by('contact').values_list('id__max', flat=True) |
| | 39 | # force the queryset (qs) for the subquery to be evaluated in its |
| | 40 | # current state |
| | 41 | list(qs) |
| | 42 | books = Book.objects.order_by('id') |
| | 43 | qs1 = books.filter(id__in=qs) |
| | 44 | qs2 = books.filter(id__in=list(qs)) |
| | 45 | self.assertEqual(list(qs1), list(qs2)) |