Skip to content

Commit 3dcb730

Browse files
committed
edits
1 parent d1d9c33 commit 3dcb730

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

django_mongodb/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ def collection_name(self):
442442
if isinstance(v, BaseTable) and self.query.alias_refcount[k]
443443
)
444444
except StopIteration:
445+
# Use a dummy collection if the query doesn't specify a table
446+
# (such as Constraint.validate() with a condition).
445447
query = self.query_class(self)
446448
query.aggregation_pipeline = [{"$facet": {"__null": []}}]
447449
self.subqueries.insert(0, query)

django_mongodb/features.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
2323
supports_expression_indexes = False
2424
supports_foreign_keys = False
2525
supports_ignore_conflicts = False
26+
# Before MongoDB 6.0, $in cannot be used in partialFilterExpression.
2627
supports_in_index_operator = property(operator.attrgetter("is_mongodb_6_0"))
28+
# Before MongoDB 6.0, $or cannot be used in partialFilterExpression.
29+
supports_or_index_operator = property(operator.attrgetter("is_mongodb_6_0"))
2730
supports_json_field_contains = False
2831
# BSON Date type doesn't support microsecond precision.
2932
supports_microsecond_precision = False
@@ -97,7 +100,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
97100
"expressions.tests.ExpressionOperatorTests.test_lefthand_bitwise_xor_right_null",
98101
"expressions.tests.ExpressionOperatorTests.test_lefthand_transformed_field_bitwise_or",
99102
}
100-
# Before MongoDB 6.0, $in cannot be used in partialFilterExpression.
101103
_django_test_expected_failures_partial_expression_in = {
102104
"schema.tests.SchemaTests.test_remove_ignored_unique_constraint_not_create_fk_index",
103105
}
@@ -106,7 +108,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
106108
def django_test_expected_failures(self):
107109
expected_failures = super().django_test_expected_failures
108110
expected_failures.update(self._django_test_expected_failures)
109-
if not self.is_mongodb_6_0:
111+
if not self.supports_in_index_operator:
110112
expected_failures.update(self._django_test_expected_failures_partial_expression_in)
111113
if not self.is_mongodb_6_3:
112114
expected_failures.update(self._django_test_expected_failures_bitwise)

django_mongodb/indexes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from .query_utils import process_rhs
1111

12-
mongo_operators_idx = {
12+
MONGO_INDEX_OPERATORS = {
1313
"exact": "$eq",
1414
"gt": "$gt",
1515
"gte": "$gte",
@@ -33,7 +33,7 @@ def builtin_lookup_idx(self, compiler, connection):
3333
lhs_mql = self.lhs.target.column
3434
value = process_rhs(self, compiler, connection)
3535
try:
36-
operator = mongo_operators_idx[self.lookup_name]
36+
operator = MONGO_INDEX_OPERATORS[self.lookup_name]
3737
except KeyError:
3838
raise NotSupportedError(
3939
f"MongoDB does not support the '{self.lookup_name}' lookup in indexes."
@@ -44,7 +44,7 @@ def builtin_lookup_idx(self, compiler, connection):
4444
def in_idx(self, compiler, connection):
4545
if not connection.features.supports_in_index_operator:
4646
raise NotSupportedError(
47-
f"MongoDB < 6.0 does not support the {self.lookup_name} lookup in indexes."
47+
f"MongoDB < 6.0 does not support the '{self.lookup_name}' lookup in indexes."
4848
)
4949
return builtin_lookup_idx(self, compiler, connection)
5050

django_mongodb/lookups.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ def is_null(self, compiler, connection):
5151
if not isinstance(self.rhs, bool):
5252
raise ValueError("The QuerySet value for an isnull lookup must be True or False.")
5353
if isinstance(self.lhs, Value):
54-
if self.lhs.value is None or (
55-
self.lhs.value == "" and connection.features.interprets_empty_strings_as_nulls
56-
):
54+
if self.lhs.value is None:
5755
result_exception = FullResultSet if self.rhs else EmptyResultSet
5856
else:
5957
result_exception = EmptyResultSet if self.rhs else FullResultSet

tests/indexes_/test_mql.py renamed to tests/indexes_/test_condition.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def test_xor_not_supported(self):
3535
condition=Q(pk=True) ^ Q(pk=False),
3636
)._get_condition_mql(Article, schema_editor=editor)
3737

38-
@skipIfDBFeature("supports_in_index_operator")
38+
@skipIfDBFeature("supports_or_index_operator")
3939
def test_or_not_supported(self):
40-
msg = "MongoDB does not support the 'Or' lookup in indexes."
40+
msg = "MongoDB < 6.0 does not support the '|' operator in indexes."
4141
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
4242
Index(
4343
name="test",
@@ -47,7 +47,7 @@ def test_or_not_supported(self):
4747

4848
@skipIfDBFeature("supports_in_index_operator")
4949
def test_in_not_supported(self):
50-
msg = "MongoDB does not support the 'in' lookup in indexes."
50+
msg = "MongoDB < 6.0 does not support the 'in' lookup in indexes."
5151
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
5252
Index(
5353
name="test",
@@ -90,7 +90,7 @@ def test_composite_op_index(self):
9090
(operator.or_, "$or"),
9191
(operator.and_, "$and"),
9292
)
93-
if connection.features.supports_in_index_operator
93+
if connection.features.supports_or_index_operator
9494
else ((operator.and_, "$and"),)
9595
)
9696
for op, mongo_operator in operators:

0 commit comments

Comments
 (0)