summaryrefslogtreecommitdiff
path: root/tests/model_indexes
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2018-09-13 09:34:02 +0200
committerTim Graham <timograham@gmail.com>2018-10-29 19:34:54 -0400
commita906c9898284a9aecb5f48bdc534e9c1273864a6 (patch)
tree3861d861073753fd0f10cf1d46413f693e43bb7d /tests/model_indexes
parent9625d13f7b76ed22c8d4c24d411531abe8502854 (diff)
downloaddjango-a906c9898284a9aecb5f48bdc534e9c1273864a6.tar.gz
Fixed #29547 -- Added support for partial indexes.
Thanks to Ian Foote, Mariusz Felisiak, Simon Charettes, and Markus Holtermann for comments and feedback.
Diffstat (limited to 'tests/model_indexes')
-rw-r--r--tests/model_indexes/tests.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py
index 36c217982e..71825043f8 100644
--- a/tests/model_indexes/tests.py
+++ b/tests/model_indexes/tests.py
@@ -1,5 +1,6 @@
from django.conf import settings
from django.db import connection, models
+from django.db.models.query_utils import Q
from django.test import SimpleTestCase, skipUnlessDBFeature
from django.test.utils import isolate_apps
@@ -14,8 +15,10 @@ class IndexesTests(SimpleTestCase):
def test_repr(self):
index = models.Index(fields=['title'])
multi_col_index = models.Index(fields=['title', 'author'])
+ partial_index = models.Index(fields=['title'], name='long_books_idx', condition=Q(pages__gt=400))
self.assertEqual(repr(index), "<Index: fields='title'>")
self.assertEqual(repr(multi_col_index), "<Index: fields='title, author'>")
+ self.assertEqual(repr(partial_index), "<Index: fields='title', condition=(AND: ('pages__gt', 400))>")
def test_eq(self):
index = models.Index(fields=['title'])
@@ -52,6 +55,14 @@ class IndexesTests(SimpleTestCase):
with self.assertRaisesMessage(ValueError, msg):
models.Index(name='test_opclass', fields=['field', 'other'], opclasses=['jsonb_path_ops'])
+ def test_condition_requires_index_name(self):
+ with self.assertRaisesMessage(ValueError, 'An index must be named to use condition.'):
+ models.Index(condition=Q(pages__gt=400))
+
+ def test_condition_must_be_q(self):
+ with self.assertRaisesMessage(ValueError, 'Index.condition must be a Q instance.'):
+ models.Index(condition='invalid', name='long_book_idx')
+
def test_max_name_length(self):
msg = 'Index names cannot be longer than 30 characters.'
with self.assertRaisesMessage(ValueError, msg):
@@ -110,6 +121,25 @@ class IndexesTests(SimpleTestCase):
{'fields': ['title'], 'name': 'model_index_title_196f42_idx', 'db_tablespace': 'idx_tbls'}
)
+ def test_deconstruct_with_condition(self):
+ index = models.Index(
+ name='big_book_index',
+ fields=['title'],
+ condition=Q(pages__gt=400),
+ )
+ index.set_name_with_model(Book)
+ path, args, kwargs = index.deconstruct()
+ self.assertEqual(path, 'django.db.models.Index')
+ self.assertEqual(args, ())
+ self.assertEqual(
+ kwargs,
+ {
+ 'fields': ['title'],
+ 'name': 'model_index_title_196f42_idx',
+ 'condition': Q(pages__gt=400),
+ }
+ )
+
def test_clone(self):
index = models.Index(fields=['title'])
new_index = index.clone()