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