summaryrefslogtreecommitdiff
path: root/tests/aggregation
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2021-05-21 21:48:46 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-07-02 06:40:54 +0200
commitf3112fde981052801e0c2121027424496c03efdf (patch)
tree6fa1fe3a6a2efc9927df938f60a7a97a74d0737b /tests/aggregation
parentfde6fb28984a76d5bee794f4c0458eb25fe56fcd (diff)
downloaddjango-f3112fde981052801e0c2121027424496c03efdf.tar.gz
Fixed #26430 -- Fixed coalesced aggregation of empty result sets.
Disable the EmptyResultSet optimization when performing aggregation as it might interfere with coalescence.
Diffstat (limited to 'tests/aggregation')
-rw-r--r--tests/aggregation/tests.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 49123396dd..db24f36a79 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -8,6 +8,7 @@ from django.db.models import (
Avg, Case, Count, DecimalField, DurationField, Exists, F, FloatField,
IntegerField, Max, Min, OuterRef, Subquery, Sum, Value, When,
)
+from django.db.models.expressions import RawSQL
from django.db.models.functions import Coalesce, Greatest
from django.test import TestCase
from django.test.testcases import skipUnlessDBFeature
@@ -1340,3 +1341,34 @@ class AggregateTestCase(TestCase):
('Stuart Russell', 1),
('Peter Norvig', 2),
], lambda a: (a.name, a.contact_count), ordered=False)
+
+ def test_coalesced_empty_result_set(self):
+ self.assertEqual(
+ Publisher.objects.none().aggregate(
+ sum_awards=Coalesce(Sum('num_awards'), 0),
+ )['sum_awards'],
+ 0,
+ )
+ # Multiple expressions.
+ self.assertEqual(
+ Publisher.objects.none().aggregate(
+ sum_awards=Coalesce(Sum('num_awards'), None, 0),
+ )['sum_awards'],
+ 0,
+ )
+ # Nested coalesce.
+ self.assertEqual(
+ Publisher.objects.none().aggregate(
+ sum_awards=Coalesce(Coalesce(Sum('num_awards'), None), 0),
+ )['sum_awards'],
+ 0,
+ )
+ # Expression coalesce.
+ self.assertIsInstance(
+ Store.objects.none().aggregate(
+ latest_opening=Coalesce(
+ Max('original_opening'), RawSQL('CURRENT_TIMESTAMP', []),
+ ),
+ )['latest_opening'],
+ datetime.datetime,
+ )