Skip to content

Commit 383fbd9

Browse files
committed
Add indexes_ tests
1 parent 0b04d52 commit 383fbd9

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

.github/workflows/runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"get_or_create",
6767
"i18n",
6868
"indexes",
69+
"indexes_",
6970
"inline_formsets",
7071
"introspection",
7172
"invalid_models_tests",

tests/indexes_/__init__.py

Whitespace-only changes.

tests/indexes_/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.db import models
2+
3+
4+
class Article(models.Model):
5+
headline = models.CharField(max_length=100)
6+
pub_date = models.DateTimeField()
7+
published = models.BooleanField(default=False)
8+
number = models.IntegerField()
9+
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)