Skip to content

Commit 38cf2b4

Browse files
committed
Move assertAddRemoveIndex() to a mixin
1 parent d5b0e1b commit 38cf2b4

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

tests/indexes_/test_base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.db import connection
2+
3+
4+
class SchemaAssertionMixin:
5+
def assertAddRemoveIndex(self, editor, model, index):
6+
with self.assertNumQueries(1):
7+
editor.add_index(index=index, model=model)
8+
try:
9+
self.assertIn(
10+
index.name,
11+
connection.introspection.get_constraints(
12+
cursor=None,
13+
table_name=model._meta.db_table,
14+
),
15+
)
16+
finally:
17+
editor.remove_index(index=index, model=model)
18+
self.assertNotIn(
19+
index.name,
20+
connection.introspection.get_constraints(
21+
cursor=None,
22+
table_name=model._meta.db_table,
23+
),
24+
)

tests/indexes_/test_condition.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,10 @@
55
from django.test import TestCase
66

77
from .models import Article
8+
from .test_base import SchemaAssertionMixin
89

910

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-
11+
class PartialIndexTests(SchemaAssertionMixin, TestCase):
2212
def test_not_supported(self):
2313
msg = "MongoDB does not support the 'isnull' lookup in indexes."
2414
with connection.schema_editor() as editor, self.assertRaisesMessage(NotSupportedError, msg):

0 commit comments

Comments
 (0)