Skip to content

Commit 85b66bc

Browse files
committed
Refactor and fix unit tests.
1 parent e216a61 commit 85b66bc

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

django_mongodb/base.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,6 @@ def _isnull_operator(a, b):
119119
"iregex": lambda a, b: regex_match(a, b, insensitive=True),
120120
}
121121

122-
mongo_operators_idx = {
123-
"exact": lambda a, b: {a: {"$eq": b}},
124-
"gt": lambda a, b: {a: {"$gt": b}},
125-
"gte": lambda a, b: {a: {"$gte": b}},
126-
"lt": lambda a, b: {a: {"$lt": b}},
127-
"lte": lambda a, b: {a: {"$lte": b}},
128-
"in": lambda a, b: {a: {"$in": b}},
129-
}
130-
131122
display_name = "MongoDB"
132123
vendor = "mongodb"
133124
Database = Database

django_mongodb/indexes.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
from django.db import NotSupportedError
33
from django.db.models import Index
44
from django.db.models.expressions import Col
5+
from django.db.models.fields.related_lookups import In
56
from django.db.models.lookups import BuiltinLookup
67
from django.db.models.sql.query import Query
78
from django.db.models.sql.where import AND, XOR, WhereNode
89

910
from .query_utils import process_rhs
1011

12+
mongo_operators_idx = {
13+
"exact": lambda a, b: {a: {"$eq": b}},
14+
"gt": lambda a, b: {a: {"$gt": b}},
15+
"gte": lambda a, b: {a: {"$gte": b}},
16+
"lt": lambda a, b: {a: {"$lt": b}},
17+
"lte": lambda a, b: {a: {"$lte": b}},
18+
"in": lambda a, b: {a: {"$in": b}},
19+
}
20+
1121

1222
def _get_condition_mql(self, model, schema_editor):
1323
"""Analogous to Index._get_condition_sql()."""
@@ -23,13 +33,21 @@ def builtin_lookup_idx(self, compiler, connection):
2333
lhs_mql = self.lhs.target.column
2434
value = process_rhs(self, compiler, connection)
2535
try:
26-
return connection.mongo_operators_idx[self.lookup_name](lhs_mql, value)
36+
return mongo_operators_idx[self.lookup_name](lhs_mql, value)
2737
except KeyError:
2838
raise NotSupportedError(
29-
f"MongoDB does not support the {self.lookup_name} lookup in indexes."
39+
f"MongoDB does not support the '{self.lookup_name}' lookup in indexes."
3040
) from None
3141

3242

43+
def in_idx(self, compiler, connection):
44+
if not connection.features.is_mongodb_6_0:
45+
raise NotSupportedError(
46+
f"MongoDB does not support the {self.lookup_name} lookup in indexes."
47+
)
48+
return builtin_lookup_idx(self, compiler, connection)
49+
50+
3351
def where_node_idx(self, compiler, connection):
3452
if self.connector == AND:
3553
full_needed, empty_needed = len(self.children), 1
@@ -38,13 +56,13 @@ def where_node_idx(self, compiler, connection):
3856
if self.connector == AND:
3957
operator = "$and"
4058
elif self.connector == XOR:
41-
raise NotSupportedError("Xor in indexes is not supported.")
59+
raise NotSupportedError("MongoDB does not support the 'Xor' lookup in indexes.")
4260
else:
4361
if not connection.features.is_mongodb_6_0:
44-
raise NotSupportedError("Or in indexes is not supported.")
62+
raise NotSupportedError("MongoDB does not support the 'Or' lookup in indexes.")
4563
operator = "$or"
4664
if self.negated:
47-
raise NotSupportedError("Negated field in indexes is not supported.")
65+
raise NotSupportedError("MongoDB does not support negated field in indexes.")
4866
children_mql = []
4967
for child in self.children:
5068
try:
@@ -77,5 +95,6 @@ def where_node_idx(self, compiler, connection):
7795

7896
def register_indexes():
7997
BuiltinLookup.as_mql_idx = builtin_lookup_idx
98+
In.as_mql_idx = in_idx
8099
Index._get_condition_mql = _get_condition_mql
81100
WhereNode.as_mql_idx = where_node_idx

tests/indexes_/models.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@ class Article(models.Model):
77
published = models.BooleanField(default=False)
88
number = models.IntegerField()
99
text = models.CharField(max_length=100)
10-
11-
class Meta:
12-
indexes = [models.Index(fields=["headline", "pub_date"])]

tests/indexes_/test_mql.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,16 @@
99

1010
class PartialIndexTests(TestCase):
1111
def test_not_supported(self):
12-
with connection.schema_editor() as editor:
13-
index = Index(
12+
msg = "MongoDB does not support the 'isnull' lookup in indexes."
13+
with connection.schema_editor() as editor, self.assertRaisesMessage(NotSupportedError, msg):
14+
Index(
1415
name="not_supported",
1516
fields=["headline"],
1617
condition=Q(pk__isnull=True) & Q(pk__contains="test1"),
17-
)
18-
self.assertEqual(
19-
index._get_condition_mql(Article, schema_editor=editor),
20-
{},
21-
)
22-
editor.add_index(index=index, model=Article)
23-
with connection.cursor() as cursor:
24-
self.assertIn(
25-
index.name,
26-
connection.introspection.get_constraints(
27-
cursor=cursor,
28-
table_name=Article._meta.db_table,
29-
),
30-
)
31-
editor.remove_index(index=index, model=Article)
18+
)._get_condition_mql(Article, schema_editor=editor)
3219

3320
def test_negated_not_supported(self):
34-
msg = "Negated field in indexes is not supported."
21+
msg = "MongoDB does not support negated field in indexes."
3522
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
3623
Index(
3724
name="test",
@@ -40,7 +27,7 @@ def test_negated_not_supported(self):
4027
)._get_condition_mql(Article, schema_editor=editor)
4128

4229
def test_xor_not_supported(self):
43-
msg = "Xor in indexes is not supported."
30+
msg = "MongoDB does not support the 'Xor' lookup in indexes."
4431
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
4532
Index(
4633
name="test",
@@ -50,12 +37,22 @@ def test_xor_not_supported(self):
5037

5138
@skipIfDBFeature("is_mongodb_6_0")
5239
def test_or_not_supported(self):
53-
msg = "Or in indexes is not supported."
40+
msg = "MongoDB does not support the 'Or' lookup in indexes."
5441
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
5542
Index(
5643
name="test",
5744
fields=["headline"],
58-
condition=Q(pk=True) ^ Q(pk=False),
45+
condition=Q(pk=True) | Q(pk=False),
46+
)._get_condition_mql(Article, schema_editor=editor)
47+
48+
@skipIfDBFeature("is_mongodb_6_0")
49+
def test_in_not_supported(self):
50+
msg = "MongoDB does not support the 'in' lookup in indexes."
51+
with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor:
52+
Index(
53+
name="test",
54+
fields=["headline"],
55+
condition=Q(pk__in=[True]),
5956
)._get_condition_mql(Article, schema_editor=editor)
6057

6158
@skipUnlessDBFeature("is_mongodb_6_0")

0 commit comments

Comments
 (0)