Skip to content

Commit d4fe20d

Browse files
committed
test triage and rhs json handle.
1 parent 53caaee commit d4fe20d

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

django_mongodb/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
8080
"lte": lambda val: {"$lte": val},
8181
"in": lambda val: {"$in": val},
8282
"range": lambda val: {"$gte": val[0], "$lte": val[1]},
83-
"isnull": lambda val: None if val else {"$ne": None},
83+
"isnull": lambda val: None if val else {"$ne": None, "$exists": True},
8484
"iexact": safe_regex("^%s$", re.IGNORECASE),
8585
"startswith": safe_regex("^%s"),
8686
"istartswith": safe_regex("^%s", re.IGNORECASE),

django_mongodb/expressions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from django.db.models.expressions import Col, Value
1+
from django.db import NotSupportedError
2+
from django.db.models.expressions import Col, ExpressionWrapper, Value
3+
from django.db.models.functions.comparison import Cast
24

35

46
def col(self, compiler, connection): # noqa: ARG001
@@ -13,7 +15,22 @@ def value_agg(self, compiler, connection): # noqa: ARG001
1315
return {"$literal": self.value}
1416

1517

18+
def expression(self, compiler, connection):
19+
return self.expression.as_mql(compiler, connection)
20+
21+
22+
def expression_agg(self, compiler, connection):
23+
return self.expression.as_mql_agg(compiler, connection)
24+
25+
26+
def cast(self, compiler, connection): # noqa: ARG001
27+
raise NotSupportedError("Cast is not supported on this database backend")
28+
29+
1630
def register_expressions():
1731
Col.as_mql = col
1832
Value.as_mql = value
1933
Value.as_mql_agg = value_agg
34+
ExpressionWrapper.as_mql = expression
35+
ExpressionWrapper.as_mql_agg = expression_agg
36+
Cast.as_mql = cast

django_mongodb/features.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
165165
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_outerref",
166166
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_subquery_with_parameters",
167167
"lookup.tests.LookupQueryingTests.test_filter_subquery_lhs",
168+
"model_fields.test_jsonfield.TestQuerying.test_usage_in_subquery",
168169
# ExpressionWrapper not supported.
169170
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_expression_annotation_with_aggregation",
170171
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_f_expression_annotation_with_aggregation",
@@ -202,6 +203,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
202203
# Year lookup + lt/gt crashes: 'dict' object has no attribute 'startswith'
203204
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_year_greaterthan_lookup",
204205
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_year_lessthan_lookup",
206+
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_on_subquery",
207+
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_count",
208+
"model_fields.test_jsonfield.TestQuerying.test_obj_subquery_lookup",
205209
},
206210
"Count doesn't work in QuerySet.annotate()": {
207211
"annotations.tests.AliasTests.test_alias_annotate_with_aggregation",
@@ -276,6 +280,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
276280
"update.tests.SimpleTest.test_empty_update_with_inheritance",
277281
"update.tests.SimpleTest.test_foreign_key_update_with_id",
278282
"update.tests.SimpleTest.test_nonempty_update_with_inheritance",
283+
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_key_transform",
284+
"model_fields.test_jsonfield.TestQuerying.test_ordering_by_transform",
285+
"model_fields.test_jsonfield.TestQuerying.test_order_grouping_custom_decoder",
286+
"model_fields.test_jsonfield.TestQuerying.test_join_key_transform_annotation_expression",
279287
},
280288
"Test inspects query for SQL": {
281289
"lookup.tests.LookupTests.test_in_ignore_none",
@@ -290,6 +298,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
290298
"timezones.tests.NewDatabaseTests.test_cursor_execute_returns_naive_datetime",
291299
"timezones.tests.NewDatabaseTests.test_cursor_explicit_time_zone",
292300
"timezones.tests.NewDatabaseTests.test_raw_sql",
301+
"model_fields.test_jsonfield.TestQuerying.test_key_transform_raw_expression",
302+
"model_fields.test_jsonfield.TestQuerying.test_key_sql_injection_escape",
303+
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_raw_expression",
293304
},
294305
"Transform not supported.": {
295306
"db_functions.math.test_abs.AbsTests.test_transform",
@@ -340,6 +351,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
340351
"Mongodb's Null behaviour is different from sql's": {
341352
"model_fields.test_jsonfield.TestQuerying.test_none_key_and_exact_lookup",
342353
# "model_fields.test_jsonfield.TestQuerying.test_isnull_key",
354+
"model_fields.test_jsonfield.TestSaveLoad.test_json_null_different_from_sql_null",
355+
"model_fields.test_jsonfield.TestQuerying.test_none_key",
343356
},
344357
"Pipeline filtering": {"model_fields.test_jsonfield.TestQuerying.test_icontains"},
345358
}

django_mongodb/fields.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def from_db_value(self, value, expression, connection):
6363

6464

6565
def json_process_rhs(node, compiler, connection):
66+
rhs = node.rhs
67+
if hasattr(rhs, "as_mql"):
68+
return rhs.as_mql(compiler, connection)
6669
_, value = node.process_rhs(compiler, connection)
6770
lookup_name = node.lookup_name
6871
if lookup_name not in ("in", "range"):

django_mongodb/query_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def process_lhs(node, compiler, connection, bare_column_ref=False):
1919
return mql
2020

2121

22-
def process_rhs(node, compiler, connection):
22+
def process_rhs(node, compiler, connection, json_column_ref=False):
2323
rhs = node.rhs
2424
if hasattr(rhs, "as_mql"):
2525
return rhs.as_mql(compiler, connection)
@@ -30,11 +30,11 @@ def process_rhs(node, compiler, connection):
3030
value = value[0]
3131
# Remove percent signs added by PatternLookup.process_rhs() for LIKE
3232
# queries.
33-
if lookup_name in ("startswith", "istartswith"):
33+
if lookup_name in ("startswith", "istartswith") and json_column_ref is False:
3434
value = value[:-1]
35-
elif lookup_name in ("endswith", "iendswith"):
35+
elif lookup_name in ("endswith", "iendswith") and json_column_ref is False:
3636
value = value[1:]
37-
elif lookup_name in ("contains", "icontains"):
37+
elif lookup_name in ("contains", "icontains") and json_column_ref is False:
3838
value = value[1:-1]
3939

4040
return connection.ops.prep_lookup_value(value, node.lhs.output_field, node.lookup_name)

0 commit comments

Comments
 (0)