|
| 1 | +import operator |
| 2 | + |
| 3 | +from django.db import NotSupportedError, connection |
| 4 | +from django.db.models import Index, Q |
| 5 | +from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature |
| 6 | + |
| 7 | +from .models import Article |
| 8 | + |
| 9 | + |
| 10 | +class PartialIndexTests(TestCase): |
| 11 | + def assertAddRemoveIndex(self, editor, model, index): |
| 12 | + editor.add_index(index=index, model=model) |
| 13 | + self.assertIn( |
| 14 | + index.name, |
| 15 | + connection.introspection.get_constraints( |
| 16 | + cursor=None, |
| 17 | + table_name=model._meta.db_table, |
| 18 | + ), |
| 19 | + ) |
| 20 | + editor.remove_index(index=index, model=model) |
| 21 | + |
| 22 | + def test_not_supported(self): |
| 23 | + msg = "MongoDB does not support the 'isnull' lookup in indexes." |
| 24 | + with connection.schema_editor() as editor, self.assertRaisesMessage(NotSupportedError, msg): |
| 25 | + Index( |
| 26 | + name="test", |
| 27 | + fields=["headline"], |
| 28 | + condition=Q(pk__isnull=True), |
| 29 | + )._get_condition_mql(Article, schema_editor=editor) |
| 30 | + |
| 31 | + def test_negated_not_supported(self): |
| 32 | + msg = "MongoDB does not support the '~' operator in indexes." |
| 33 | + with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor: |
| 34 | + Index( |
| 35 | + name="test", |
| 36 | + fields=["headline"], |
| 37 | + condition=~Q(pk=True), |
| 38 | + )._get_condition_mql(Article, schema_editor=editor) |
| 39 | + |
| 40 | + def test_xor_not_supported(self): |
| 41 | + msg = "MongoDB does not support the '^' operator lookup in indexes." |
| 42 | + with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor: |
| 43 | + Index( |
| 44 | + name="test", |
| 45 | + fields=["headline"], |
| 46 | + condition=Q(pk=True) ^ Q(pk=False), |
| 47 | + )._get_condition_mql(Article, schema_editor=editor) |
| 48 | + |
| 49 | + @skipIfDBFeature("supports_or_index_operator") |
| 50 | + def test_or_not_supported(self): |
| 51 | + msg = "MongoDB < 6.0 does not support the '|' operator in indexes." |
| 52 | + with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor: |
| 53 | + Index( |
| 54 | + name="test", |
| 55 | + fields=["headline"], |
| 56 | + condition=Q(pk=True) | Q(pk=False), |
| 57 | + )._get_condition_mql(Article, schema_editor=editor) |
| 58 | + |
| 59 | + @skipIfDBFeature("supports_in_index_operator") |
| 60 | + def test_in_not_supported(self): |
| 61 | + msg = "MongoDB < 6.0 does not support the 'in' lookup in indexes." |
| 62 | + with self.assertRaisesMessage(NotSupportedError, msg), connection.schema_editor() as editor: |
| 63 | + Index( |
| 64 | + name="test", |
| 65 | + fields=["headline"], |
| 66 | + condition=Q(pk__in=[True]), |
| 67 | + )._get_condition_mql(Article, schema_editor=editor) |
| 68 | + |
| 69 | + def test_operations(self): |
| 70 | + operators = ( |
| 71 | + ("gt", "$gt"), |
| 72 | + ("gte", "$gte"), |
| 73 | + ("lt", "$lt"), |
| 74 | + ("lte", "$lte"), |
| 75 | + ) |
| 76 | + for op, mongo_operator in operators: |
| 77 | + with self.subTest(operator=op), connection.schema_editor() as editor: |
| 78 | + index = Index( |
| 79 | + name="test", |
| 80 | + fields=["headline"], |
| 81 | + condition=Q(**{f"number__{op}": 3}), |
| 82 | + ) |
| 83 | + self.assertEqual( |
| 84 | + {"number": {mongo_operator: 3}}, |
| 85 | + index._get_condition_mql(Article, schema_editor=editor), |
| 86 | + ) |
| 87 | + self.assertAddRemoveIndex(editor, Article, index) |
| 88 | + |
| 89 | + @skipUnlessDBFeature("supports_in_index_operator") |
| 90 | + def test_composite_index(self): |
| 91 | + with connection.schema_editor() as editor: |
| 92 | + index = Index( |
| 93 | + name="test", |
| 94 | + fields=["headline"], |
| 95 | + condition=Q(number__gte=3) & (Q(body__gt="test1") | Q(body__in=["A", "B"])), |
| 96 | + ) |
| 97 | + self.assertEqual( |
| 98 | + index._get_condition_mql(Article, schema_editor=editor), |
| 99 | + { |
| 100 | + "$and": [ |
| 101 | + {"number": {"$gte": 3}}, |
| 102 | + {"$or": [{"body": {"$gt": "test1"}}, {"body": {"$in": ["A", "B"]}}]}, |
| 103 | + ] |
| 104 | + }, |
| 105 | + ) |
| 106 | + self.assertAddRemoveIndex(editor, Article, index) |
| 107 | + |
| 108 | + def test_composite_op_index(self): |
| 109 | + operators = ( |
| 110 | + (operator.or_, "$or"), |
| 111 | + (operator.and_, "$and"), |
| 112 | + ) |
| 113 | + if not connection.features.supports_or_index_operator: |
| 114 | + operators = operators[1:] |
| 115 | + for op, mongo_operator in operators: |
| 116 | + with self.subTest(operator=op), connection.schema_editor() as editor: |
| 117 | + index = Index( |
| 118 | + name="test", |
| 119 | + fields=["headline"], |
| 120 | + condition=op(Q(number__gte=3), Q(body__gt="test1")), |
| 121 | + ) |
| 122 | + self.assertEqual( |
| 123 | + {mongo_operator: [{"number": {"$gte": 3}}, {"body": {"$gt": "test1"}}]}, |
| 124 | + index._get_condition_mql(Article, schema_editor=editor), |
| 125 | + ) |
| 126 | + self.assertAddRemoveIndex(editor, Article, index) |
0 commit comments