diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/model_indexes | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/model_indexes')
-rw-r--r-- | tests/model_indexes/models.py | 18 | ||||
-rw-r--r-- | tests/model_indexes/tests.py | 238 |
2 files changed, 140 insertions, 116 deletions
diff --git a/tests/model_indexes/models.py b/tests/model_indexes/models.py index 42651cd868..3bda606904 100644 --- a/tests/model_indexes/models.py +++ b/tests/model_indexes/models.py @@ -4,16 +4,18 @@ from django.db import models class Book(models.Model): title = models.CharField(max_length=50) author = models.CharField(max_length=50) - pages = models.IntegerField(db_column='page_count') - shortcut = models.CharField(max_length=50, db_tablespace='idx_tbls') - isbn = models.CharField(max_length=50, db_tablespace='idx_tbls') + pages = models.IntegerField(db_column="page_count") + shortcut = models.CharField(max_length=50, db_tablespace="idx_tbls") + isbn = models.CharField(max_length=50, db_tablespace="idx_tbls") barcode = models.CharField(max_length=31) class Meta: indexes = [ - models.Index(fields=['title']), - models.Index(fields=['isbn', 'id']), - models.Index(fields=['barcode'], name='%(app_label)s_%(class)s_barcode_idx'), + models.Index(fields=["title"]), + models.Index(fields=["isbn", "id"]), + models.Index( + fields=["barcode"], name="%(app_label)s_%(class)s_barcode_idx" + ), ] @@ -24,8 +26,8 @@ class AbstractModel(models.Model): class Meta: abstract = True indexes = [ - models.Index(fields=['name']), - models.Index(fields=['shortcut'], name='%(app_label)s_%(class)s_idx'), + models.Index(fields=["name"]), + models.Index(fields=["shortcut"], name="%(app_label)s_%(class)s_idx"), ] diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py index 95dec7ef48..6f40011a5b 100644 --- a/tests/model_indexes/tests.py +++ b/tests/model_indexes/tests.py @@ -10,30 +10,31 @@ from .models import Book, ChildModel1, ChildModel2 class SimpleIndexesTests(SimpleTestCase): - def test_suffix(self): - self.assertEqual(models.Index.suffix, 'idx') + self.assertEqual(models.Index.suffix, "idx") def test_repr(self): - index = models.Index(fields=['title']) - named_index = models.Index(fields=['title'], name='title_idx') - multi_col_index = models.Index(fields=['title', 'author']) - partial_index = models.Index(fields=['title'], name='long_books_idx', condition=models.Q(pages__gt=400)) + index = models.Index(fields=["title"]) + named_index = models.Index(fields=["title"], name="title_idx") + multi_col_index = models.Index(fields=["title", "author"]) + partial_index = models.Index( + fields=["title"], name="long_books_idx", condition=models.Q(pages__gt=400) + ) covering_index = models.Index( - fields=['title'], - name='include_idx', - include=['author', 'pages'], + fields=["title"], + name="include_idx", + include=["author", "pages"], ) opclasses_index = models.Index( - fields=['headline', 'body'], - name='opclasses_idx', - opclasses=['varchar_pattern_ops', 'text_pattern_ops'], + fields=["headline", "body"], + name="opclasses_idx", + opclasses=["varchar_pattern_ops", "text_pattern_ops"], ) - func_index = models.Index(Lower('title'), 'subtitle', name='book_func_idx') + 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', + fields=["title"], + db_tablespace="idx_tbls", + name="book_tablespace_idx", ) self.assertEqual(repr(index), "<Index: fields=['title']>") self.assertEqual( @@ -68,9 +69,9 @@ class SimpleIndexesTests(SimpleTestCase): ) def test_eq(self): - index = models.Index(fields=['title']) - same_index = models.Index(fields=['title']) - another_index = models.Index(fields=['title', 'author']) + index = models.Index(fields=["title"]) + same_index = models.Index(fields=["title"]) + another_index = models.Index(fields=["title", "author"]) index.model = Book same_index.model = Book another_index.model = Book @@ -79,108 +80,124 @@ class SimpleIndexesTests(SimpleTestCase): self.assertNotEqual(index, another_index) 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') + 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) def test_index_fields_type(self): - with self.assertRaisesMessage(ValueError, 'Index.fields must be a list or tuple.'): - models.Index(fields='title') + with self.assertRaisesMessage( + ValueError, "Index.fields must be a list or tuple." + ): + models.Index(fields="title") def test_index_fields_strings(self): - msg = 'Index.fields must contain only strings with field names.' + msg = "Index.fields must contain only strings with field names." with self.assertRaisesMessage(ValueError, msg): - models.Index(fields=[models.F('title')]) + models.Index(fields=[models.F("title")]) def test_fields_tuple(self): - self.assertEqual(models.Index(fields=('title',)).fields, ['title']) + self.assertEqual(models.Index(fields=("title",)).fields, ["title"]) def test_requires_field_or_expression(self): - msg = 'At least one field or expression is required to define an index.' + msg = "At least one field or expression is required to define an index." with self.assertRaisesMessage(ValueError, msg): models.Index() 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']) + models.Index(Upper("foo"), fields=["field"]) 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']) + 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') + 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.' + 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']) + 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.'): + with self.assertRaisesMessage( + ValueError, "An index must be named to use condition." + ): models.Index(condition=models.Q(pages__gt=400)) def test_expressions_requires_index_name(self): - msg = 'An index must be named to use expressions.' + msg = "An index must be named to use expressions." with self.assertRaisesMessage(ValueError, msg): - models.Index(Lower('field')) + 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.' + "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'], + Lower("field"), + name="test_func_opclass", + opclasses=["jsonb_path_ops"], ) 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') + with self.assertRaisesMessage( + ValueError, "Index.condition must be a Q instance." + ): + models.Index(condition="invalid", name="long_book_idx") def test_include_requires_list_or_tuple(self): - msg = 'Index.include must be a list or tuple.' + msg = "Index.include must be a list or tuple." with self.assertRaisesMessage(ValueError, msg): - models.Index(name='test_include', fields=['field'], include='other') + models.Index(name="test_include", fields=["field"], include="other") def test_include_requires_index_name(self): - msg = 'A covering index must be named.' + msg = "A covering index must be named." with self.assertRaisesMessage(ValueError, msg): - models.Index(fields=['field'], include=['other']) + models.Index(fields=["field"], include=["other"]) def test_name_auto_generation(self): - index = models.Index(fields=['author']) + index = models.Index(fields=["author"]) index.set_name_with_model(Book) - self.assertEqual(index.name, 'model_index_author_0f5565_idx') + self.assertEqual(index.name, "model_index_author_0f5565_idx") # '-' for DESC columns should be accounted for in the index name. - index = models.Index(fields=['-author']) + index = models.Index(fields=["-author"]) index.set_name_with_model(Book) - self.assertEqual(index.name, 'model_index_author_708765_idx') + self.assertEqual(index.name, "model_index_author_708765_idx") # fields may be truncated in the name. db_column is used for naming. - long_field_index = models.Index(fields=['pages']) + long_field_index = models.Index(fields=["pages"]) long_field_index.set_name_with_model(Book) - self.assertEqual(long_field_index.name, 'model_index_page_co_69235a_idx') + 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' + long_field_index.suffix = "suff" msg = ( - 'Index too long for multiple database support. Is self.suffix ' - 'longer than 3 characters?' + "Index too long for multiple database support. Is self.suffix " + "longer than 3 characters?" ) with self.assertRaisesMessage(ValueError, msg): long_field_index.set_name_with_model(Book) - @isolate_apps('model_indexes') + @isolate_apps("model_indexes") def test_name_auto_generation_with_quoted_db_table(self): class QuotedDbTable(models.Model): name = models.CharField(max_length=50) @@ -188,74 +205,78 @@ class SimpleIndexesTests(SimpleTestCase): class Meta: db_table = '"t_quoted"' - index = models.Index(fields=['name']) + index = models.Index(fields=["name"]) index.set_name_with_model(QuotedDbTable) - self.assertEqual(index.name, 't_quoted_name_e4ed1b_idx') + self.assertEqual(index.name, "t_quoted_name_e4ed1b_idx") def test_deconstruction(self): - index = models.Index(fields=['title'], db_tablespace='idx_tbls') + index = models.Index(fields=["title"], db_tablespace="idx_tbls") index.set_name_with_model(Book) path, args, kwargs = index.deconstruct() - self.assertEqual(path, 'django.db.models.Index') + self.assertEqual(path, "django.db.models.Index") self.assertEqual(args, ()) self.assertEqual( kwargs, - {'fields': ['title'], 'name': 'model_index_title_196f42_idx', 'db_tablespace': 'idx_tbls'} + { + "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'], + name="big_book_index", + fields=["title"], condition=models.Q(pages__gt=400), ) index.set_name_with_model(Book) path, args, kwargs = index.deconstruct() - self.assertEqual(path, 'django.db.models.Index') + self.assertEqual(path, "django.db.models.Index") self.assertEqual(args, ()) self.assertEqual( kwargs, { - 'fields': ['title'], - 'name': 'model_index_title_196f42_idx', - 'condition': models.Q(pages__gt=400), - } + "fields": ["title"], + "name": "model_index_title_196f42_idx", + "condition": models.Q(pages__gt=400), + }, ) def test_deconstruct_with_include(self): index = models.Index( - name='book_include_idx', - fields=['title'], - include=['author'], + 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(path, "django.db.models.Index") self.assertEqual(args, ()) self.assertEqual( kwargs, { - 'fields': ['title'], - 'name': 'model_index_title_196f42_idx', - 'include': ('author',), + "fields": ["title"], + "name": "model_index_title_196f42_idx", + "include": ("author",), }, ) def test_deconstruct_with_expressions(self): - index = models.Index(Upper('title'), name='book_func_idx') + 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'}) + self.assertEqual(path, "django.db.models.Index") + self.assertEqual(args, (Upper("title"),)) + self.assertEqual(kwargs, {"name": "book_func_idx"}) def test_clone(self): - index = models.Index(fields=['title']) + index = models.Index(fields=["title"]) new_index = index.clone() self.assertIsNot(index, new_index) self.assertEqual(index.fields, new_index.fields) def test_clone_with_expressions(self): - index = models.Index(Upper('title'), name='book_func_idx') + 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) @@ -265,9 +286,9 @@ class SimpleIndexesTests(SimpleTestCase): self.assertCountEqual( index_names, [ - 'model_index_title_196f42_idx', - 'model_index_isbn_34f975_idx', - 'model_indexes_book_barcode_idx', + "model_index_title_196f42_idx", + "model_index_isbn_34f975_idx", + "model_indexes_book_barcode_idx", ], ) @@ -275,36 +296,37 @@ class SimpleIndexesTests(SimpleTestCase): index_names = [index.name for index in ChildModel1._meta.indexes] self.assertEqual( index_names, - ['model_index_name_440998_idx', 'model_indexes_childmodel1_idx'], + ["model_index_name_440998_idx", "model_indexes_childmodel1_idx"], ) index_names = [index.name for index in ChildModel2._meta.indexes] self.assertEqual( index_names, - ['model_index_name_b6c374_idx', 'model_indexes_childmodel2_idx'], + ["model_index_name_b6c374_idx", "model_indexes_childmodel2_idx"], ) class IndexesTests(TestCase): - - @skipUnlessDBFeature('supports_tablespaces') + @skipUnlessDBFeature("supports_tablespaces") def test_db_tablespace(self): editor = connection.schema_editor() # Index with db_tablespace attribute. for fields in [ # Field with db_tablespace specified on model. - ['shortcut'], + ["shortcut"], # Field without db_tablespace specified on model. - ['author'], + ["author"], # Multi-column with db_tablespaces specified on model. - ['shortcut', 'isbn'], + ["shortcut", "isbn"], # Multi-column without db_tablespace specified on model. - ['title', 'author'], + ["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()) + index = models.Index(fields=fields, db_tablespace="idx_tbls2") + self.assertIn( + '"idx_tbls2"', str(index.create_sql(Book, editor)).lower() + ) # Indexes without db_tablespace attribute. - for fields in [['author'], ['shortcut', 'isbn'], ['title', 'author']]: + 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 @@ -313,28 +335,28 @@ class IndexesTests(TestCase): if settings.DEFAULT_INDEX_TABLESPACE: self.assertIn( '"%s"' % settings.DEFAULT_INDEX_TABLESPACE, - str(index.create_sql(Book, editor)).lower() + str(index.create_sql(Book, editor)).lower(), ) else: - self.assertNotIn('TABLESPACE', str(index.create_sql(Book, editor))) + 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']) + index = models.Index(fields=["shortcut"]) self.assertIn('"idx_tbls"', str(index.create_sql(Book, editor)).lower()) - @skipUnlessDBFeature('supports_tablespaces') + @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', + 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) + self.assertIn(editor.quote_name("idx_tbls2"), sql) # Functional index without db_tablespace attribute. - index = models.Index(Lower('shortcut').desc(), name='functional_no_tbls') + 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 @@ -346,4 +368,4 @@ class IndexesTests(TestCase): sql, ) else: - self.assertNotIn('TABLESPACE', sql) + self.assertNotIn("TABLESPACE", sql) |