@@ -73,3 +73,138 @@ def test_eq_and_in(self):
7373 "lookup__book" ,
7474 [{"$match" : {"$and" : [{"isbn" : {"$in" : ("12345" , "56789" )}}, {"title" : "Moby Dick" }]}}],
7575 )
76+
77+ def test_gt (self ):
78+ with self .assertNumQueries (1 ) as ctx :
79+ list (Number .objects .filter (num__gt = 2 ))
80+ self .assertAggregateQuery (
81+ ctx .captured_queries [0 ]["sql" ],
82+ "lookup__number" ,
83+ [
84+ {"$match" : {"num" : {"$gt" : 2 }}},
85+ {"$addFields" : {"num" : "$num" }},
86+ {"$sort" : SON ([("num" , 1 )])},
87+ ],
88+ )
89+
90+ def test_gte (self ):
91+ with self .assertNumQueries (1 ) as ctx :
92+ list (Number .objects .filter (num__gte = 2 ))
93+ self .assertAggregateQuery (
94+ ctx .captured_queries [0 ]["sql" ],
95+ "lookup__number" ,
96+ [
97+ {"$match" : {"num" : {"$gte" : 2 }}},
98+ {"$addFields" : {"num" : "$num" }},
99+ {"$sort" : SON ([("num" , 1 )])},
100+ ],
101+ )
102+
103+ def test_union_simple_conditions (self ):
104+ with self .assertNumQueries (1 ) as ctx :
105+ list (Book .objects .filter (title = "star wars" ).union (Book .objects .filter (isbn__in = "1234" )))
106+ self .assertAggregateQuery (
107+ ctx .captured_queries [0 ]["sql" ],
108+ "lookup__book" ,
109+ [
110+ {"$match" : {"title" : "star wars" }},
111+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
112+ {
113+ "$unionWith" : {
114+ "coll" : "lookup__book" ,
115+ "pipeline" : [
116+ {"$match" : {"isbn" : {"$in" : ("1" , "2" , "3" , "4" )}}},
117+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
118+ ],
119+ }
120+ },
121+ {"$group" : {"_id" : {"_id" : "$_id" , "title" : "$title" , "isbn" : "$isbn" }}},
122+ {"$addFields" : {"_id" : "$_id._id" , "title" : "$_id.title" , "isbn" : "$_id.isbn" }},
123+ ],
124+ )
125+
126+ def test_union_all_simple_conditions (self ):
127+ with self .assertNumQueries (1 ) as ctx :
128+ list (
129+ Book .objects .filter (title = "star wars" ).union (
130+ Book .objects .filter (isbn = "1234" ), all = True
131+ )
132+ )
133+ self .assertAggregateQuery (
134+ ctx .captured_queries [0 ]["sql" ],
135+ "lookup__book" ,
136+ [
137+ {"$match" : {"title" : "star wars" }},
138+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
139+ {
140+ "$unionWith" : {
141+ "coll" : "lookup__book" ,
142+ "pipeline" : [
143+ {"$match" : {"isbn" : "1234" }},
144+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
145+ ],
146+ }
147+ },
148+ ],
149+ )
150+
151+ def test_subquery_filter_constant (self ):
152+ with self .assertNumQueries (1 ) as ctx :
153+ list (Number .objects .filter (num__in = Number .objects .filter (num__gt = 2 ).values ("num" )))
154+ self .assertAggregateQuery (
155+ ctx .captured_queries [0 ]["sql" ],
156+ "lookup__number" ,
157+ [
158+ {
159+ "$lookup" : {
160+ "as" : "__subquery0" ,
161+ "from" : "lookup__number" ,
162+ "let" : {},
163+ "pipeline" : [
164+ {"$match" : {"num" : {"$gt" : 2 }}},
165+ {
166+ "$facet" : {
167+ "group" : [
168+ {"$group" : {"_id" : None , "tmp_name" : {"$addToSet" : "$num" }}}
169+ ]
170+ }
171+ },
172+ {
173+ "$project" : {
174+ "num" : {
175+ "$ifNull" : [
176+ {
177+ "$getField" : {
178+ "input" : {"$arrayElemAt" : ["$group" , 0 ]},
179+ "field" : "tmp_name" ,
180+ }
181+ },
182+ [],
183+ ]
184+ }
185+ }
186+ },
187+ ],
188+ }
189+ },
190+ {
191+ "$set" : {
192+ "__subquery0" : {
193+ "$cond" : {
194+ "if" : {
195+ "$or" : [
196+ {"$eq" : [{"$type" : "$__subquery0" }, "missing" ]},
197+ {"$eq" : [{"$size" : "$__subquery0" }, 0 ]},
198+ ]
199+ },
200+ "then" : {},
201+ "else" : {"$arrayElemAt" : ["$__subquery0" , 0 ]},
202+ }
203+ }
204+ }
205+ },
206+ {"$match" : {"$expr" : {"$in" : ["$num" , "$__subquery0.num" ]}}},
207+ {"$addFields" : {"num" : "$num" }},
208+ {"$sort" : SON ([("num" , 1 )])},
209+ ],
210+ )
0 commit comments