From e2bd68f76d75f02657f6bf694724b3f590ee4bb8 Mon Sep 17 00:00:00 2001 From: Emanuel Lupi Date: Wed, 6 Nov 2024 00:41:28 -0300 Subject: [PATCH] Add test for mongo version 5 and mongo version 6+ --- django_mongodb/indexes.py | 2 ++ tests/indexes_/test_mql.py | 46 +++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/django_mongodb/indexes.py b/django_mongodb/indexes.py index 881485c4..afd08fa3 100644 --- a/django_mongodb/indexes.py +++ b/django_mongodb/indexes.py @@ -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.") diff --git a/tests/indexes_/test_mql.py b/tests/indexes_/test_mql.py index baebeed4..a4d7439e 100644 --- a/tests/indexes_/test_mql.py +++ b/tests/indexes_/test_mql.py @@ -1,3 +1,5 @@ +import operator + from django.db import NotSupportedError, connection from django.db.models import ( Index, @@ -5,6 +7,8 @@ ) from django.test import ( TestCase, + skipIfDBFeature, + skipUnlessDBFeature, ) from .models import Article @@ -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"])), ) @@ -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)