|
| 1 | +from django.db import NotSupportedError, connection |
| 2 | +from django.db.models import ( |
| 3 | + Index, |
| 4 | + Q, |
| 5 | +) |
| 6 | +from django.test import ( |
| 7 | + TestCase, |
| 8 | +) |
| 9 | + |
| 10 | +from .models import Article |
| 11 | + |
| 12 | + |
| 13 | +class PartialIndexTests(TestCase): |
| 14 | + def test_not_supported(self): |
| 15 | + with connection.schema_editor() as editor: |
| 16 | + index = Index( |
| 17 | + name="not_supported", |
| 18 | + # This is changed |
| 19 | + fields=["headline"], |
| 20 | + condition=Q(pk__isnull=True) & Q(pk__contains="test1"), |
| 21 | + ) |
| 22 | + self.assertEqual( |
| 23 | + "{}", |
| 24 | + str(index._get_condition_mql(Article, schema_editor=editor)), |
| 25 | + ) |
| 26 | + editor.add_index(index=index, model=Article) |
| 27 | + with connection.cursor() as cursor: |
| 28 | + self.assertIn( |
| 29 | + index.name, |
| 30 | + connection.introspection.get_constraints( |
| 31 | + cursor=cursor, |
| 32 | + table_name=Article._meta.db_table, |
| 33 | + ), |
| 34 | + ) |
| 35 | + editor.remove_index(index=index, model=Article) |
| 36 | + |
| 37 | + def test_raises_on_negated(self): |
| 38 | + with self.assertRaises(NotSupportedError), connection.schema_editor() as editor: |
| 39 | + Index( |
| 40 | + name="raises_on_negated", |
| 41 | + # This is changed |
| 42 | + fields=["headline"], |
| 43 | + condition=~Q(pk=True), |
| 44 | + )._get_condition_mql(Article, schema_editor=editor) |
| 45 | + |
| 46 | + def test_composite_index(self): |
| 47 | + with connection.schema_editor() as editor: |
| 48 | + index = Index( |
| 49 | + name="composite_proposition", |
| 50 | + # This is changed |
| 51 | + fields=["headline"], |
| 52 | + condition=Q(number__gte=3) & (Q(text__gt="test1") | Q(text__in=["A", "B"])), |
| 53 | + ) |
| 54 | + index._get_condition_mql(Article, schema_editor=editor) |
| 55 | + self.assertEqual( |
| 56 | + "{'$and': [{'number': {'$gte': 3}}, {'$or': [{'text': {'$gt': 'test1'}}, " |
| 57 | + "{'text': {'$in': ['A', 'B']}}]}]}", |
| 58 | + str(index._get_condition_mql(Article, schema_editor=editor)), |
| 59 | + ) |
| 60 | + editor.add_index(index=index, model=Article) |
| 61 | + with connection.cursor() as cursor: |
| 62 | + self.assertIn( |
| 63 | + index.name, |
| 64 | + connection.introspection.get_constraints( |
| 65 | + cursor=cursor, |
| 66 | + table_name=Article._meta.db_table, |
| 67 | + ), |
| 68 | + ) |
| 69 | + editor.remove_index(index=index, model=Article) |
0 commit comments