1. import os
    
  2. 
    
  3. from django.core.management import call_command
    
  4. from django.test import TestCase, TransactionTestCase
    
  5. from django.test.utils import extend_sys_path
    
  6. 
    
  7. from .models import (
    
  8.     ConcreteModel,
    
  9.     ConcreteModelSubclass,
    
  10.     ConcreteModelSubclassProxy,
    
  11.     ProxyModel,
    
  12. )
    
  13. 
    
  14. 
    
  15. class ProxyModelInheritanceTests(TransactionTestCase):
    
  16.     """
    
  17.     Proxy model inheritance across apps can result in migrate not creating the table
    
  18.     for the proxied model (as described in #12286).  This test creates two dummy
    
  19.     apps and calls migrate, then verifies that the table has been created.
    
  20.     """
    
  21. 
    
  22.     available_apps = []
    
  23. 
    
  24.     def test_table_exists(self):
    
  25.         with extend_sys_path(os.path.dirname(os.path.abspath(__file__))):
    
  26.             with self.modify_settings(INSTALLED_APPS={"append": ["app1", "app2"]}):
    
  27.                 call_command("migrate", verbosity=0, run_syncdb=True)
    
  28.                 from app1.models import ProxyModel
    
  29.                 from app2.models import NiceModel
    
  30. 
    
  31.                 self.assertEqual(NiceModel.objects.count(), 0)
    
  32.                 self.assertEqual(ProxyModel.objects.count(), 0)
    
  33. 
    
  34. 
    
  35. class MultiTableInheritanceProxyTest(TestCase):
    
  36.     def test_model_subclass_proxy(self):
    
  37.         """
    
  38.         Deleting an instance of a model proxying a multi-table inherited
    
  39.         subclass should cascade delete down the whole inheritance chain (see
    
  40.         #18083).
    
  41.         """
    
  42.         instance = ConcreteModelSubclassProxy.objects.create()
    
  43.         instance.delete()
    
  44.         self.assertEqual(0, ConcreteModelSubclassProxy.objects.count())
    
  45.         self.assertEqual(0, ConcreteModelSubclass.objects.count())
    
  46.         self.assertEqual(0, ConcreteModel.objects.count())
    
  47. 
    
  48.     def test_deletion_through_intermediate_proxy(self):
    
  49.         child = ConcreteModelSubclass.objects.create()
    
  50.         proxy = ProxyModel.objects.get(pk=child.pk)
    
  51.         proxy.delete()
    
  52.         self.assertFalse(ConcreteModel.objects.exists())
    
  53.         self.assertFalse(ConcreteModelSubclass.objects.exists())