2013-07-29 19:19:04 +02:00
|
|
|
from __future__ import unicode_literals
|
2012-11-24 06:43:20 +01:00
|
|
|
|
|
|
|
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
|
2013-12-23 16:01:13 +01:00
|
|
|
from django.test import TestCase, override_settings
|
2012-11-24 06:43:20 +01:00
|
|
|
|
2013-02-26 13:19:18 +01:00
|
|
|
from swappable_models.models import Article
|
2012-12-20 09:10:19 +01:00
|
|
|
|
2012-11-24 06:43:20 +01:00
|
|
|
|
|
|
|
class SwappableModelTests(TestCase):
|
2013-06-14 23:11:51 +02:00
|
|
|
|
|
|
|
available_apps = [
|
|
|
|
'swappable_models',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
]
|
|
|
|
|
2012-11-24 06:43:20 +01:00
|
|
|
@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
|
2012-11-24 08:07:50 +01:00
|
|
|
Permission.objects.filter(content_type__app_label='swappable_models').delete()
|
|
|
|
ContentType.objects.filter(app_label='swappable_models').delete()
|
2012-11-24 06:43:20 +01:00
|
|
|
|
2013-09-03 17:51:34 +02:00
|
|
|
# Re-run migrate. This will re-build the permissions and content types.
|
2012-11-24 06:43:20 +01:00
|
|
|
new_io = StringIO()
|
2013-09-03 17:51:34 +02:00
|
|
|
management.call_command('migrate', load_initial_data=False, interactive=False, stdout=new_io)
|
2012-11-24 06:43:20 +01:00
|
|
|
|
|
|
|
# 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)
|
2012-12-20 09:10:19 +01:00
|
|
|
|
|
|
|
@override_settings(TEST_ARTICLE_MODEL='swappable_models.article')
|
|
|
|
def test_case_insensitive(self):
|
|
|
|
"Model names are case insensitive. Check that model swapping honors this."
|
|
|
|
try:
|
|
|
|
Article.objects.all()
|
|
|
|
except AttributeError:
|
|
|
|
self.fail('Swappable model names should be case insensitive.')
|
|
|
|
|
|
|
|
self.assertIsNone(Article._meta.swapped)
|