2
0

tests.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import unicode_literals
  2. from swappable_models.models import Article
  3. from django.contrib.auth.models import Permission
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.core import management
  6. from django.test import TestCase, override_settings
  7. from django.utils.six import StringIO
  8. class SwappableModelTests(TestCase):
  9. available_apps = [
  10. 'swappable_models',
  11. 'django.contrib.auth',
  12. 'django.contrib.contenttypes',
  13. ]
  14. @override_settings(TEST_ARTICLE_MODEL='swappable_models.AlternateArticle')
  15. def test_generated_data(self):
  16. "Permissions and content types are not created for a swapped model"
  17. # Delete all permissions and content_types
  18. Permission.objects.filter(content_type__app_label='swappable_models').delete()
  19. ContentType.objects.filter(app_label='swappable_models').delete()
  20. # Re-run migrate. This will re-build the permissions and content types.
  21. new_io = StringIO()
  22. management.call_command('migrate', interactive=False, stdout=new_io)
  23. # Check that content types and permissions exist for the swapped model,
  24. # but not for the swappable model.
  25. apps_models = [(p.content_type.app_label, p.content_type.model)
  26. for p in Permission.objects.all()]
  27. self.assertIn(('swappable_models', 'alternatearticle'), apps_models)
  28. self.assertNotIn(('swappable_models', 'article'), apps_models)
  29. apps_models = [(ct.app_label, ct.model)
  30. for ct in ContentType.objects.all()]
  31. self.assertIn(('swappable_models', 'alternatearticle'), apps_models)
  32. self.assertNotIn(('swappable_models', 'article'), apps_models)
  33. @override_settings(TEST_ARTICLE_MODEL='swappable_models.article')
  34. def test_case_insensitive(self):
  35. "Model names are case insensitive. Check that model swapping honors this."
  36. Article.objects.all()
  37. self.assertIsNone(Article._meta.swapped)