summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Wobrock <david.wobrock@gmail.com>2021-09-29 00:00:50 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-09-29 20:23:29 +0200
commitdd1fa3a31b4680c0d3712e6ae122b878138580c7 (patch)
tree10205352e74cd3708581c7c262d25840be6aa12a /tests
parentad36a198a12df4dff65992191b3eb0a474e2daac (diff)
Fixed #33018 -- Fixed annotations with empty queryset.
Thanks Simon Charette for the review and implementation idea.
Diffstat (limited to 'tests')
-rw-r--r--tests/annotations/tests.py6
-rw-r--r--tests/db_functions/comparison/test_coalesce.py13
2 files changed, 18 insertions, 1 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index c0fe308a07..62912ee99c 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -210,6 +210,12 @@ class NonAggregateAnnotationTestCase(TestCase):
self.assertEqual(len(books), Book.objects.count())
self.assertTrue(all(not book.selected for book in books))
+ def test_empty_queryset_annotation(self):
+ qs = Author.objects.annotate(
+ empty=Subquery(Author.objects.values('id').none())
+ )
+ self.assertIsNone(qs.first().empty)
+
def test_annotate_with_aggregation(self):
books = Book.objects.annotate(is_book=Value(1), rating_count=Count('rating'))
for book in books:
diff --git a/tests/db_functions/comparison/test_coalesce.py b/tests/db_functions/comparison/test_coalesce.py
index 8ba4b01fe6..1093079d68 100644
--- a/tests/db_functions/comparison/test_coalesce.py
+++ b/tests/db_functions/comparison/test_coalesce.py
@@ -1,4 +1,4 @@
-from django.db.models import TextField
+from django.db.models import Subquery, TextField
from django.db.models.functions import Coalesce, Lower
from django.test import TestCase
from django.utils import timezone
@@ -70,3 +70,14 @@ class CoalesceTests(TestCase):
authors, ['John Smith', 'Rhonda'],
lambda a: a.name
)
+
+ def test_empty_queryset(self):
+ Author.objects.create(name='John Smith')
+ tests = [
+ Author.objects.none(),
+ Subquery(Author.objects.none()),
+ ]
+ for empty_query in tests:
+ with self.subTest(empty_query.__class__.__name__):
+ qs = Author.objects.annotate(annotation=Coalesce(empty_query, 42))
+ self.assertEqual(qs.first().annotation, 42)