2012-11-24 13:43:20 +08:00
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.core import management
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import TestCase, override_settings
|
2017-01-07 19:11:46 +08:00
|
|
|
|
|
|
|
from .models import Article
|
2012-12-20 16:10:19 +08:00
|
|
|
|
2012-11-24 13:43:20 +08:00
|
|
|
|
|
|
|
class SwappableModelTests(TestCase):
|
2013-06-15 05:11:51 +08:00
|
|
|
|
2017-01-28 09:06:55 +08:00
|
|
|
# Limit memory usage when calling 'migrate'.
|
|
|
|
available_apps = [
|
|
|
|
'swappable_models',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
]
|
|
|
|
|
2012-11-24 13:43:20 +08: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 15:07:50 +08:00
|
|
|
Permission.objects.filter(content_type__app_label='swappable_models').delete()
|
|
|
|
ContentType.objects.filter(app_label='swappable_models').delete()
|
2012-11-24 13:43:20 +08:00
|
|
|
|
2013-09-03 23:51:34 +08:00
|
|
|
# Re-run migrate. This will re-build the permissions and content types.
|
2020-05-13 15:12:43 +08:00
|
|
|
management.call_command('migrate', interactive=False, verbosity=0)
|
2012-11-24 13:43:20 +08:00
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Content types and permissions exist for the swapped model,
|
2012-11-24 13:43:20 +08:00
|
|
|
# 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 16:10:19 +08:00
|
|
|
|
|
|
|
@override_settings(TEST_ARTICLE_MODEL='swappable_models.article')
|
|
|
|
def test_case_insensitive(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"Model names are case insensitive. Model swapping honors this."
|
2016-06-28 23:21:26 +08:00
|
|
|
Article.objects.all()
|
2012-12-20 16:10:19 +08:00
|
|
|
self.assertIsNone(Article._meta.swapped)
|