diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2012-11-24 13:43:20 +0800 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2012-11-24 13:43:20 +0800 |
| commit | c8985a8a7317042a641e870cb75b3005cc5d67b1 (patch) | |
| tree | fa0c590a7c8430c00443efe0c32677df876eb758 /tests | |
| parent | 0a0a0d66b316598f7c296e8bf75749a14ce3ac49 (diff) | |
Fixed #19806 -- Ensure that content types and permissions aren't created for swapped models.
Thanks to rizumu for the report.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/swappable_models/__init__.py | 0 | ||||
| -rw-r--r-- | tests/regressiontests/swappable_models/models.py | 15 | ||||
| -rw-r--r-- | tests/regressiontests/swappable_models/tests.py | 46 |
3 files changed, 61 insertions, 0 deletions
diff --git a/tests/regressiontests/swappable_models/__init__.py b/tests/regressiontests/swappable_models/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/regressiontests/swappable_models/__init__.py diff --git a/tests/regressiontests/swappable_models/models.py b/tests/regressiontests/swappable_models/models.py new file mode 100644 index 0000000000..92692d4396 --- /dev/null +++ b/tests/regressiontests/swappable_models/models.py @@ -0,0 +1,15 @@ +from django.db import models + + +class Article(models.Model): + title = models.CharField(max_length=100) + publication_date = models.DateField() + + class Meta: + swappable = 'TEST_ARTICLE_MODEL' + + +class AlternateArticle(models.Model): + title = models.CharField(max_length=100) + publication_date = models.DateField() + byline = models.CharField(max_length=100) diff --git a/tests/regressiontests/swappable_models/tests.py b/tests/regressiontests/swappable_models/tests.py new file mode 100644 index 0000000000..d9a01f9e26 --- /dev/null +++ b/tests/regressiontests/swappable_models/tests.py @@ -0,0 +1,46 @@ +from __future__ import absolute_import, unicode_literals + +from django.utils.six import StringIO + +from django.contrib.auth.models import Permission +from django.contrib.contenttypes.models import ContentType +from django.core import management +from django.db.models.loading import cache +from django.test import TestCase +from django.test.utils import override_settings + + +class SwappableModelTests(TestCase): + def setUp(self): + # This test modifies the installed apps, so we need to make sure + # we're not dealing with a cached app list. + cache._get_models_cache.clear() + + def tearDown(self): + # By fiddling with swappable models, we alter the installed models + # cache, so flush it to make sure there are no side effects. + cache._get_models_cache.clear() + + @override_settings(TEST_ARTICLE_MODEL='swappable_models.AlternateArticle') + def test_generated_data(self): + "Permissions and content types are not created for a swapped model" + + # Delete all permissions and content_types + Permission.objects.all().delete() + ContentType.objects.all().delete() + + # Re-run syncdb. This will re-build the permissions and content types. + new_io = StringIO() + management.call_command('syncdb', load_initial_data=False, interactive=False, stdout=new_io) + + # Check that content types and permissions exist for the swapped model, + # but not for the swappable model. + apps_models = [(p.content_type.app_label, p.content_type.model) + for p in Permission.objects.all()] + self.assertIn(('swappable_models', 'alternatearticle'), apps_models) + self.assertNotIn(('swappable_models', 'article'), apps_models) + + apps_models = [(ct.app_label, ct.model) + for ct in ContentType.objects.all()] + self.assertIn(('swappable_models', 'alternatearticle'), apps_models) + self.assertNotIn(('swappable_models', 'article'), apps_models) |
