File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 11from bson import SON
2+ from django .db .models import Sum
23from django .test import TestCase
34
45from 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" )))
You can’t perform that action at this time.
0 commit comments