2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2012-05-12 17:58:32 +00:00
|
|
|
|
2013-12-24 11:25:17 +00:00
|
|
|
from django.apps import apps
|
2011-10-15 15:54:53 +00:00
|
|
|
from django.conf import settings
|
2011-10-14 21:49:43 +00:00
|
|
|
from django.db import connection
|
2013-10-10 20:42:30 +00:00
|
|
|
from django.core.management.color import no_style
|
2011-10-14 21:49:43 +00:00
|
|
|
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
|
|
|
|
2014-06-10 19:35:48 +00:00
|
|
|
from .models.tablespaces import (Article, ArticleRef, Authors, Reviewers,
|
|
|
|
Scientist, ScientistRef)
|
2011-10-14 21:49:43 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-10-14 21:49:43 +00:00
|
|
|
def sql_for_table(model):
|
2014-06-10 19:35:48 +00:00
|
|
|
return '\n'.join(connection.creation.sql_create_model(model,
|
|
|
|
no_style())[0])
|
2011-10-14 21:49:43 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-10-14 21:49:43 +00:00
|
|
|
def sql_for_index(model):
|
2014-06-10 19:35:48 +00:00
|
|
|
return '\n'.join(connection.creation.sql_indexes_for_model(model,
|
|
|
|
no_style()))
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
|
2014-06-10 19:35:48 +00:00
|
|
|
# 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, and the tests depend
|
2011-10-14 21:49:43 +00:00
|
|
|
class TablespacesTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# The unmanaged models need to be removed after the test in order to
|
2011-10-15 09:00:31 +00:00
|
|
|
# prevent bad interactions with the flush operation in other tests.
|
2014-06-10 19:35:48 +00:00
|
|
|
self._old_models = apps.app_configs['model_options'].models.copy()
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
for model in Article, Authors, Reviewers, Scientist:
|
|
|
|
model._meta.managed = True
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for model in Article, Authors, Reviewers, Scientist:
|
|
|
|
model._meta.managed = False
|
|
|
|
|
2014-06-10 19:35:48 +00:00
|
|
|
apps.app_configs['model_options'].models = self._old_models
|
|
|
|
apps.all_models['model_options'] = self._old_models
|
2013-12-29 20:24:40 +00:00
|
|
|
apps.clear_cache()
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
def assertNumContains(self, haystack, needle, count):
|
|
|
|
real_count = haystack.count(needle)
|
|
|
|
self.assertEqual(real_count, count, "Found %d instances of '%s', "
|
|
|
|
"expected %d" % (real_count, needle, count))
|
|
|
|
|
|
|
|
@skipUnlessDBFeature('supports_tablespaces')
|
|
|
|
def test_tablespace_for_model(self):
|
2011-10-15 15:54:53 +00:00
|
|
|
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)
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
@skipIfDBFeature('supports_tablespaces')
|
|
|
|
def test_tablespace_ignored_for_model(self):
|
|
|
|
# No tablespace-related SQL
|
|
|
|
self.assertEqual(sql_for_table(Scientist),
|
2011-10-15 09:00:31 +00:00
|
|
|
sql_for_table(ScientistRef))
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
@skipUnlessDBFeature('supports_tablespaces')
|
|
|
|
def test_tablespace_for_indexed_field(self):
|
2011-10-15 15:54:53 +00:00
|
|
|
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)
|
|
|
|
|
2011-10-14 21:49:43 +00:00
|
|
|
# 1 for the index on reference
|
2011-10-15 15:54:53 +00:00
|
|
|
self.assertNumContains(sql, 'idx_tbsp', 1)
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
@skipIfDBFeature('supports_tablespaces')
|
|
|
|
def test_tablespace_ignored_for_indexed_field(self):
|
|
|
|
# No tablespace-related SQL
|
|
|
|
self.assertEqual(sql_for_table(Article),
|
2011-10-15 09:00:31 +00:00
|
|
|
sql_for_table(ArticleRef))
|
2011-10-14 21:49:43 +00:00
|
|
|
|
|
|
|
@skipUnlessDBFeature('supports_tablespaces')
|
|
|
|
def test_tablespace_for_many_to_many_field(self):
|
2011-10-15 15:54:53 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|