tests.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import absolute_import, unicode_literals
  2. import os
  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. from django.utils._os import upath
  7. from .models import (ConcreteModel, ConcreteModelSubclass,
  8. ConcreteModelSubclassProxy)
  9. class ProxyModelInheritanceTests(TransactionTestCase):
  10. """
  11. Proxy model inheritance across apps can result in migrate not creating the table
  12. for the proxied model (as described in #12286). This test creates two dummy
  13. apps and calls migrate, then verifies that the table has been created.
  14. """
  15. available_apps = []
  16. def test_table_exists(self):
  17. with extend_sys_path(os.path.dirname(os.path.abspath(upath(__file__)))):
  18. with self.modify_settings(INSTALLED_APPS={'append': ['app1', 'app2']}):
  19. call_command('migrate', verbosity=0, run_syncdb=True)
  20. from app1.models import ProxyModel
  21. from app2.models import NiceModel
  22. self.assertEqual(NiceModel.objects.all().count(), 0)
  23. self.assertEqual(ProxyModel.objects.all().count(), 0)
  24. class MultiTableInheritanceProxyTest(TestCase):
  25. def test_model_subclass_proxy(self):
  26. """
  27. Deleting an instance of a model proxying a multi-table inherited
  28. subclass should cascade delete down the whole inheritance chain (see
  29. #18083).
  30. """
  31. instance = ConcreteModelSubclassProxy.objects.create()
  32. instance.delete()
  33. self.assertEqual(0, ConcreteModelSubclassProxy.objects.count())
  34. self.assertEqual(0, ConcreteModelSubclass.objects.count())
  35. self.assertEqual(0, ConcreteModel.objects.count())