Refs #27412 -- Confirmed support for executing Coalesce(subquery).

This has been supported for subqueries wrapped in Subquery since the expression
was introduced and for Queryset directly since Subquery resolves to sql.Query.

Piggy-backed on the existing tests covering Coalesce handling of EmptyResultSet
as it seemed like a proper location to combine testing.
This commit is contained in:
Simon Charette 2022-08-23 23:06:04 -04:00 committed by Carlton Gibson
parent 5f5401b114
commit b30c0081d4
1 changed files with 7 additions and 4 deletions

View File

@ -64,11 +64,14 @@ class CoalesceTests(TestCase):
def test_empty_queryset(self):
Author.objects.create(name="John Smith")
queryset = Author.objects.values("id")
tests = [
Author.objects.none(),
Subquery(Author.objects.none()),
(queryset.none(), "QuerySet.none()"),
(queryset.filter(id=0), "QuerySet.filter(id=0)"),
(Subquery(queryset.none()), "Subquery(QuerySet.none())"),
(Subquery(queryset.filter(id=0)), "Subquery(Queryset.filter(id=0)"),
]
for empty_query in tests:
with self.subTest(empty_query.__class__.__name__):
for empty_query, description in tests:
with self.subTest(description), self.assertNumQueries(1):
qs = Author.objects.annotate(annotation=Coalesce(empty_query, 42))
self.assertEqual(qs.first().annotation, 42)