2019-09-03 10:09:31 +08:00
|
|
|
from unittest import mock
|
|
|
|
|
2017-06-28 03:15:15 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import connection, models
|
2019-10-11 02:04:17 +08:00
|
|
|
from django.db.models.functions import Lower, Upper
|
2022-06-13 11:48:43 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, override_settings, skipUnlessDBFeature
|
2017-12-06 04:05:10 +08:00
|
|
|
from django.test.utils import isolate_apps
|
2016-06-26 00:32:56 +08:00
|
|
|
|
2017-03-17 23:25:12 +08:00
|
|
|
from .models import Book, ChildModel1, ChildModel2
|
2016-06-26 00:32:56 +08:00
|
|
|
|
|
|
|
|
2019-01-13 05:14:54 +08:00
|
|
|
class SimpleIndexesTests(SimpleTestCase):
|
2017-02-16 02:33:55 +08:00
|
|
|
def test_suffix(self):
|
|
|
|
self.assertEqual(models.Index.suffix, "idx")
|
2016-06-26 00:32:56 +08:00
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
index = models.Index(fields=["title"])
|
2021-02-19 19:16:24 +08:00
|
|
|
named_index = models.Index(fields=["title"], name="title_idx")
|
2016-06-26 00:32:56 +08:00
|
|
|
multi_col_index = models.Index(fields=["title", "author"])
|
2019-08-20 15:54:41 +08:00
|
|
|
partial_index = models.Index(
|
|
|
|
fields=["title"], name="long_books_idx", condition=models.Q(pages__gt=400)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2019-10-31 20:33:53 +08:00
|
|
|
covering_index = models.Index(
|
|
|
|
fields=["title"],
|
|
|
|
name="include_idx",
|
|
|
|
include=["author", "pages"],
|
|
|
|
)
|
2020-06-17 16:32:43 +08:00
|
|
|
opclasses_index = models.Index(
|
|
|
|
fields=["headline", "body"],
|
|
|
|
name="opclasses_idx",
|
|
|
|
opclasses=["varchar_pattern_ops", "text_pattern_ops"],
|
|
|
|
)
|
2021-02-19 19:16:24 +08:00
|
|
|
func_index = models.Index(Lower("title"), "subtitle", name="book_func_idx")
|
|
|
|
tablespace_index = models.Index(
|
|
|
|
fields=["title"],
|
|
|
|
db_tablespace="idx_tbls",
|
|
|
|
name="book_tablespace_idx",
|
|
|
|
)
|
|
|
|
self.assertEqual(repr(index), "<Index: fields=['title']>")
|
|
|
|
self.assertEqual(
|
|
|
|
repr(named_index),
|
|
|
|
"<Index: fields=['title'] name='title_idx'>",
|
|
|
|
)
|
|
|
|
self.assertEqual(repr(multi_col_index), "<Index: fields=['title', 'author']>")
|
|
|
|
self.assertEqual(
|
|
|
|
repr(partial_index),
|
|
|
|
"<Index: fields=['title'] name='long_books_idx' "
|
|
|
|
"condition=(AND: ('pages__gt', 400))>",
|
|
|
|
)
|
2019-10-31 20:33:53 +08:00
|
|
|
self.assertEqual(
|
|
|
|
repr(covering_index),
|
2021-02-19 19:16:24 +08:00
|
|
|
"<Index: fields=['title'] name='include_idx' "
|
|
|
|
"include=('author', 'pages')>",
|
2020-06-17 16:32:43 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(opclasses_index),
|
2021-02-19 19:16:24 +08:00
|
|
|
"<Index: fields=['headline', 'body'] name='opclasses_idx' "
|
|
|
|
"opclasses=['varchar_pattern_ops', 'text_pattern_ops']>",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(func_index),
|
|
|
|
"<Index: expressions=(Lower(F(title)), F(subtitle)) "
|
|
|
|
"name='book_func_idx'>",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(tablespace_index),
|
|
|
|
"<Index: fields=['title'] name='book_tablespace_idx' "
|
|
|
|
"db_tablespace='idx_tbls'>",
|
2019-10-31 20:33:53 +08:00
|
|
|
)
|
2016-06-26 00:32:56 +08:00
|
|
|
|
|
|
|
def test_eq(self):
|
|
|
|
index = models.Index(fields=["title"])
|
|
|
|
same_index = models.Index(fields=["title"])
|
|
|
|
another_index = models.Index(fields=["title", "author"])
|
2016-06-20 23:50:05 +08:00
|
|
|
index.model = Book
|
|
|
|
same_index.model = Book
|
|
|
|
another_index.model = Book
|
2016-06-26 00:32:56 +08:00
|
|
|
self.assertEqual(index, same_index)
|
2019-09-03 10:09:31 +08:00
|
|
|
self.assertEqual(index, mock.ANY)
|
2016-06-26 00:32:56 +08:00
|
|
|
self.assertNotEqual(index, another_index)
|
|
|
|
|
2019-10-11 02:04:17 +08:00
|
|
|
def test_eq_func(self):
|
|
|
|
index = models.Index(Lower("title"), models.F("author"), name="book_func_idx")
|
|
|
|
same_index = models.Index(Lower("title"), "author", name="book_func_idx")
|
|
|
|
another_index = models.Index(Lower("title"), name="book_func_idx")
|
|
|
|
self.assertEqual(index, same_index)
|
|
|
|
self.assertEqual(index, mock.ANY)
|
|
|
|
self.assertNotEqual(index, another_index)
|
|
|
|
|
2016-08-14 08:00:51 +08:00
|
|
|
def test_index_fields_type(self):
|
2018-03-08 23:56:55 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "Index.fields must be a list or tuple."
|
|
|
|
):
|
2016-08-14 08:00:51 +08:00
|
|
|
models.Index(fields="title")
|
|
|
|
|
2021-01-12 20:05:01 +08:00
|
|
|
def test_index_fields_strings(self):
|
|
|
|
msg = "Index.fields must contain only strings with field names."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(fields=[models.F("title")])
|
|
|
|
|
2018-03-08 23:56:55 +08:00
|
|
|
def test_fields_tuple(self):
|
|
|
|
self.assertEqual(models.Index(fields=("title",)).fields, ["title"])
|
|
|
|
|
2019-10-11 02:04:17 +08:00
|
|
|
def test_requires_field_or_expression(self):
|
|
|
|
msg = "At least one field or expression is required to define an index."
|
2016-06-26 00:32:56 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index()
|
|
|
|
|
2019-10-11 02:04:17 +08:00
|
|
|
def test_expressions_and_fields_mutually_exclusive(self):
|
|
|
|
msg = "Index.fields and expressions are mutually exclusive."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(Upper("foo"), fields=["field"])
|
|
|
|
|
2017-10-12 01:25:52 +08:00
|
|
|
def test_opclasses_requires_index_name(self):
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "An index must be named to use opclasses."
|
|
|
|
):
|
|
|
|
models.Index(opclasses=["jsonb_path_ops"])
|
|
|
|
|
|
|
|
def test_opclasses_requires_list_or_tuple(self):
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "Index.opclasses must be a list or tuple."
|
|
|
|
):
|
|
|
|
models.Index(
|
|
|
|
name="test_opclass", fields=["field"], opclasses="jsonb_path_ops"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_opclasses_and_fields_same_length(self):
|
|
|
|
msg = "Index.fields and Index.opclasses must have the same number of elements."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(
|
|
|
|
name="test_opclass",
|
|
|
|
fields=["field", "other"],
|
|
|
|
opclasses=["jsonb_path_ops"],
|
|
|
|
)
|
|
|
|
|
2018-09-13 15:34:02 +08:00
|
|
|
def test_condition_requires_index_name(self):
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "An index must be named to use condition."
|
|
|
|
):
|
2019-08-20 15:54:41 +08:00
|
|
|
models.Index(condition=models.Q(pages__gt=400))
|
2018-09-13 15:34:02 +08:00
|
|
|
|
2019-10-11 02:04:17 +08:00
|
|
|
def test_expressions_requires_index_name(self):
|
|
|
|
msg = "An index must be named to use expressions."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(Lower("field"))
|
|
|
|
|
|
|
|
def test_expressions_with_opclasses(self):
|
|
|
|
msg = (
|
|
|
|
"Index.opclasses cannot be used with expressions. Use "
|
|
|
|
"django.contrib.postgres.indexes.OpClass() instead."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(
|
|
|
|
Lower("field"),
|
|
|
|
name="test_func_opclass",
|
|
|
|
opclasses=["jsonb_path_ops"],
|
|
|
|
)
|
|
|
|
|
2018-09-13 15:34:02 +08:00
|
|
|
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")
|
|
|
|
|
2019-10-31 20:33:53 +08:00
|
|
|
def test_include_requires_list_or_tuple(self):
|
|
|
|
msg = "Index.include must be a list or tuple."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(name="test_include", fields=["field"], include="other")
|
|
|
|
|
|
|
|
def test_include_requires_index_name(self):
|
|
|
|
msg = "A covering index must be named."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
models.Index(fields=["field"], include=["other"])
|
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
def test_name_auto_generation(self):
|
|
|
|
index = models.Index(fields=["author"])
|
2016-07-18 22:29:47 +08:00
|
|
|
index.set_name_with_model(Book)
|
2016-06-26 00:32:56 +08:00
|
|
|
self.assertEqual(index.name, "model_index_author_0f5565_idx")
|
|
|
|
|
2016-07-22 20:52:44 +08:00
|
|
|
# '-' for DESC columns should be accounted for in the index name.
|
|
|
|
index = models.Index(fields=["-author"])
|
|
|
|
index.set_name_with_model(Book)
|
|
|
|
self.assertEqual(index.name, "model_index_author_708765_idx")
|
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
# fields may be truncated in the name. db_column is used for naming.
|
|
|
|
long_field_index = models.Index(fields=["pages"])
|
2016-07-18 22:29:47 +08:00
|
|
|
long_field_index.set_name_with_model(Book)
|
2016-06-26 00:32:56 +08:00
|
|
|
self.assertEqual(long_field_index.name, "model_index_page_co_69235a_idx")
|
|
|
|
|
|
|
|
# suffix can't be longer than 3 characters.
|
|
|
|
long_field_index.suffix = "suff"
|
2021-03-24 13:45:08 +08:00
|
|
|
msg = (
|
|
|
|
"Index too long for multiple database support. Is self.suffix "
|
|
|
|
"longer than 3 characters?"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2016-07-18 22:29:47 +08:00
|
|
|
long_field_index.set_name_with_model(Book)
|
2016-06-26 00:32:56 +08:00
|
|
|
|
2017-12-06 04:05:10 +08:00
|
|
|
@isolate_apps("model_indexes")
|
|
|
|
def test_name_auto_generation_with_quoted_db_table(self):
|
|
|
|
class QuotedDbTable(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = '"t_quoted"'
|
|
|
|
|
|
|
|
index = models.Index(fields=["name"])
|
|
|
|
index.set_name_with_model(QuotedDbTable)
|
|
|
|
self.assertEqual(index.name, "t_quoted_name_e4ed1b_idx")
|
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
def test_deconstruction(self):
|
2017-06-28 03:15:15 +08:00
|
|
|
index = models.Index(fields=["title"], db_tablespace="idx_tbls")
|
2016-06-20 23:50:05 +08:00
|
|
|
index.set_name_with_model(Book)
|
2016-06-26 00:32:56 +08:00
|
|
|
path, args, kwargs = index.deconstruct()
|
|
|
|
self.assertEqual(path, "django.db.models.Index")
|
|
|
|
self.assertEqual(args, ())
|
2017-06-28 03:15:15 +08:00
|
|
|
self.assertEqual(
|
|
|
|
kwargs,
|
|
|
|
{
|
|
|
|
"fields": ["title"],
|
|
|
|
"name": "model_index_title_196f42_idx",
|
|
|
|
"db_tablespace": "idx_tbls",
|
2022-02-04 03:24:19 +08:00
|
|
|
},
|
2017-06-28 03:15:15 +08:00
|
|
|
)
|
2017-03-17 23:25:12 +08:00
|
|
|
|
2018-09-13 15:34:02 +08:00
|
|
|
def test_deconstruct_with_condition(self):
|
|
|
|
index = models.Index(
|
|
|
|
name="big_book_index",
|
|
|
|
fields=["title"],
|
2019-08-20 15:54:41 +08:00
|
|
|
condition=models.Q(pages__gt=400),
|
2018-09-13 15:34:02 +08:00
|
|
|
)
|
|
|
|
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",
|
2019-08-20 15:54:41 +08:00
|
|
|
"condition": models.Q(pages__gt=400),
|
2018-09-13 15:34:02 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-10-31 20:33:53 +08:00
|
|
|
def test_deconstruct_with_include(self):
|
|
|
|
index = models.Index(
|
|
|
|
name="book_include_idx",
|
|
|
|
fields=["title"],
|
|
|
|
include=["author"],
|
|
|
|
)
|
|
|
|
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",
|
|
|
|
"include": ("author",),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-10-11 02:04:17 +08:00
|
|
|
def test_deconstruct_with_expressions(self):
|
|
|
|
index = models.Index(Upper("title"), name="book_func_idx")
|
|
|
|
path, args, kwargs = index.deconstruct()
|
|
|
|
self.assertEqual(path, "django.db.models.Index")
|
|
|
|
self.assertEqual(args, (Upper("title"),))
|
|
|
|
self.assertEqual(kwargs, {"name": "book_func_idx"})
|
|
|
|
|
2017-03-17 23:25:12 +08:00
|
|
|
def test_clone(self):
|
|
|
|
index = models.Index(fields=["title"])
|
|
|
|
new_index = index.clone()
|
|
|
|
self.assertIsNot(index, new_index)
|
|
|
|
self.assertEqual(index.fields, new_index.fields)
|
|
|
|
|
2019-10-11 02:04:17 +08:00
|
|
|
def test_clone_with_expressions(self):
|
|
|
|
index = models.Index(Upper("title"), name="book_func_idx")
|
|
|
|
new_index = index.clone()
|
|
|
|
self.assertIsNot(index, new_index)
|
|
|
|
self.assertEqual(index.expressions, new_index.expressions)
|
|
|
|
|
2017-06-07 22:13:12 +08:00
|
|
|
def test_name_set(self):
|
|
|
|
index_names = [index.name for index in Book._meta.indexes]
|
2019-07-05 20:15:41 +08:00
|
|
|
self.assertCountEqual(
|
|
|
|
index_names,
|
|
|
|
[
|
|
|
|
"model_index_title_196f42_idx",
|
|
|
|
"model_index_isbn_34f975_idx",
|
|
|
|
"model_indexes_book_barcode_idx",
|
|
|
|
],
|
|
|
|
)
|
2017-06-07 22:13:12 +08:00
|
|
|
|
2017-03-17 23:25:12 +08:00
|
|
|
def test_abstract_children(self):
|
|
|
|
index_names = [index.name for index in ChildModel1._meta.indexes]
|
2019-07-05 20:15:41 +08:00
|
|
|
self.assertEqual(
|
|
|
|
index_names,
|
|
|
|
["model_index_name_440998_idx", "model_indexes_childmodel1_idx"],
|
|
|
|
)
|
2017-03-17 23:25:12 +08:00
|
|
|
index_names = [index.name for index in ChildModel2._meta.indexes]
|
2019-07-05 20:15:41 +08:00
|
|
|
self.assertEqual(
|
|
|
|
index_names,
|
|
|
|
["model_index_name_b6c374_idx", "model_indexes_childmodel2_idx"],
|
|
|
|
)
|
2017-06-28 03:15:15 +08:00
|
|
|
|
2019-01-13 05:14:54 +08:00
|
|
|
|
2022-06-13 11:48:43 +08:00
|
|
|
@override_settings(DEFAULT_TABLESPACE=None)
|
2019-01-13 05:14:54 +08:00
|
|
|
class IndexesTests(TestCase):
|
2017-06-28 03:15:15 +08:00
|
|
|
@skipUnlessDBFeature("supports_tablespaces")
|
|
|
|
def test_db_tablespace(self):
|
2019-01-13 03:26:24 +08:00
|
|
|
editor = connection.schema_editor()
|
|
|
|
# Index with db_tablespace attribute.
|
|
|
|
for fields in [
|
|
|
|
# Field with db_tablespace specified on model.
|
|
|
|
["shortcut"],
|
|
|
|
# Field without db_tablespace specified on model.
|
|
|
|
["author"],
|
|
|
|
# Multi-column with db_tablespaces specified on model.
|
|
|
|
["shortcut", "isbn"],
|
|
|
|
# Multi-column without db_tablespace specified on model.
|
|
|
|
["title", "author"],
|
|
|
|
]:
|
|
|
|
with self.subTest(fields=fields):
|
|
|
|
index = models.Index(fields=fields, db_tablespace="idx_tbls2")
|
|
|
|
self.assertIn(
|
|
|
|
'"idx_tbls2"', str(index.create_sql(Book, editor)).lower()
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2019-01-13 03:26:24 +08:00
|
|
|
# Indexes without db_tablespace attribute.
|
|
|
|
for fields in [["author"], ["shortcut", "isbn"], ["title", "author"]]:
|
|
|
|
with self.subTest(fields=fields):
|
|
|
|
index = models.Index(fields=fields)
|
|
|
|
# The DEFAULT_INDEX_TABLESPACE setting can't be tested because
|
|
|
|
# it's evaluated when the model class is defined. As a
|
|
|
|
# consequence, @override_settings doesn't work.
|
|
|
|
if settings.DEFAULT_INDEX_TABLESPACE:
|
|
|
|
self.assertIn(
|
|
|
|
'"%s"' % settings.DEFAULT_INDEX_TABLESPACE,
|
|
|
|
str(index.create_sql(Book, editor)).lower(),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertNotIn("TABLESPACE", str(index.create_sql(Book, editor)))
|
|
|
|
# Field with db_tablespace specified on the model and an index without
|
|
|
|
# db_tablespace.
|
|
|
|
index = models.Index(fields=["shortcut"])
|
|
|
|
self.assertIn('"idx_tbls"', str(index.create_sql(Book, editor)).lower())
|
2019-10-11 02:04:17 +08:00
|
|
|
|
|
|
|
@skipUnlessDBFeature("supports_tablespaces")
|
|
|
|
def test_func_with_tablespace(self):
|
|
|
|
# Functional index with db_tablespace attribute.
|
|
|
|
index = models.Index(
|
|
|
|
Lower("shortcut").desc(),
|
|
|
|
name="functional_tbls",
|
|
|
|
db_tablespace="idx_tbls2",
|
|
|
|
)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
sql = str(index.create_sql(Book, editor))
|
|
|
|
self.assertIn(editor.quote_name("idx_tbls2"), sql)
|
|
|
|
# Functional index without db_tablespace attribute.
|
|
|
|
index = models.Index(Lower("shortcut").desc(), name="functional_no_tbls")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
sql = str(index.create_sql(Book, editor))
|
|
|
|
# The DEFAULT_INDEX_TABLESPACE setting can't be tested because it's
|
|
|
|
# evaluated when the model class is defined. As a consequence,
|
|
|
|
# @override_settings doesn't work.
|
|
|
|
if settings.DEFAULT_INDEX_TABLESPACE:
|
|
|
|
self.assertIn(
|
|
|
|
editor.quote_name(settings.DEFAULT_INDEX_TABLESPACE),
|
|
|
|
sql,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertNotIn("TABLESPACE", sql)
|