we can t test the defaulttablespace and defaultindextablespace settings because they re evaluated when the model class is defined as a consequence overridesettings doesn t work and the tests depend the unmanaged models need to be removed after the test in order to prevent bad interactions with the flush operation in other tests 1 for the table 1 for the index on the primary key 1 for the table 1 for the index on the primary key no tablespacerelated sql 1 for the table 1 for the primary key 1 for the index on code 1 for the table 1 for the primary key 1 for the index on code 1 for the index on reference no tablespacerelated sql the join table of the manytomanyfield goes to the model s tablespace and its indexes too unless defaultindextablespace is set 1 for the table 1 for the primary key 1 for the table 1 for the index on the primary key the manytomanyfield declares no dbtablespace its indexes go to the model s tablespace unless defaultindextablespace is set the join table of the manytomanyfield goes to the model s tablespace and its indexes too unless defaultindextablespace is set 1 for the table 1 for the primary key 1 for the table 1 for the index on the primary key the manytomanyfield declares dbtablespace its indexes go there 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 the unmanaged models need to be removed after the test in order to prevent bad interactions with the flush operation in other tests 1 for the table 1 for the index on the primary key 1 for the table 1 for the index on the primary key no tablespace related sql 1 for the table 1 for the primary key 1 for the index on code 1 for the table 1 for the primary key 1 for the index on code 1 for the index on reference no tablespace related sql the join table of the manytomanyfield goes to the model s tablespace and its indexes too unless default_index_tablespace is set 1 for the table 1 for the primary key 1 for the table 1 for the index on the primary key the manytomanyfield declares no db_tablespace its indexes go to the model s tablespace unless default_index_tablespace is set the join table of the manytomanyfield goes to the model s tablespace and its indexes too unless default_index_tablespace is set 1 for the table 1 for the primary key 1 for the table 1 for the index on the primary key the manytomanyfield declares db_tablespace its indexes go there
from django.apps import apps
from django.conf import settings
from django.db import connection
from django.test import TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature

from .models.tablespaces import (
    Article,
    ArticleRef,
    Authors,
    Reviewers,
    Scientist,
    ScientistRef,
)


def sql_for_table(model):
    with connection.schema_editor(collect_sql=True) as editor:
        editor.create_model(model)
    return editor.collected_sql[0]


def sql_for_index(model):
    return "\n".join(
        str(sql) for sql in connection.schema_editor()._model_indexes_sql(model)
    )





class TablespacesTests(TransactionTestCase):
    available_apps = ["model_options"]

    def setUp(self):
        
        
        self._old_models = apps.app_configs["model_options"].models.copy()

        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

        apps.app_configs["model_options"].models = self._old_models
        apps.all_models["model_options"] = self._old_models
        apps.clear_cache()

    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):
        sql = sql_for_table(Scientist).lower()
        if settings.DEFAULT_INDEX_TABLESPACE:
            
            self.assertNumContains(sql, "tbl_tbsp", 1)
            
            self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 1)
        else:
            
            self.assertNumContains(sql, "tbl_tbsp", 2)

    @skipIfDBFeature("supports_tablespaces")
    def test_tablespace_ignored_for_model(self):
        
        self.assertEqual(sql_for_table(Scientist), sql_for_table(ScientistRef))

    @skipUnlessDBFeature("supports_tablespaces")
    def test_tablespace_for_indexed_field(self):
        sql = sql_for_table(Article).lower()
        if settings.DEFAULT_INDEX_TABLESPACE:
            
            self.assertNumContains(sql, "tbl_tbsp", 1)
            
            self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 2)
        else:
            
            self.assertNumContains(sql, "tbl_tbsp", 3)

        
        self.assertNumContains(sql, "idx_tbsp", 1)

    @skipIfDBFeature("supports_tablespaces")
    def test_tablespace_ignored_for_indexed_field(self):
        
        self.assertEqual(sql_for_table(Article), sql_for_table(ArticleRef))

    @skipUnlessDBFeature("supports_tablespaces")
    def test_tablespace_for_many_to_many_field(self):
        sql = sql_for_table(Authors).lower()
        
        
        if settings.DEFAULT_INDEX_TABLESPACE:
            
            self.assertNumContains(sql, "tbl_tbsp", 1)
            
            self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 1)
        else:
            
            self.assertNumContains(sql, "tbl_tbsp", 2)
        self.assertNumContains(sql, "idx_tbsp", 0)

        sql = sql_for_index(Authors).lower()
        
        
        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()
        
        
        if settings.DEFAULT_INDEX_TABLESPACE:
            
            self.assertNumContains(sql, "tbl_tbsp", 1)
            
            self.assertNumContains(sql, settings.DEFAULT_INDEX_TABLESPACE, 1)
        else:
            
            self.assertNumContains(sql, "tbl_tbsp", 2)
        self.assertNumContains(sql, "idx_tbsp", 0)

        sql = sql_for_index(Reviewers).lower()
        
        self.assertNumContains(sql, "tbl_tbsp", 0)
        self.assertNumContains(sql, "idx_tbsp", 2)