Ticket #12822: ticket_12822_test.diff

File ticket_12822_test.diff, 1.8 KB (added by tobias, 5 years ago)

updated tests demonstrating the actual issue

  • new file tests/regressiontests/aggregation_regress/tests.py

    diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
    new file mode 100644
    - +  
     1from decimal import Decimal
     2from datetime import date
     3
     4from django.test import TestCase
     5from django.db.models import Max
     6
     7from regressiontests.aggregation_regress.models import *
     8
     9
     10class 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))
Back to Top