From a3a1290d47326c3f87824b3cf7ca969cb0d364aa Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Thu, 2 Jun 2022 21:03:00 +0200 Subject: [PATCH] Refs #27236 -- Moved models with Meta.index_together inside of test methods. --- tests/indexes/models.py | 9 --------- tests/indexes/tests.py | 28 +++++++++++++++++++--------- tests/schema/models.py | 18 ------------------ tests/schema/tests.py | 25 +++++++++++++++++++++---- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/tests/indexes/models.py b/tests/indexes/models.py index 241556ca69..f7fcb6175c 100644 --- a/tests/indexes/models.py +++ b/tests/indexes/models.py @@ -43,15 +43,6 @@ class Article(models.Model): ] -# Model for index_together being used only with single list -class IndexTogetherSingleList(models.Model): - headline = models.CharField(max_length=100) - pub_date = models.DateTimeField() - - class Meta: - index_together = ["headline", "pub_date"] - - class IndexedArticle(models.Model): headline = models.CharField(max_length=100, db_index=True) body = models.TextField(db_index=True) diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index a752c3ac54..11f72850f3 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -3,7 +3,15 @@ from unittest import skipUnless from django.conf import settings from django.db import connection -from django.db.models import CASCADE, ForeignKey, Index, Q +from django.db.models import ( + CASCADE, + CharField, + DateTimeField, + ForeignKey, + Index, + Model, + Q, +) from django.db.models.functions import Lower from django.test import ( TestCase, @@ -11,15 +19,10 @@ from django.test import ( skipIfDBFeature, skipUnlessDBFeature, ) -from django.test.utils import override_settings +from django.test.utils import isolate_apps, override_settings from django.utils import timezone -from .models import ( - Article, - ArticleTranslation, - IndexedArticle2, - IndexTogetherSingleList, -) +from .models import Article, ArticleTranslation, IndexedArticle2 class SchemaIndexesTests(TestCase): @@ -79,8 +82,15 @@ class SchemaIndexesTests(TestCase): index_sql[0], ) + @isolate_apps("indexes") def test_index_together_single_list(self): - # Test for using index_together with a single list (#22172) + class IndexTogetherSingleList(Model): + headline = CharField(max_length=100) + pub_date = DateTimeField() + + class Meta: + index_together = ["headline", "pub_date"] + index_sql = connection.schema_editor()._model_indexes_sql( IndexTogetherSingleList ) diff --git a/tests/schema/models.py b/tests/schema/models.py index 55f7d2c13c..75e32a0eab 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -62,15 +62,6 @@ class AuthorWithUniqueName(models.Model): apps = new_apps -class AuthorWithIndexedNameAndBirthday(models.Model): - name = models.CharField(max_length=255) - birthday = models.DateField() - - class Meta: - apps = new_apps - index_together = [["name", "birthday"]] - - class AuthorWithUniqueNameAndBirthday(models.Model): name = models.CharField(max_length=255) birthday = models.DateField() @@ -180,15 +171,6 @@ class Tag(models.Model): apps = new_apps -class TagIndexed(models.Model): - title = models.CharField(max_length=255) - slug = models.SlugField(unique=True) - - class Meta: - apps = new_apps - index_together = [["slug", "title"]] - - class TagM2MTest(models.Model): title = models.CharField(max_length=255) slug = models.SlugField(unique=True) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index fe717f8841..7651b691b9 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -64,7 +64,6 @@ from .models import ( AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName, - AuthorWithIndexedNameAndBirthday, AuthorWithUniqueName, AuthorWithUniqueNameAndBirthday, Book, @@ -79,7 +78,6 @@ from .models import ( Note, NoteRename, Tag, - TagIndexed, TagM2MTest, TagUniqueRename, Thing, @@ -114,7 +112,6 @@ class SchemaTests(TransactionTestCase): Node, Note, Tag, - TagIndexed, TagM2MTest, TagUniqueRename, Thing, @@ -2952,13 +2949,24 @@ class SchemaTests(TransactionTestCase): with connection.schema_editor() as editor: editor.alter_index_together(Book, [["author", "title"]], []) + @isolate_apps("schema") def test_create_index_together(self): """ Tests creating models with index_together already defined """ + + class TagIndexed(Model): + title = CharField(max_length=255) + slug = SlugField(unique=True) + + class Meta: + app_label = "schema" + index_together = [["slug", "title"]] + # Create the table with connection.schema_editor() as editor: editor.create_model(TagIndexed) + self.isolated_local_models = [TagIndexed] # Ensure there is an index self.assertIs( any( @@ -2970,10 +2978,19 @@ class SchemaTests(TransactionTestCase): ) @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields") + @isolate_apps("schema") def test_remove_index_together_does_not_remove_meta_indexes(self): + class AuthorWithIndexedNameAndBirthday(Model): + name = CharField(max_length=255) + birthday = DateField() + + class Meta: + app_label = "schema" + index_together = [["name", "birthday"]] + with connection.schema_editor() as editor: editor.create_model(AuthorWithIndexedNameAndBirthday) - self.local_models = [AuthorWithIndexedNameAndBirthday] + self.isolated_local_models = [AuthorWithIndexedNameAndBirthday] # Add the custom index index = Index(fields=["name", "birthday"], name="author_name_birthday_idx") custom_index_name = index.name