Skip to content

Commit 5310c0f

Browse files
timgrahamWaVEV
authored andcommitted
edits
1 parent 017d3ed commit 5310c0f

File tree

4 files changed

+28
-53
lines changed

4 files changed

+28
-53
lines changed

django_mongodb/base.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ def _isnull_operator(a, b):
127127
"lt": lambda a, b: {a: {"$lt": b}},
128128
"lte": lambda a, b: {a: {"$lte": b}},
129129
"in": lambda a, b: {a: {"$in": b}},
130-
# Partial indexes do not support the $not expression.
131-
# Operators like $exists: False, $ne, and $nor cannot be used in partial indexes.
132-
# So, indexes on null values (isnull=True|False) can't be implemented.
133-
"isnull": lambda a, b: {}, # noqa: ARG005
134-
# Partial indexes do not support the $regex expression.
135-
"contains": lambda a, b: {}, # noqa: ARG005
136130
}
137131

138132
display_name = "MongoDB"

django_mongodb/features.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5757
"db_functions.datetime.test_extract_trunc.DateFunctionWithTimeZoneTests.test_trunc_timezone_applied_before_truncation",
5858
# Length of null considered zero rather than null.
5959
"db_functions.text.test_length.LengthTests.test_basic",
60-
# Partial indexes don't support multiple conditions. It requires this
61-
# backend to convert $aggregate MQL syntax to $find (or else just
62-
# generate $find syntax in the first place).
63-
"indexes.tests.PartialIndexTests.test_is_null_condition",
64-
"indexes.tests.PartialIndexTests.test_multiple_conditions",
6560
# Unexpected alias_refcount in alias_map.
6661
"queries.tests.Queries1Tests.test_order_by_tables",
6762
# The $sum aggregation returns 0 instead of None for null.
@@ -574,9 +569,6 @@ def django_test_expected_failures(self):
574569
# Probably something to do with lack of transaction support.
575570
"migration_test_data_persistence.tests.MigrationDataNormalPersistenceTestCase.test_persistence",
576571
},
577-
"Partial indexes to be supported.": {
578-
"indexes.tests.PartialIndexConditionIgnoredTests.test_condition_ignored",
579-
},
580572
"Database caching not implemented.": {
581573
"cache.tests.CreateCacheTableForDBCacheTests",
582574
"cache.tests.DBCacheTests",

django_mongodb/indexes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ def builtin_lookup_idx(self, compiler, connection):
2222
raise NotSupportedError("Expressions as indexes are not supported.")
2323
lhs_mql = self.lhs.target.column
2424
value = process_rhs(self, compiler, connection)
25-
return connection.mongo_operators_idx[self.lookup_name](lhs_mql, value)
25+
try:
26+
return connection.mongo_operators_idx[self.lookup_name](lhs_mql, value)
27+
except KeyError:
28+
raise NotSupportedError(
29+
f"MongoDB does not support the {self.lookup_name} lookup in indexes."
30+
) from None
2631

2732

2833
def where_node_idx(self, compiler, connection):

tests/indexes_/test_mql.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import operator
22

33
from django.db import NotSupportedError, connection
4-
from django.db.models import (
5-
Index,
6-
Q,
7-
)
8-
from django.test import (
9-
TestCase,
10-
skipIfDBFeature,
11-
skipUnlessDBFeature,
12-
)
4+
from django.db.models import Index, Q
5+
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
136

147
from .models import Article
158

@@ -23,8 +16,8 @@ def test_not_supported(self):
2316
condition=Q(pk__isnull=True) & Q(pk__contains="test1"),
2417
)
2518
self.assertEqual(
26-
"{}",
27-
str(index._get_condition_mql(Article, schema_editor=editor)),
19+
index._get_condition_mql(Article, schema_editor=editor),
20+
{},
2821
)
2922
editor.add_index(index=index, model=Article)
3023
with connection.cursor() as cursor:
@@ -37,41 +30,33 @@ def test_not_supported(self):
3730
)
3831
editor.remove_index(index=index, model=Article)
3932

40-
def test_raises_on_negated(self):
41-
with self.assertRaises(
42-
NotSupportedError
43-
) as context_manager, connection.schema_editor() as editor:
33+
def test_negated_not_supported(self):
34+
msg = "Negated field in indexes is not supported."
35+
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
4436
Index(
45-
name="raises_on_negated",
37+
name="test",
4638
fields=["headline"],
4739
condition=~Q(pk=True),
4840
)._get_condition_mql(Article, schema_editor=editor)
49-
self.assertEqual(
50-
context_manager.exception.args[0], "Negated field in indexes is not supported."
51-
)
5241

53-
def test_raises_on_xor(self):
54-
with self.assertRaises(
55-
NotSupportedError
56-
) as context_manager, connection.schema_editor() as editor:
42+
def test_xor_not_supported(self):
43+
msg = "Xor in indexes is not supported."
44+
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
5745
Index(
58-
name="raises_on_negated",
46+
name="test",
5947
fields=["headline"],
6048
condition=Q(pk=True) ^ Q(pk=False),
6149
)._get_condition_mql(Article, schema_editor=editor)
62-
self.assertEqual(context_manager.exception.args[0], "Xor in indexes is not supported.")
6350

6451
@skipIfDBFeature("is_mongodb_6_0")
65-
def test_raises_on_or(self):
66-
with self.assertRaises(
67-
NotSupportedError
68-
) as context_manager, connection.schema_editor() as editor:
52+
def test_or_not_supported(self):
53+
msg = "Or in indexes is not supported."
54+
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
6955
Index(
70-
name="raises_on_negated",
56+
name="test",
7157
fields=["headline"],
7258
condition=Q(pk=True) ^ Q(pk=False),
7359
)._get_condition_mql(Article, schema_editor=editor)
74-
self.assertEqual(context_manager.exception.args[0], "Or in indexes is not supported.")
7560

7661
@skipUnlessDBFeature("is_mongodb_6_0")
7762
def test_composite_index(self):
@@ -82,15 +67,14 @@ def test_composite_index(self):
8267
condition=Q(number__gte=3) & (Q(text__gt="test1") | Q(text__in=["A", "B"])),
8368
)
8469
index._get_condition_mql(Article, schema_editor=editor)
85-
target = {
86-
"$and": [
87-
{"number": {"$gte": 3}},
88-
{"$or": [{"text": {"$gt": "test1"}}, {"text": {"$in": ["A", "B"]}}]},
89-
]
90-
}
9170
self.assertEqual(
92-
target,
9371
index._get_condition_mql(Article, schema_editor=editor),
72+
{
73+
"$and": [
74+
{"number": {"$gte": 3}},
75+
{"$or": [{"text": {"$gt": "test1"}}, {"text": {"$in": ["A", "B"]}}]},
76+
]
77+
},
9478
)
9579
editor.add_index(index=index, model=Article)
9680
with connection.cursor() as cursor:

0 commit comments

Comments
 (0)