Refs #27236 -- Moved models with Meta.index_together inside of test methods.

This commit is contained in:
David Wobrock 2022-06-02 21:03:00 +02:00 committed by Mariusz Felisiak
parent 61badf1d58
commit a3a1290d47
4 changed files with 40 additions and 40 deletions

View File

@ -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)

View File

@ -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
)

View File

@ -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)

View File

@ -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