Skip to content

Commit f1b0878

Browse files
committed
Update queries_ tests
1 parent b16c721 commit f1b0878

File tree

4 files changed

+45
-59
lines changed

4 files changed

+45
-59
lines changed

django_mongodb_backend/query.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,7 @@ def get_pipeline(self):
9090
for query in self.subqueries or ():
9191
pipeline.extend(query.get_pipeline())
9292
if self.match_mql:
93-
# optimized = self.query_optimizer.convert_expr_to_match(self.match_mql)
94-
# if any("$and" in str(cond) for cond in optimized) and not "$and" in str(self.match_mql["$expr"]):
95-
# print(f"$AND MISMATCH MQL\nOptimized:\n\t{pprint.pformat(optimized)}\nOriginal:\n\t{self.match_mql}")
96-
# if any("$or" in str(cond) for cond in optimized) and not "$or" in str(self.match_mql["$expr"]):
97-
# print(f"$OR MISMATCH MQL\nOptimized:\n\t{pprint.pformat(optimized)}\nOriginal:\n\t{self.match_mql}")
9893
pipeline.extend(self.query_optimizer.convert_expr_to_match(self.match_mql))
99-
# pipeline.append({"$match": self.match_mql})
10094
if self.aggregation_pipeline:
10195
pipeline.extend(self.aggregation_pipeline)
10296
if self.project_fields:

django_mongodb_backend/query_conversion/query_optimizer.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ def convert_expr_to_match(self, expr):
2626
expr_content = expr_query["$expr"]
2727

2828
# Handle the expression content
29-
optimized_query = self._process_expression(expr_content)
30-
31-
# print(f"Original expr:\n{json_util.dumps(expr_query)}\nOptimized expr:\n{json_util.dumps(optimized_query)}")
32-
return optimized_query
29+
return self._process_expression(expr_content)
3330

3431
def _process_expression(self, expr):
3532
"""
@@ -81,12 +78,12 @@ def _process_logical_conditions(self, logical_op, logical_conditions):
8178
if optimized := convert_expression(condition):
8279
optimized_conditions.append(optimized)
8380
else:
84-
# print(f"Can't optimize condition: {condition}")
8581
_remaining_conditions.append(condition)
8682
else:
8783
_remaining_conditions.append(condition)
8884
if _remaining_conditions:
89-
# Any expressions we can't optimize must remain in an $expr that preserves the logical operator
85+
# Any expressions we can't optimize must remain
86+
# in an $expr that preserves the logical operator
9087
if len(_remaining_conditions) > 1:
9188
remaining_conditions.append({"$expr": {logical_op: _remaining_conditions}})
9289
else:

tests/queries_/test_explain.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def test_object_id(self):
2020
id = ObjectId()
2121
result = Author.objects.filter(id=id).explain()
2222
parsed = json_util.loads(result)
23-
self.assertEqual(
24-
parsed["command"]["pipeline"], [{"$match": {"$expr": {"$eq": ["$_id", id]}}}]
25-
)
23+
self.assertEqual(parsed["command"]["pipeline"], [{"$match": {"_id": id}}])
2624

2725
def test_non_ascii(self):
2826
"""The json is dumped with ensure_ascii=False."""
@@ -32,6 +30,4 @@ def test_non_ascii(self):
3230
# non-ASCII characters.
3331
self.assertIn(name, result)
3432
parsed = json.loads(result)
35-
self.assertEqual(
36-
parsed["command"]["pipeline"], [{"$match": {"$expr": {"$eq": ["$name", name]}}}]
37-
)
33+
self.assertEqual(parsed["command"]["pipeline"], [{"$match": {"name": name}}])

tests/queries_/test_mql.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_all(self):
2525
with self.assertNumQueries(1) as ctx:
2626
list(Author.objects.all())
2727
query = ctx.captured_queries[0]["sql"]
28-
self.assertEqual(query, "db.queries__author.aggregate([{'$match': {'$expr': {}}}])")
28+
self.assertEqual(query, "db.queries__author.aggregate([{'$match': {}}])")
2929

3030
def test_join(self):
3131
with self.assertNumQueries(1) as ctx:
@@ -40,12 +40,13 @@ def test_join(self):
4040
"{'$and': [{'$eq': ['$$parent__field__0', '$_id']}, "
4141
"{'$eq': ['$name', 'Bob']}]}}}], 'as': 'queries__author'}}, "
4242
"{'$unwind': '$queries__author'}, "
43-
"{'$match': {'$expr': {'$eq': ['$queries__author.name', 'Bob']}}}])",
43+
"{'$match': {'queries__author.name': 'Bob'}}])",
4444
)
4545

4646

4747
class FKLookupConditionPushdownTests(TestCase):
4848
def test_filter_on_local_and_related_fields(self):
49+
self.maxDiff = None
4950
with self.assertNumQueries(1) as ctx:
5051
list(Book.objects.filter(title="Don", author__name="John"))
5152
query = ctx.captured_queries[0]["sql"]
@@ -57,8 +58,8 @@ def test_filter_on_local_and_related_fields(self):
5758
"{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0', "
5859
"'$_id']}, {'$eq': ['$name', 'John']}]}}}], 'as': "
5960
"'queries__author'}}, {'$unwind': '$queries__author'}, {'$match': "
60-
"{'$expr': {'$and': [{'$eq': ['$queries__author.name', 'John']}, "
61-
"{'$eq': ['$title', 'Don']}]}}}])",
61+
"{'$and': [{'queries__author.name': 'John'}, "
62+
"{'title': 'Don'}]}}])",
6263
)
6364

6465
def test_or_mixing_local_and_related_fields_is_not_pushable(self):
@@ -71,9 +72,9 @@ def test_or_mixing_local_and_related_fields_is_not_pushable(self):
7172
"'queries__author', 'let': {'parent__field__0': '$author_id'}, "
7273
"'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': "
7374
"['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, "
74-
"{'$unwind': '$queries__author'}, {'$match': {'$expr': {'$or': "
75-
"[{'$eq': ['$title', 'Don']}, {'$eq': ['$queries__author.name', "
76-
"'John']}]}}}])",
75+
"{'$unwind': '$queries__author'}, {'$match': {'$or': "
76+
"[{'title': 'Don'}, {'queries__author.name': "
77+
"'John'}]}}])",
7778
)
7879

7980
def test_filter_on_self_join_fields(self):
@@ -90,9 +91,9 @@ def test_filter_on_self_join_fields(self):
9091
"{'parent__field__0': '$parent_id'}, 'pipeline': [{'$match': {'$expr': "
9192
"{'$and': [{'$eq': ['$$parent__field__0', '$_id']}, {'$and': [{'$eq': "
9293
"['$group_id', ObjectId('6891ff7822e475eddc20f159')]}, {'$eq': ['$name', "
93-
"'parent']}]}]}}}], 'as': 'T2'}}, {'$unwind': '$T2'}, {'$match': {'$expr': "
94-
"{'$and': [{'$eq': ['$T2.group_id', ObjectId('6891ff7822e475eddc20f159')]}, "
95-
"{'$eq': ['$T2.name', 'parent']}]}}}])",
94+
"'parent']}]}]}}}], 'as': 'T2'}}, {'$unwind': '$T2'}, {'$match': "
95+
"{'$and': [{'T2.group_id': ObjectId('6891ff7822e475eddc20f159')}, "
96+
"{'T2.name': 'parent'}]}}])",
9697
)
9798

9899
def test_filter_on_reverse_foreignkey_relation(self):
@@ -107,12 +108,13 @@ def test_filter_on_reverse_foreignkey_relation(self):
107108
"['$$parent__field__0', '$order_id']}, {'$eq': ['$status', "
108109
"ObjectId('6891ff7822e475eddc20f159')]}]}}}], 'as': "
109110
"'queries__orderitem'}}, {'$unwind': '$queries__orderitem'}, "
110-
"{'$match': {'$expr': {'$eq': ['$queries__orderitem.status', "
111-
"ObjectId('6891ff7822e475eddc20f159')]}}}, "
111+
"{'$match': {'queries__orderitem.status': "
112+
"ObjectId('6891ff7822e475eddc20f159')}}, "
112113
"{'$addFields': {'_id': '$_id'}}, {'$sort': SON([('_id', 1)])}])",
113114
)
114115

115116
def test_filter_on_local_and_nested_join_fields(self):
117+
self.maxDiff = None
116118
with self.assertNumQueries(1) as ctx:
117119
list(
118120
Order.objects.filter(
@@ -134,11 +136,11 @@ def test_filter_on_local_and_nested_join_fields(self):
134136
"{'parent__field__0': '$queries__orderitem.order_id'}, "
135137
"'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': "
136138
"['$$parent__field__0', '$_id']}, {'$eq': ['$name', 'My Order']}]}"
137-
"}}], 'as': 'T3'}}, {'$unwind': '$T3'}, {'$match': {'$expr': "
138-
"{'$and': [{'$eq': ['$T3.name', 'My Order']}, {'$eq': "
139-
"['$queries__orderitem.status', "
140-
"ObjectId('6891ff7822e475eddc20f159')]}, {'$eq': ['$name', "
141-
"'My Order']}]}}}, {'$addFields': {'_id': '$_id'}}, "
139+
"}}], 'as': 'T3'}}, {'$unwind': '$T3'}, {'$match': "
140+
"{'$and': [{'T3.name': 'My Order'}, "
141+
"{'queries__orderitem.status': ObjectId('6891ff7822e475eddc20f159')}, "
142+
"{'name': 'My Order'}]}}, "
143+
"{'$addFields': {'_id': '$_id'}}, "
142144
"{'$sort': SON([('_id', 1)])}])",
143145
)
144146

@@ -157,13 +159,14 @@ def test_negated_related_filter_is_not_pushable(self):
157159
)
158160

159161
def test_or_on_local_fields_only(self):
162+
self.maxDiff = None
160163
with self.assertNumQueries(1) as ctx:
161164
list(Order.objects.filter(models.Q(name="A") | models.Q(name="B")))
162165
query = ctx.captured_queries[0]["sql"]
163166
self.assertEqual(
164167
query,
165-
"db.queries__order.aggregate([{'$match': {'$expr': {'$or': "
166-
"[{'$eq': ['$name', 'A']}, {'$eq': ['$name', 'B']}]}}}, "
168+
"db.queries__order.aggregate([{'$match': {'$or': "
169+
"[{'name': 'A'}, {'name': 'B'}]}}, "
167170
"{'$addFields': {'_id': '$_id'}}, {'$sort': SON([('_id', 1)])}])",
168171
)
169172

@@ -177,9 +180,8 @@ def test_or_with_mixed_pushable_and_non_pushable_fields(self):
177180
"'queries__author', 'let': {'parent__field__0': '$author_id'}, "
178181
"'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': "
179182
"['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, "
180-
"{'$unwind': '$queries__author'}, {'$match': {'$expr': {'$or': "
181-
"[{'$eq': ['$queries__author.name', 'John']}, {'$eq': ['$title', "
182-
"'Don']}]}}}])",
183+
"{'$unwind': '$queries__author'}, {'$match': {'$or': "
184+
"[{'queries__author.name': 'John'}, {'title': 'Don'}]}}])",
183185
)
184186

185187
def test_push_equality_between_parent_and_child_fields(self):
@@ -201,6 +203,7 @@ def test_push_equality_between_parent_and_child_fields(self):
201203

202204
class M2MLookupConditionPushdownTests(TestCase):
203205
def test_simple_related_filter_is_pushed(self):
206+
self.maxDiff = None
204207
with self.assertNumQueries(1) as ctx:
205208
list(Library.objects.filter(readers__name="Alice"))
206209
query = ctx.captured_queries[0]["sql"]
@@ -246,7 +249,7 @@ def test_simple_related_filter_is_pushed(self):
246249
], "as": "queries__reader"
247250
}},
248251
{"$unwind": "$queries__reader"},
249-
{"$match": {"$expr": {"$eq": ["$queries__reader.name", "Alice"]}}}
252+
{"$match": {"queries__reader.name": "Alice"}}
250253
])
251254
"""
252255
self.assertEqual(query, uglify_mongo_aggregate(expected_query))
@@ -285,12 +288,12 @@ def test_subquery_join_is_pushed(self):
285288
{"$unwind": "$U2"},
286289
{
287290
"$match": {
288-
"$expr": {
289-
"$and": [
290-
{"$eq": ["$U2.name", "Alice"]},
291+
"$and": [
292+
{"U2.name": "Alice"},
293+
{"$expr":
291294
{"$eq": ["$library_id","$$parent__field__0"]}
292-
]
293-
}
295+
}
296+
]
294297
}
295298
},
296299
{"$project": {"a": {"$literal": 1}}},
@@ -385,12 +388,10 @@ def test_filter_on_local_and_related_fields(self):
385388
{"$unwind": "$queries__reader"},
386389
{
387390
"$match": {
388-
"$expr": {
389-
"$and": [
390-
{"$eq": ["$name", "Central"]},
391-
{"$eq": ["$queries__reader.name", "Alice"]}
392-
]
393-
}
391+
"$and": [
392+
{"name": "Central"},
393+
{"queries__reader.name": "Alice"}
394+
]
394395
}
395396
}
396397
]
@@ -473,7 +474,7 @@ def test_or_on_local_fields_only(self):
473474
}
474475
},
475476
{"$unwind": "$queries__reader"},
476-
{"$match": {"$expr": {"$eq": ["$name", "Ateneo"]}}},
477+
{"$match": {"name": "Ateneo"}},
477478
{
478479
"$project": {
479480
"queries__reader": {"foreing_field": "$queries__reader.name"},
@@ -556,12 +557,10 @@ def test_or_with_mixed_pushable_and_non_pushable_fields(self):
556557
{"$unwind": "$queries__reader"},
557558
{
558559
"$match": {
559-
"$expr": {
560-
"$or": [
561-
{"$eq": ["$queries__reader.name", "Alice"]},
562-
{"$eq": ["$name", "Central"]}
563-
]
564-
}
560+
"$or": [
561+
{"queries__reader.name": "Alice"},
562+
{"name": "Central"}
563+
]
565564
}
566565
}
567566
])

0 commit comments

Comments
 (0)