Skip to content

Commit e6fbc1c

Browse files
committed
Add having test.
1 parent 2a18534 commit e6fbc1c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/lookup_/tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from bson import SON
2+
from django.db.models import Sum
23
from django.test import TestCase
34

45
from django_mongodb_backend.test import MongoTestCaseMixin
@@ -100,6 +101,28 @@ def test_gte(self):
100101
],
101102
)
102103

104+
def test_group_by_with_having(self):
105+
with self.assertNumQueries(1) as ctx:
106+
list(Number.objects.values("num").annotate(total=Sum("num")).filter(total=1))
107+
self.assertAggregateQuery(
108+
ctx.captured_queries[0]["sql"],
109+
"lookup__number",
110+
[
111+
{
112+
"$group": {
113+
"__aggregation1": {"$sum": "$num"},
114+
"_id": {"num": "$num"},
115+
"total": {"$sum": "$num"},
116+
}
117+
},
118+
{"$addFields": {"num": "$_id.num"}},
119+
{"$unset": "_id"},
120+
{"$match": {"__aggregation1": 1}},
121+
{"$project": {"num": 1, "total": "$__aggregation1"}},
122+
{"$sort": SON([("num", 1)])},
123+
],
124+
)
125+
103126
def test_subquery_filter_constant(self):
104127
with self.assertNumQueries(1) as ctx:
105128
list(Number.objects.filter(num__in=Number.objects.filter(num__gt=2).values("num")))

0 commit comments

Comments
 (0)