From de8af67680df536e984b0a78bdc340e96169ec52 Mon Sep 17 00:00:00 2001 From: Ben Cail Date: Fri, 23 Feb 2024 10:16:39 -0500 Subject: [PATCH] add a couple tests for OPTIONS values --- tests/model_options/test_tablespaces.py | 48 ++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/model_options/test_tablespaces.py b/tests/model_options/test_tablespaces.py index 0aa2e0fccf..aba8f8b6a7 100644 --- a/tests/model_options/test_tablespaces.py +++ b/tests/model_options/test_tablespaces.py @@ -1,7 +1,9 @@ +import copy from django.apps import apps from django.conf import settings -from django.db import connection +from django.db import connection, models from django.test import TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature +from django.test.utils import isolate_apps from .models.tablespaces import ( Article, @@ -133,3 +135,47 @@ class TablespacesTests(TransactionTestCase): # The ManyToManyField declares db_tablespace, its indexes go there. self.assertNumContains(sql, "tbl_tbsp", 0) self.assertNumContains(sql, "idx_tbsp", 2) + + +class DefaultTablespaceTests(TransactionTestCase): + available_apps = ["model_options"] + + 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") + @isolate_apps("model_options") + def test_default_tablespace(self): + tablespace = "default_tablespace" + databases = copy.deepcopy(settings.DATABASES) + databases["default"]["OPTIONS"]["DEFAULT_TABLESPACE"] = tablespace + with self.settings(DATABASES=databases): + + class ScientistRef(models.Model): + name = models.CharField(max_length=50) + + sql = sql_for_table(ScientistRef).lower() + + # 1 for the table + 1 for the index on the primary key + self.assertNumContains(sql, tablespace, 2) + + @skipUnlessDBFeature("supports_tablespaces") + @isolate_apps("model_options") + def test_default_index_tablespace(self): + index_tablespace = "default_index_tablespace" + databases = copy.deepcopy(settings.DATABASES) + databases["default"]["OPTIONS"]["DEFAULT_INDEX_TABLESPACE"] = index_tablespace + with self.settings(DATABASES=databases): + + class ScientistRef(models.Model): + name = models.CharField(max_length=50) + + sql = sql_for_table(ScientistRef).lower() + + # 1 for the index on the primary key + self.assertNumContains(sql, index_tablespace, 1)