Skip to content

Commit

Permalink
edits
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Nov 12, 2024
1 parent d1d9c33 commit 3dcb730
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ def collection_name(self):
if isinstance(v, BaseTable) and self.query.alias_refcount[k]
)
except StopIteration:
# Use a dummy collection if the query doesn't specify a table
# (such as Constraint.validate() with a condition).
query = self.query_class(self)
query.aggregation_pipeline = [{"$facet": {"__null": []}}]
self.subqueries.insert(0, query)
Expand Down
6 changes: 4 additions & 2 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_expression_indexes = False
supports_foreign_keys = False
supports_ignore_conflicts = False
# Before MongoDB 6.0, $in cannot be used in partialFilterExpression.
supports_in_index_operator = property(operator.attrgetter("is_mongodb_6_0"))
# Before MongoDB 6.0, $or cannot be used in partialFilterExpression.
supports_or_index_operator = property(operator.attrgetter("is_mongodb_6_0"))
supports_json_field_contains = False
# BSON Date type doesn't support microsecond precision.
supports_microsecond_precision = False
Expand Down Expand Up @@ -97,7 +100,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"expressions.tests.ExpressionOperatorTests.test_lefthand_bitwise_xor_right_null",
"expressions.tests.ExpressionOperatorTests.test_lefthand_transformed_field_bitwise_or",
}
# Before MongoDB 6.0, $in cannot be used in partialFilterExpression.
_django_test_expected_failures_partial_expression_in = {
"schema.tests.SchemaTests.test_remove_ignored_unique_constraint_not_create_fk_index",
}
Expand All @@ -106,7 +108,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
def django_test_expected_failures(self):
expected_failures = super().django_test_expected_failures
expected_failures.update(self._django_test_expected_failures)
if not self.is_mongodb_6_0:
if not self.supports_in_index_operator:
expected_failures.update(self._django_test_expected_failures_partial_expression_in)
if not self.is_mongodb_6_3:
expected_failures.update(self._django_test_expected_failures_bitwise)
Expand Down
6 changes: 3 additions & 3 deletions django_mongodb/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .query_utils import process_rhs

mongo_operators_idx = {
MONGO_INDEX_OPERATORS = {
"exact": "$eq",
"gt": "$gt",
"gte": "$gte",
Expand All @@ -33,7 +33,7 @@ def builtin_lookup_idx(self, compiler, connection):
lhs_mql = self.lhs.target.column
value = process_rhs(self, compiler, connection)
try:
operator = mongo_operators_idx[self.lookup_name]
operator = MONGO_INDEX_OPERATORS[self.lookup_name]
except KeyError:
raise NotSupportedError(
f"MongoDB does not support the '{self.lookup_name}' lookup in indexes."
Expand All @@ -44,7 +44,7 @@ def builtin_lookup_idx(self, compiler, connection):
def in_idx(self, compiler, connection):
if not connection.features.supports_in_index_operator:
raise NotSupportedError(
f"MongoDB < 6.0 does not support the {self.lookup_name} lookup in indexes."
f"MongoDB < 6.0 does not support the '{self.lookup_name}' lookup in indexes."
)
return builtin_lookup_idx(self, compiler, connection)

Expand Down
4 changes: 1 addition & 3 deletions django_mongodb/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def is_null(self, compiler, connection):
if not isinstance(self.rhs, bool):
raise ValueError("The QuerySet value for an isnull lookup must be True or False.")
if isinstance(self.lhs, Value):
if self.lhs.value is None or (
self.lhs.value == "" and connection.features.interprets_empty_strings_as_nulls
):
if self.lhs.value is None:
result_exception = FullResultSet if self.rhs else EmptyResultSet
else:
result_exception = EmptyResultSet if self.rhs else FullResultSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def test_xor_not_supported(self):
condition=Q(pk=True) ^ Q(pk=False),
)._get_condition_mql(Article, schema_editor=editor)

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

@skipIfDBFeature("supports_in_index_operator")
def test_in_not_supported(self):
msg = "MongoDB does not support the 'in' lookup in indexes."
msg = "MongoDB < 6.0 does not support the 'in' lookup in indexes."
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
Index(
name="test",
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_composite_op_index(self):
(operator.or_, "$or"),
(operator.and_, "$and"),
)
if connection.features.supports_in_index_operator
if connection.features.supports_or_index_operator
else ((operator.and_, "$and"),)
)
for op, mongo_operator in operators:
Expand Down

0 comments on commit 3dcb730

Please sign in to comment.