tests.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from __future__ import absolute_import, unicode_literals
  2. from django.apps import apps
  3. from django.apps.registry import Apps
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.db import models
  6. from django.test import TestCase, override_settings
  7. from django.utils import six
  8. from .default_config_app.apps import CustomConfig
  9. from .models import TotallyNormal, SoAlternative, new_apps
  10. # Small list with a variety of cases for tests that iterate on installed apps.
  11. # Intentionally not in alphabetical order to check if the order is preserved.
  12. SOME_INSTALLED_APPS = [
  13. 'apps.apps.MyAdmin',
  14. 'apps.apps.MyAuth',
  15. 'django.contrib.contenttypes',
  16. 'django.contrib.sessions',
  17. 'django.contrib.messages',
  18. 'django.contrib.staticfiles',
  19. ]
  20. SOME_INSTALLED_APPS_NAMES = [
  21. 'django.contrib.admin',
  22. 'django.contrib.auth',
  23. ] + SOME_INSTALLED_APPS[2:]
  24. class AppsTests(TestCase):
  25. def test_singleton_master(self):
  26. """
  27. Ensures that only one master registry can exist.
  28. """
  29. with self.assertRaises(RuntimeError):
  30. Apps(installed_apps=None)
  31. def test_ready(self):
  32. """
  33. Tests the ready property of the master registry.
  34. """
  35. # The master app registry is always ready when the tests run.
  36. self.assertTrue(apps.ready)
  37. # Non-master app registries are populated in __init__.
  38. self.assertTrue(Apps().ready)
  39. def test_bad_app_config(self):
  40. """
  41. Tests when INSTALLED_APPS contains an incorrect app config.
  42. """
  43. with self.assertRaises(ImproperlyConfigured):
  44. with self.settings(INSTALLED_APPS=['apps.apps.BadConfig']):
  45. pass
  46. def test_not_an_app_config(self):
  47. """
  48. Tests when INSTALLED_APPS contains a class that isn't an app config.
  49. """
  50. with self.assertRaises(ImproperlyConfigured):
  51. with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
  52. pass
  53. def test_no_such_app(self):
  54. """
  55. Tests when INSTALLED_APPS contains an app that doesn't exist, either
  56. directly or via an app config.
  57. """
  58. with self.assertRaises(ImportError):
  59. with self.settings(INSTALLED_APPS=['there is no such app']):
  60. pass
  61. with self.assertRaises(ImportError):
  62. with self.settings(INSTALLED_APPS=['apps.apps.NoSuchApp']):
  63. pass
  64. def test_no_such_app_config(self):
  65. """
  66. Tests when INSTALLED_APPS contains an entry that doesn't exist.
  67. """
  68. with self.assertRaises(ImportError):
  69. with self.settings(INSTALLED_APPS=['apps.apps.NoSuchConfig']):
  70. pass
  71. def test_default_app_config(self):
  72. with self.settings(INSTALLED_APPS=['apps.default_config_app']):
  73. config = apps.get_app_config('default_config_app')
  74. self.assertIsInstance(config, CustomConfig)
  75. @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
  76. def test_get_app_configs(self):
  77. """
  78. Tests get_app_configs().
  79. """
  80. app_configs = apps.get_app_configs()
  81. self.assertListEqual(
  82. [app_config.name for app_config in app_configs],
  83. SOME_INSTALLED_APPS_NAMES)
  84. @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
  85. def test_get_app_config(self):
  86. """
  87. Tests get_app_config().
  88. """
  89. app_config = apps.get_app_config('admin')
  90. self.assertEqual(app_config.name, 'django.contrib.admin')
  91. app_config = apps.get_app_config('staticfiles')
  92. self.assertEqual(app_config.name, 'django.contrib.staticfiles')
  93. with self.assertRaises(LookupError):
  94. apps.get_app_config('webdesign')
  95. @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
  96. def test_is_installed(self):
  97. self.assertTrue(apps.is_installed('django.contrib.admin'))
  98. self.assertTrue(apps.is_installed('django.contrib.auth'))
  99. self.assertTrue(apps.is_installed('django.contrib.staticfiles'))
  100. self.assertFalse(apps.is_installed('django.contrib.webdesign'))
  101. @override_settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig'])
  102. def test_relabeling(self):
  103. self.assertEqual(apps.get_app_config('relabeled').name, 'apps')
  104. def test_duplicate_labels(self):
  105. with six.assertRaisesRegex(self, ImproperlyConfigured, "Application labels aren't unique"):
  106. with self.settings(INSTALLED_APPS=['apps.apps.PlainAppsConfig', 'apps']):
  107. pass
  108. def test_duplicate_names(self):
  109. with six.assertRaisesRegex(self, ImproperlyConfigured, "Application names aren't unique"):
  110. with self.settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig', 'apps']):
  111. pass
  112. def test_models_py(self):
  113. """
  114. Tests that the models in the models.py file were loaded correctly.
  115. """
  116. self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
  117. with self.assertRaises(LookupError):
  118. apps.get_model("apps", "SoAlternative")
  119. with self.assertRaises(LookupError):
  120. new_apps.get_model("apps", "TotallyNormal")
  121. self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)
  122. def test_dynamic_load(self):
  123. """
  124. Makes a new model at runtime and ensures it goes into the right place.
  125. """
  126. old_models = list(apps.get_app_config("apps").get_models())
  127. # Construct a new model in a new app registry
  128. body = {}
  129. new_apps = Apps(["apps"])
  130. meta_contents = {
  131. 'app_label': "apps",
  132. 'apps': new_apps,
  133. }
  134. meta = type(str("Meta"), tuple(), meta_contents)
  135. body['Meta'] = meta
  136. body['__module__'] = TotallyNormal.__module__
  137. temp_model = type(str("SouthPonies"), (models.Model,), body)
  138. # Make sure it appeared in the right place!
  139. self.assertListEqual(list(apps.get_app_config("apps").get_models()), old_models)
  140. with self.assertRaises(LookupError):
  141. apps.get_model("apps", "SouthPonies")
  142. self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)