summaryrefslogtreecommitdiff
path: root/tests/indexes
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-12-15 13:31:08 -0500
committerGitHub <noreply@github.com>2016-12-15 13:31:08 -0500
commitf94475e5262abe78d9771bc353f741b20cc3c725 (patch)
tree9b84f80b4bf86e32b31802719081040da00ce3a0 /tests/indexes
parentebb85d1e790bb697bff3e0a101c87c5f9c6dff7c (diff)
downloaddjango-f94475e5262abe78d9771bc353f741b20cc3c725.tar.gz
Refs #27558 -- Isolated indexes test on MySQL.
MySQL schema changes must be done in TransactionTestCase.
Diffstat (limited to 'tests/indexes')
-rw-r--r--tests/indexes/tests.py33
1 files changed, 22 insertions, 11 deletions
diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py
index 81e2278b19..b266d71e9a 100644
--- a/tests/indexes/tests.py
+++ b/tests/indexes/tests.py
@@ -3,7 +3,7 @@ from unittest import skipUnless
from django.db import connection
from django.db.models.deletion import CASCADE
from django.db.models.fields.related import ForeignKey
-from django.test import TestCase
+from django.test import TestCase, TransactionTestCase
from .models import Article, ArticleTranslation, IndexTogetherSingleList
@@ -84,7 +84,11 @@ class SchemaIndexesTests(TestCase):
index_sql = connection.schema_editor()._model_indexes_sql(Article)
self.assertEqual(len(index_sql), 1)
- @skipUnless(connection.vendor == 'mysql', "This is a mysql-specific issue")
+
+@skipUnless(connection.vendor == 'mysql', 'MySQL tests')
+class SchemaIndexesMySQLTests(TransactionTestCase):
+ available_apps = ['indexes']
+
def test_no_index_for_foreignkey(self):
"""
MySQL on InnoDB already creates indexes automatically for foreign keys.
@@ -103,12 +107,19 @@ class SchemaIndexesTests(TestCase):
# The index also shouldn't be created if the ForeignKey is added after
# the model was created.
- with connection.schema_editor() as editor:
- new_field = ForeignKey(Article, CASCADE)
- new_field.set_attributes_from_name('new_foreign_key')
- editor.add_field(ArticleTranslation, new_field)
- self.assertEqual(editor.deferred_sql, [
- 'ALTER TABLE `indexes_articletranslation` '
- 'ADD CONSTRAINT `indexes_articletrans_new_foreign_key_id_d27a9146_fk_indexes_a` '
- 'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)'
- ])
+ field_created = False
+ try:
+ with connection.schema_editor() as editor:
+ new_field = ForeignKey(Article, CASCADE)
+ new_field.set_attributes_from_name('new_foreign_key')
+ editor.add_field(ArticleTranslation, new_field)
+ field_created = True
+ self.assertEqual(editor.deferred_sql, [
+ 'ALTER TABLE `indexes_articletranslation` '
+ 'ADD CONSTRAINT `indexes_articletrans_new_foreign_key_id_d27a9146_fk_indexes_a` '
+ 'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)'
+ ])
+ finally:
+ if field_created:
+ with connection.schema_editor() as editor:
+ editor.remove_field(ArticleTranslation, new_field)