diff --git a/tests/modeltests/tablespaces/tests.py b/tests/modeltests/tablespaces/tests.py index e8c16717173..17fdb8dd741 100644 --- a/tests/modeltests/tablespaces/tests.py +++ b/tests/modeltests/tablespaces/tests.py @@ -1,5 +1,6 @@ import copy +from django.conf import settings from django.db import connection from django.db import models from django.db.models.loading import cache @@ -10,7 +11,7 @@ from models import Article, ArticleRef, Authors, Reviewers, Scientist, Scientist # We can't test the DEFAULT_TABLESPACE and DEFAULT_INDEX_TABLESPACE settings # because they're evaluated when the model class is defined. As a consequence, -# @override_settings doesn't work. +# @override_settings doesn't work, and the tests depend def sql_for_table(model): return '\n'.join(connection.creation.sql_create_model(model, no_style())[0]) @@ -45,8 +46,15 @@ class TablespacesTests(TestCase): @skipUnlessDBFeature('supports_tablespaces') def test_tablespace_for_model(self): - # 1 for the table + 1 for the index on the primary key - self.assertNumContains(sql_for_table(Scientist).lower(), 'tbl_tbsp', 2) + sql = sql_for_table(Scientist).lower() + if settings.DEFAULT_INDEX_TABLESPACE: + # 1 for the table + self.assertNumContains(sql, 'tbl_tbsp', 1) + # 1 for the index on the primary key + self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 1) + else: + # 1 for the table + 1 for the index on the primary key + self.assertNumContains(sql, 'tbl_tbsp', 2) @skipIfDBFeature('supports_tablespaces') def test_tablespace_ignored_for_model(self): @@ -56,10 +64,18 @@ class TablespacesTests(TestCase): @skipUnlessDBFeature('supports_tablespaces') def test_tablespace_for_indexed_field(self): - # 1 for the table + 1 for the primary key + 1 for the index on name - self.assertNumContains(sql_for_table(Article).lower(), 'tbl_tbsp', 3) + sql = sql_for_table(Article).lower() + if settings.DEFAULT_INDEX_TABLESPACE: + # 1 for the table + self.assertNumContains(sql, 'tbl_tbsp', 1) + # 1 for the primary key + 1 for the index on code + self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 2) + else: + # 1 for the table + 1 for the primary key + 1 for the index on code + self.assertNumContains(sql, 'tbl_tbsp', 3) + # 1 for the index on reference - self.assertNumContains(sql_for_table(Article).lower(), 'idx_tbsp', 1) + self.assertNumContains(sql, 'idx_tbsp', 1) @skipIfDBFeature('supports_tablespaces') def test_tablespace_ignored_for_indexed_field(self): @@ -69,20 +85,42 @@ class TablespacesTests(TestCase): @skipUnlessDBFeature('supports_tablespaces') def test_tablespace_for_many_to_many_field(self): - # The join table of the ManyToManyField always goes to the tablespace - # of the model. - self.assertNumContains(sql_for_table(Authors).lower(), 'tbl_tbsp', 2) - self.assertNumContains(sql_for_table(Authors).lower(), 'idx_tbsp', 0) - # The ManyToManyField declares no db_tablespace, indexes for the two - # foreign keys in the join table go to the tablespace of the model. - self.assertNumContains(sql_for_index(Authors).lower(), 'tbl_tbsp', 2) - self.assertNumContains(sql_for_index(Authors).lower(), 'idx_tbsp', 0) + sql = sql_for_table(Authors).lower() + # The join table of the ManyToManyField goes to the model's tablespace, + # and its indexes too, unless DEFAULT_INDEX_TABLESPACE is set. + if settings.DEFAULT_INDEX_TABLESPACE: + # 1 for the table + self.assertNumContains(sql, 'tbl_tbsp', 1) + # 1 for the primary key + self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 1) + else: + # 1 for the table + 1 for the index on the primary key + self.assertNumContains(sql, 'tbl_tbsp', 2) + self.assertNumContains(sql, 'idx_tbsp', 0) - # The join table of the ManyToManyField always goes to the tablespace - # of the model. - self.assertNumContains(sql_for_table(Reviewers).lower(), 'tbl_tbsp', 2) - self.assertNumContains(sql_for_table(Reviewers).lower(), 'idx_tbsp', 0) - # The ManyToManyField declares db_tablespace, indexes for the two - # foreign keys in the join table go to this tablespace. - self.assertNumContains(sql_for_index(Reviewers).lower(), 'tbl_tbsp', 0) - self.assertNumContains(sql_for_index(Reviewers).lower(), 'idx_tbsp', 2) + sql = sql_for_index(Authors).lower() + # The ManyToManyField declares no db_tablespace, its indexes go to + # the model's tablespace, unless DEFAULT_INDEX_TABLESPACE is set. + if settings.DEFAULT_INDEX_TABLESPACE: + self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 2) + else: + self.assertNumContains(sql, 'tbl_tbsp', 2) + self.assertNumContains(sql, 'idx_tbsp', 0) + + sql = sql_for_table(Reviewers).lower() + # The join table of the ManyToManyField goes to the model's tablespace, + # and its indexes too, unless DEFAULT_INDEX_TABLESPACE is set. + if settings.DEFAULT_INDEX_TABLESPACE: + # 1 for the table + self.assertNumContains(sql, 'tbl_tbsp', 1) + # 1 for the primary key + self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 1) + else: + # 1 for the table + 1 for the index on the primary key + self.assertNumContains(sql, 'tbl_tbsp', 2) + self.assertNumContains(sql, 'idx_tbsp', 0) + + sql = sql_for_index(Reviewers).lower() + # The ManyToManyField declares db_tablespace, its indexes go there. + self.assertNumContains(sql, 'tbl_tbsp', 0) + self.assertNumContains(sql, 'idx_tbsp', 2)