diff options
| author | Simon Charette <charette.s@gmail.com> | 2022-11-06 11:19:33 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-11-07 20:23:53 +0100 |
| commit | 76e37513e22f4d9a01c7f15eee36fe44388e6670 (patch) | |
| tree | 575decec7547c3c128857b0444f342452865a0f9 /tests | |
| parent | 4b702c832cd550fe682ef37a69e93866815b9123 (diff) | |
Refs #33374 -- Adjusted full match condition handling.
Adjusting WhereNode.as_sql() to raise an exception when encoutering a
full match just like with empty matches ensures that all case are
explicitly handled.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/annotations/tests.py | 19 | ||||
| -rw-r--r-- | tests/queries/tests.py | 20 |
2 files changed, 30 insertions, 9 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py index 52c15bba87..d05af552b4 100644 --- a/tests/annotations/tests.py +++ b/tests/annotations/tests.py @@ -24,7 +24,15 @@ from django.db.models import ( When, ) from django.db.models.expressions import RawSQL -from django.db.models.functions import Coalesce, ExtractYear, Floor, Length, Lower, Trim +from django.db.models.functions import ( + Cast, + Coalesce, + ExtractYear, + Floor, + Length, + Lower, + Trim, +) from django.test import TestCase, skipUnlessDBFeature from django.test.utils import register_lookup @@ -282,6 +290,13 @@ class NonAggregateAnnotationTestCase(TestCase): self.assertEqual(len(books), Book.objects.count()) self.assertTrue(all(book.selected for book in books)) + def test_full_expression_wrapped_annotation(self): + books = Book.objects.annotate( + selected=Coalesce(~Q(pk__in=[]), True), + ) + self.assertEqual(len(books), Book.objects.count()) + self.assertTrue(all(book.selected for book in books)) + def test_full_expression_annotation_with_aggregation(self): qs = Book.objects.filter(isbn="159059725").annotate( selected=ExpressionWrapper(~Q(pk__in=[]), output_field=BooleanField()), @@ -292,7 +307,7 @@ class NonAggregateAnnotationTestCase(TestCase): def test_aggregate_over_full_expression_annotation(self): qs = Book.objects.annotate( selected=ExpressionWrapper(~Q(pk__in=[]), output_field=BooleanField()), - ).aggregate(Sum("selected")) + ).aggregate(selected__sum=Sum(Cast("selected", IntegerField()))) self.assertEqual(qs["selected__sum"], Book.objects.count()) def test_empty_queryset_annotation(self): diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 63e9ea6687..332a6e5d9a 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -5,7 +5,7 @@ import unittest from operator import attrgetter from threading import Lock -from django.core.exceptions import EmptyResultSet, FieldError +from django.core.exceptions import EmptyResultSet, FieldError, FullResultSet from django.db import DEFAULT_DB_ALIAS, connection from django.db.models import CharField, Count, Exists, F, Max, OuterRef, Q from django.db.models.expressions import RawSQL @@ -3588,7 +3588,8 @@ class WhereNodeTest(SimpleTestCase): with self.assertRaises(EmptyResultSet): w.as_sql(compiler, connection) w.negate() - self.assertEqual(w.as_sql(compiler, connection), ("", [])) + with self.assertRaises(FullResultSet): + w.as_sql(compiler, connection) w = WhereNode(children=[self.DummyNode(), self.DummyNode()]) self.assertEqual(w.as_sql(compiler, connection), ("(dummy AND dummy)", [])) w.negate() @@ -3597,7 +3598,8 @@ class WhereNodeTest(SimpleTestCase): with self.assertRaises(EmptyResultSet): w.as_sql(compiler, connection) w.negate() - self.assertEqual(w.as_sql(compiler, connection), ("", [])) + with self.assertRaises(FullResultSet): + w.as_sql(compiler, connection) def test_empty_full_handling_disjunction(self): compiler = WhereNodeTest.MockCompiler() @@ -3605,7 +3607,8 @@ class WhereNodeTest(SimpleTestCase): with self.assertRaises(EmptyResultSet): w.as_sql(compiler, connection) w.negate() - self.assertEqual(w.as_sql(compiler, connection), ("", [])) + with self.assertRaises(FullResultSet): + w.as_sql(compiler, connection) w = WhereNode(children=[self.DummyNode(), self.DummyNode()], connector=OR) self.assertEqual(w.as_sql(compiler, connection), ("(dummy OR dummy)", [])) w.negate() @@ -3619,7 +3622,8 @@ class WhereNodeTest(SimpleTestCase): compiler = WhereNodeTest.MockCompiler() empty_w = WhereNode() w = WhereNode(children=[empty_w, empty_w]) - self.assertEqual(w.as_sql(compiler, connection), ("", [])) + with self.assertRaises(FullResultSet): + w.as_sql(compiler, connection) w.negate() with self.assertRaises(EmptyResultSet): w.as_sql(compiler, connection) @@ -3627,9 +3631,11 @@ class WhereNodeTest(SimpleTestCase): with self.assertRaises(EmptyResultSet): w.as_sql(compiler, connection) w.negate() - self.assertEqual(w.as_sql(compiler, connection), ("", [])) + with self.assertRaises(FullResultSet): + w.as_sql(compiler, connection) w = WhereNode(children=[empty_w, NothingNode()], connector=OR) - self.assertEqual(w.as_sql(compiler, connection), ("", [])) + with self.assertRaises(FullResultSet): + w.as_sql(compiler, connection) w = WhereNode(children=[empty_w, NothingNode()], connector=AND) with self.assertRaises(EmptyResultSet): w.as_sql(compiler, connection) |
