Skip to content

Commit a661dac

Browse files
committed
test triage and rhs json handle.
1 parent ac49fdc commit a661dac

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5656
"lookup.tests.LookupTests.test_pattern_lookups_with_substr",
5757
# Querying ObjectID with string doesn't work.
5858
"lookup.tests.LookupTests.test_lookup_int_as_str",
59+
# Cast is not supported
60+
"model_fields.test_jsonfield.TestQuerying.test_key_transform_expression",
61+
"model_fields.test_jsonfield.TestQuerying.test_key_transform_annotation_expression",
62+
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_annotation_expression",
63+
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_expression",
64+
# Nand does not exist in Mongo.
65+
"model_fields.test_jsonfield.TestQuerying.test_lookup_exclude",
66+
"model_fields.test_jsonfield.TestQuerying.test_lookup_exclude_nonexistent_key",
5967
}
6068

6169
django_test_skips = {
@@ -163,6 +171,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
163171
# Subquery not supported.
164172
"annotations.tests.NonAggregateAnnotationTestCase.test_empty_queryset_annotation",
165173
"lookup.tests.LookupQueryingTests.test_filter_subquery_lhs",
174+
"model_fields.test_jsonfield.TestQuerying.test_usage_in_subquery",
166175
# ExpressionWrapper not supported.
167176
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_expression_annotation_with_aggregation",
168177
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_f_expression_annotation_with_aggregation",
@@ -197,6 +206,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
197206
"annotations.tests.AliasTests.test_order_by_alias",
198207
"annotations.tests.NonAggregateAnnotationTestCase.test_order_by_aggregate",
199208
"annotations.tests.NonAggregateAnnotationTestCase.test_order_by_annotation",
209+
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_on_subquery",
210+
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_count",
211+
"model_fields.test_jsonfield.TestQuerying.test_obj_subquery_lookup",
200212
},
201213
"Count doesn't work in QuerySet.annotate()": {
202214
"annotations.tests.AliasTests.test_alias_annotate_with_aggregation",
@@ -270,6 +282,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
270282
"update.tests.SimpleTest.test_empty_update_with_inheritance",
271283
"update.tests.SimpleTest.test_foreign_key_update_with_id",
272284
"update.tests.SimpleTest.test_nonempty_update_with_inheritance",
285+
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_key_transform",
286+
"model_fields.test_jsonfield.TestQuerying.test_ordering_by_transform",
287+
"model_fields.test_jsonfield.TestQuerying.test_order_grouping_custom_decoder",
288+
"model_fields.test_jsonfield.TestQuerying.test_join_key_transform_annotation_expression",
273289
},
274290
"Test inspects query for SQL": {
275291
"lookup.tests.LookupTests.test_in_ignore_none",
@@ -284,6 +300,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
284300
"timezones.tests.NewDatabaseTests.test_cursor_execute_returns_aware_datetime",
285301
"timezones.tests.NewDatabaseTests.test_cursor_explicit_time_zone",
286302
"timezones.tests.NewDatabaseTests.test_raw_sql",
303+
"model_fields.test_jsonfield.TestQuerying.test_key_transform_raw_expression",
304+
"model_fields.test_jsonfield.TestQuerying.test_key_sql_injection_escape",
305+
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_raw_expression",
287306
},
288307
"BSON Date type doesn't support microsecond precision.": {
289308
"basic.tests.ModelRefreshTests.test_refresh_unsaved",
@@ -301,6 +320,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
301320
"Mongodb's Null behaviour is different from sql's": {
302321
"model_fields.test_jsonfield.TestQuerying.test_none_key_and_exact_lookup",
303322
# "model_fields.test_jsonfield.TestQuerying.test_isnull_key",
323+
"model_fields.test_jsonfield.TestSaveLoad.test_json_null_different_from_sql_null",
324+
"model_fields.test_jsonfield.TestQuerying.test_none_key",
304325
},
305326
"Pipeline filtering": {"model_fields.test_jsonfield.TestQuerying.test_icontains"},
306327
}

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
@@ -15,7 +15,7 @@ def process_lhs(node, compiler, connection, bare_column_ref=False):
1515
return mql
1616

1717

18-
def process_rhs(node, compiler, connection):
18+
def process_rhs(node, compiler, connection, json_column_ref=False):
1919
rhs = node.rhs
2020
if hasattr(rhs, "as_mql"):
2121
return rhs.as_mql(compiler, connection)
@@ -26,11 +26,11 @@ def process_rhs(node, compiler, connection):
2626
value = value[0]
2727
# Remove percent signs added by PatternLookup.process_rhs() for LIKE
2828
# queries.
29-
if lookup_name in ("startswith", "istartswith"):
29+
if lookup_name in ("startswith", "istartswith") and json_column_ref is False:
3030
value = value[:-1]
31-
elif lookup_name in ("endswith", "iendswith"):
31+
elif lookup_name in ("endswith", "iendswith") and json_column_ref is False:
3232
value = value[1:]
33-
elif lookup_name in ("contains", "icontains"):
33+
elif lookup_name in ("contains", "icontains") and json_column_ref is False:
3434
value = value[1:-1]
3535

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

0 commit comments

Comments
 (0)