From 7bd2ad1dd9fc449ed1b4832fab5c975d7c8aa895 Mon Sep 17 00:00:00 2001 From: Moayad Mardini Date: Tue, 10 Jun 2014 22:35:48 +0300 Subject: [1.7.x] Created a new tests folder (`model_options`). And moved `tablespaces` option tests to it. The new folder can be used to test models/options, like the new option added in refs #22778. Backport of 5a3ae7e260 from master --- tests/model_options/__init__.py | 0 tests/model_options/models/__init__.py | 0 tests/model_options/models/tablespaces.py | 48 +++++++++++ tests/model_options/test_tablespaces.py | 128 ++++++++++++++++++++++++++++++ tests/tablespaces/__init__.py | 0 tests/tablespaces/models.py | 48 ----------- tests/tablespaces/tests.py | 126 ----------------------------- 7 files changed, 176 insertions(+), 174 deletions(-) create mode 100644 tests/model_options/__init__.py create mode 100644 tests/model_options/models/__init__.py create mode 100644 tests/model_options/models/tablespaces.py create mode 100644 tests/model_options/test_tablespaces.py delete mode 100644 tests/tablespaces/__init__.py delete mode 100644 tests/tablespaces/models.py delete mode 100644 tests/tablespaces/tests.py diff --git a/tests/model_options/__init__.py b/tests/model_options/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/model_options/models/__init__.py b/tests/model_options/models/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/model_options/models/tablespaces.py b/tests/model_options/models/tablespaces.py new file mode 100644 index 0000000000..56e22e973a --- /dev/null +++ b/tests/model_options/models/tablespaces.py @@ -0,0 +1,48 @@ +from django.db import models + +# Since the test database doesn't have tablespaces, it's impossible for Django +# to create the tables for models where db_tablespace is set. To avoid this +# problem, we mark the models as unmanaged, and temporarily revert them to +# managed during each test. We also set them to use the same tables as the +# "reference" models to avoid errors when other tests run 'migrate' +# (proxy_models_inheritance does). + + +class ScientistRef(models.Model): + name = models.CharField(max_length=50) + + +class ArticleRef(models.Model): + title = models.CharField(max_length=50, unique=True) + code = models.CharField(max_length=50, unique=True) + authors = models.ManyToManyField(ScientistRef, related_name='articles_written_set') + reviewers = models.ManyToManyField(ScientistRef, related_name='articles_reviewed_set') + + +class Scientist(models.Model): + name = models.CharField(max_length=50) + + class Meta: + db_table = 'model_options_scientistref' + db_tablespace = 'tbl_tbsp' + managed = False + + +class Article(models.Model): + title = models.CharField(max_length=50, unique=True) + code = models.CharField(max_length=50, unique=True, db_tablespace='idx_tbsp') + authors = models.ManyToManyField(Scientist, related_name='articles_written_set') + reviewers = models.ManyToManyField(Scientist, related_name='articles_reviewed_set', db_tablespace='idx_tbsp') + + class Meta: + db_table = 'model_options_articleref' + db_tablespace = 'tbl_tbsp' + managed = False + +# Also set the tables for automatically created models + +Authors = Article._meta.get_field('authors').rel.through +Authors._meta.db_table = 'model_options_articleref_authors' + +Reviewers = Article._meta.get_field('reviewers').rel.through +Reviewers._meta.db_table = 'model_options_articleref_reviewers' diff --git a/tests/model_options/test_tablespaces.py b/tests/model_options/test_tablespaces.py new file mode 100644 index 0000000000..ac331bd36c --- /dev/null +++ b/tests/model_options/test_tablespaces.py @@ -0,0 +1,128 @@ +from __future__ import unicode_literals + +from django.apps import apps +from django.conf import settings +from django.db import connection +from django.core.management.color import no_style +from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature + +from .models.tablespaces import (Article, ArticleRef, Authors, Reviewers, + Scientist, ScientistRef) + + +def sql_for_table(model): + return '\n'.join(connection.creation.sql_create_model(model, + no_style())[0]) + + +def sql_for_index(model): + return '\n'.join(connection.creation.sql_indexes_for_model(model, + no_style())) + + +# 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 +class TablespacesTests(TestCase): + + def setUp(self): + # The unmanaged models need to be removed after the test in order to + # prevent bad interactions with the flush operation in other tests. + 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: + # 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): + # No tablespace-related SQL + 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: + # 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, 'idx_tbsp', 1) + + @skipIfDBFeature('supports_tablespaces') + def test_tablespace_ignored_for_indexed_field(self): + # No tablespace-related SQL + 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() + # 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) diff --git a/tests/tablespaces/__init__.py b/tests/tablespaces/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/tablespaces/models.py b/tests/tablespaces/models.py deleted file mode 100644 index ecd1944294..0000000000 --- a/tests/tablespaces/models.py +++ /dev/null @@ -1,48 +0,0 @@ -from django.db import models - -# Since the test database doesn't have tablespaces, it's impossible for Django -# to create the tables for models where db_tablespace is set. To avoid this -# problem, we mark the models as unmanaged, and temporarily revert them to -# managed during each test. We also set them to use the same tables as the -# "reference" models to avoid errors when other tests run 'migrate' -# (proxy_models_inheritance does). - - -class ScientistRef(models.Model): - name = models.CharField(max_length=50) - - -class ArticleRef(models.Model): - title = models.CharField(max_length=50, unique=True) - code = models.CharField(max_length=50, unique=True) - authors = models.ManyToManyField(ScientistRef, related_name='articles_written_set') - reviewers = models.ManyToManyField(ScientistRef, related_name='articles_reviewed_set') - - -class Scientist(models.Model): - name = models.CharField(max_length=50) - - class Meta: - db_table = 'tablespaces_scientistref' - db_tablespace = 'tbl_tbsp' - managed = False - - -class Article(models.Model): - title = models.CharField(max_length=50, unique=True) - code = models.CharField(max_length=50, unique=True, db_tablespace='idx_tbsp') - authors = models.ManyToManyField(Scientist, related_name='articles_written_set') - reviewers = models.ManyToManyField(Scientist, related_name='articles_reviewed_set', db_tablespace='idx_tbsp') - - class Meta: - db_table = 'tablespaces_articleref' - db_tablespace = 'tbl_tbsp' - managed = False - -# Also set the tables for automatically created models - -Authors = Article._meta.get_field('authors').rel.through -Authors._meta.db_table = 'tablespaces_articleref_authors' - -Reviewers = Article._meta.get_field('reviewers').rel.through -Reviewers._meta.db_table = 'tablespaces_articleref_reviewers' diff --git a/tests/tablespaces/tests.py b/tests/tablespaces/tests.py deleted file mode 100644 index 8c69e70a19..0000000000 --- a/tests/tablespaces/tests.py +++ /dev/null @@ -1,126 +0,0 @@ -from __future__ import unicode_literals - -from django.apps import apps -from django.conf import settings -from django.db import connection -from django.core.management.color import no_style -from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature - -from .models import Article, ArticleRef, Authors, Reviewers, Scientist, ScientistRef - -# 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 - - -def sql_for_table(model): - return '\n'.join(connection.creation.sql_create_model(model, no_style())[0]) - - -def sql_for_index(model): - return '\n'.join(connection.creation.sql_indexes_for_model(model, no_style())) - - -class TablespacesTests(TestCase): - - def setUp(self): - # The unmanaged models need to be removed after the test in order to - # prevent bad interactions with the flush operation in other tests. - self._old_models = apps.app_configs['tablespaces'].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['tablespaces'].models = self._old_models - apps.all_models['tablespaces'] = 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: - # 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): - # No tablespace-related SQL - 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: - # 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, 'idx_tbsp', 1) - - @skipIfDBFeature('supports_tablespaces') - def test_tablespace_ignored_for_indexed_field(self): - # No tablespace-related SQL - 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() - # 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) -- cgit v1.3