Skip to content

Commit

Permalink
Add test for mongo version 5 and mongo version 6+
Browse files Browse the repository at this point in the history
  • Loading branch information
WaVEV committed Nov 6, 2024
1 parent 1f35400 commit e2bd68f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django_mongodb/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def where_node_idx(self, compiler, connection):
elif self.connector == XOR:
raise NotSupportedError("Xor in indexes is not supported.")
else:
if not connection.features.is_mongodb_6_0:
raise NotSupportedError("Or in indexes is not supported.")
operator = "$or"
if self.negated:
raise NotSupportedError("Negated field in indexes is not supported.")
Expand Down
46 changes: 45 additions & 1 deletion tests/indexes_/test_mql.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import operator

from django.db import NotSupportedError, connection
from django.db.models import (
Index,
Q,
)
from django.test import (
TestCase,
skipIfDBFeature,
skipUnlessDBFeature,
)

from .models import Article
Expand Down Expand Up @@ -57,11 +61,23 @@ def test_raises_on_xor(self):
)._get_condition_mql(Article, schema_editor=editor)
self.assertEqual(context_manager.exception.args[0], "Xor in indexes is not supported.")

@skipIfDBFeature("is_mongodb_6_0")
def test_raises_on_or(self):
with self.assertRaises(
NotSupportedError
) as context_manager, connection.schema_editor() as editor:
Index(
name="raises_on_negated",
fields=["headline"],
condition=Q(pk=True) ^ Q(pk=False),
)._get_condition_mql(Article, schema_editor=editor)
self.assertEqual(context_manager.exception.args[0], "Or in indexes is not supported.")

@skipUnlessDBFeature("is_mongodb_6_0")
def test_composite_index(self):
with connection.schema_editor() as editor:
index = Index(
name="composite_proposition",
# This is changed
fields=["headline"],
condition=Q(number__gte=3) & (Q(text__gt="test1") | Q(text__in=["A", "B"])),
)
Expand All @@ -86,3 +102,31 @@ def test_composite_index(self):
),
)
editor.remove_index(index=index, model=Article)

def test_composite_op_index(self):
for op in (
[operator.or_, operator.and_] if connection.features.is_mongodb_6_0 else [operator.and_]
):
with self.subTest(operator=op), connection.schema_editor() as editor:
index = Index(
name="composite_proposition",
fields=["headline"],
condition=op(Q(number__gte=3), Q(text__gt="test1")),
)
index._get_condition_mql(Article, schema_editor=editor)
mongo_operator = "$and" if op == operator.and_ else "$or"
target = {mongo_operator: [{"number": {"$gte": 3}}, {"text": {"$gt": "test1"}}]}
self.assertEqual(
target,
index._get_condition_mql(Article, schema_editor=editor),
)
editor.add_index(index=index, model=Article)
with connection.cursor() as cursor:
self.assertIn(
index.name,
connection.introspection.get_constraints(
cursor=cursor,
table_name=Article._meta.db_table,
),
)
editor.remove_index(index=index, model=Article)

0 comments on commit e2bd68f

Please sign in to comment.