tests.py 1.9 KB

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