@@ -73,3 +73,51 @@ def test_eq_and_in(self):
7373 "lookup__book" ,
7474 [{"$match" : {"$and" : [{"isbn" : {"$in" : ("12345" , "56789" )}}, {"title" : "Moby Dick" }]}}],
7575 )
76+
77+ def test_union_simple_conditions (self ):
78+ with self .assertNumQueries (1 ) as ctx :
79+ list (Book .objects .filter (title = "star wars" ).union (Book .objects .filter (isbn__in = "1234" )))
80+ self .assertAggregateQuery (
81+ ctx .captured_queries [0 ]["sql" ],
82+ "lookup__book" ,
83+ [
84+ {"$match" : {"title" : "star wars" }},
85+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
86+ {
87+ "$unionWith" : {
88+ "coll" : "lookup__book" ,
89+ "pipeline" : [
90+ {"$match" : {"isbn" : {"$in" : ("1" , "2" , "3" , "4" )}}},
91+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
92+ ],
93+ }
94+ },
95+ {"$group" : {"_id" : {"_id" : "$_id" , "title" : "$title" , "isbn" : "$isbn" }}},
96+ {"$addFields" : {"_id" : "$_id._id" , "title" : "$_id.title" , "isbn" : "$_id.isbn" }},
97+ ],
98+ )
99+
100+ def test_union_all_simple_conditions (self ):
101+ with self .assertNumQueries (1 ) as ctx :
102+ list (
103+ Book .objects .filter (title = "star wars" ).union (
104+ Book .objects .filter (isbn = "1234" ), all = True
105+ )
106+ )
107+ self .assertAggregateQuery (
108+ ctx .captured_queries [0 ]["sql" ],
109+ "lookup__book" ,
110+ [
111+ {"$match" : {"title" : "star wars" }},
112+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
113+ {
114+ "$unionWith" : {
115+ "coll" : "lookup__book" ,
116+ "pipeline" : [
117+ {"$match" : {"isbn" : "1234" }},
118+ {"$project" : {"_id" : 1 , "title" : 1 , "isbn" : 1 }},
119+ ],
120+ }
121+ },
122+ ],
123+ )
0 commit comments