3 from django
.conf
import settings
4 from django
.db
import connection
5 from django
.db
import models
6 from django
.db
.models
.loading
import cache
7 from django
.core
.management
.color
import no_style
8 from django
.test
import TestCase
, skipIfDBFeature
, skipUnlessDBFeature
10 from models
import Article
, ArticleRef
, Authors
, Reviewers
, Scientist
, ScientistRef
12 # We can't test the DEFAULT_TABLESPACE and DEFAULT_INDEX_TABLESPACE settings
13 # because they're evaluated when the model class is defined. As a consequence,
14 # @override_settings doesn't work, and the tests depend
16 def sql_for_table(model
):
17 return '\n'.join(connection
.creation
.sql_create_model(model
, no_style())[0])
19 def sql_for_index(model
):
20 return '\n'.join(connection
.creation
.sql_indexes_for_model(model
, no_style()))
23 class TablespacesTests(TestCase
):
26 # The unmanaged models need to be removed after the test in order to
27 # prevent bad interactions with the flush operation in other tests.
28 self
.old_app_models
= copy
.deepcopy(cache
.app_models
)
29 self
.old_app_store
= copy
.deepcopy(cache
.app_store
)
31 for model
in Article
, Authors
, Reviewers
, Scientist
:
32 model
._meta
.managed
= True
35 for model
in Article
, Authors
, Reviewers
, Scientist
:
36 model
._meta
.managed
= False
38 cache
.app_models
= self
.old_app_models
39 cache
.app_store
= self
.old_app_store
40 cache
._get
_models
_cache
= {}
42 def assertNumContains(self
, haystack
, needle
, count
):
43 real_count
= haystack
.count(needle
)
44 self
.assertEqual(real_count
, count
, "Found %d instances of '%s', "
45 "expected %d" % (real_count
, needle
, count
))
47 @skipUnlessDBFeature('supports_tablespaces')
48 def test_tablespace_for_model(self
):
49 sql
= sql_for_table(Scientist
).lower()
50 if settings
.DEFAULT_INDEX_TABLESPACE
:
52 self
.assertNumContains(sql
, 'tbl_tbsp', 1)
53 # 1 for the index on the primary key
54 self
.assertNumContains(sql
, settings
.DEFAULT_INDEX_TABLESPACE
, 1)
56 # 1 for the table + 1 for the index on the primary key
57 self
.assertNumContains(sql
, 'tbl_tbsp', 2)
59 @skipIfDBFeature('supports_tablespaces')
60 def test_tablespace_ignored_for_model(self
):
61 # No tablespace-related SQL
62 self
.assertEqual(sql_for_table(Scientist
),
63 sql_for_table(ScientistRef
))
65 @skipUnlessDBFeature('supports_tablespaces')
66 def test_tablespace_for_indexed_field(self
):
67 sql
= sql_for_table(Article
).lower()
68 if settings
.DEFAULT_INDEX_TABLESPACE
:
70 self
.assertNumContains(sql
, 'tbl_tbsp', 1)
71 # 1 for the primary key + 1 for the index on code
72 self
.assertNumContains(sql
, settings
.DEFAULT_INDEX_TABLESPACE
, 2)
74 # 1 for the table + 1 for the primary key + 1 for the index on code
75 self
.assertNumContains(sql
, 'tbl_tbsp', 3)
77 # 1 for the index on reference
78 self
.assertNumContains(sql
, 'idx_tbsp', 1)
80 @skipIfDBFeature('supports_tablespaces')
81 def test_tablespace_ignored_for_indexed_field(self
):
82 # No tablespace-related SQL
83 self
.assertEqual(sql_for_table(Article
),
84 sql_for_table(ArticleRef
))
86 @skipUnlessDBFeature('supports_tablespaces')
87 def test_tablespace_for_many_to_many_field(self
):
88 sql
= sql_for_table(Authors
).lower()
89 # The join table of the ManyToManyField goes to the model's tablespace,
90 # and its indexes too, unless DEFAULT_INDEX_TABLESPACE is set.
91 if settings
.DEFAULT_INDEX_TABLESPACE
:
93 self
.assertNumContains(sql
, 'tbl_tbsp', 1)
94 # 1 for the primary key
95 self
.assertNumContains(sql
, settings
.DEFAULT_INDEX_TABLESPACE
, 1)
97 # 1 for the table + 1 for the index on the primary key
98 self
.assertNumContains(sql
, 'tbl_tbsp', 2)
99 self
.assertNumContains(sql
, 'idx_tbsp', 0)
101 sql
= sql_for_index(Authors
).lower()
102 # The ManyToManyField declares no db_tablespace, its indexes go to
103 # the model's tablespace, unless DEFAULT_INDEX_TABLESPACE is set.
104 if settings
.DEFAULT_INDEX_TABLESPACE
:
105 self
.assertNumContains(sql
, settings
.DEFAULT_INDEX_TABLESPACE
, 2)
107 self
.assertNumContains(sql
, 'tbl_tbsp', 2)
108 self
.assertNumContains(sql
, 'idx_tbsp', 0)
110 sql
= sql_for_table(Reviewers
).lower()
111 # The join table of the ManyToManyField goes to the model's tablespace,
112 # and its indexes too, unless DEFAULT_INDEX_TABLESPACE is set.
113 if settings
.DEFAULT_INDEX_TABLESPACE
:
115 self
.assertNumContains(sql
, 'tbl_tbsp', 1)
116 # 1 for the primary key
117 self
.assertNumContains(sql
, settings
.DEFAULT_INDEX_TABLESPACE
, 1)
119 # 1 for the table + 1 for the index on the primary key
120 self
.assertNumContains(sql
, 'tbl_tbsp', 2)
121 self
.assertNumContains(sql
, 'idx_tbsp', 0)
123 sql
= sql_for_index(Reviewers
).lower()
124 # The ManyToManyField declares db_tablespace, its indexes go there.
125 self
.assertNumContains(sql
, 'tbl_tbsp', 0)
126 self
.assertNumContains(sql
, 'idx_tbsp', 2)