2
0

tests.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. from __future__ import absolute_import, unicode_literals
  2. import os
  3. import sys
  4. from unittest import skipUnless
  5. from django.apps import apps
  6. from django.apps.registry import Apps
  7. from django.core.exceptions import ImproperlyConfigured
  8. from django.db import models
  9. from django.test import TestCase, override_settings
  10. from django.test.utils import extend_sys_path
  11. from django.utils._os import upath
  12. from django.utils import six
  13. from .default_config_app.apps import CustomConfig
  14. from .models import TotallyNormal, SoAlternative, new_apps
  15. # Small list with a variety of cases for tests that iterate on installed apps.
  16. # Intentionally not in alphabetical order to check if the order is preserved.
  17. SOME_INSTALLED_APPS = [
  18. 'apps.apps.MyAdmin',
  19. 'apps.apps.MyAuth',
  20. 'django.contrib.contenttypes',
  21. 'django.contrib.sessions',
  22. 'django.contrib.messages',
  23. 'django.contrib.staticfiles',
  24. ]
  25. SOME_INSTALLED_APPS_NAMES = [
  26. 'django.contrib.admin',
  27. 'django.contrib.auth',
  28. ] + SOME_INSTALLED_APPS[2:]
  29. HERE = os.path.dirname(__file__)
  30. class AppsTests(TestCase):
  31. def test_singleton_master(self):
  32. """
  33. Ensures that only one master registry can exist.
  34. """
  35. with self.assertRaises(RuntimeError):
  36. Apps(installed_apps=None)
  37. def test_ready(self):
  38. """
  39. Tests the ready property of the master registry.
  40. """
  41. # The master app registry is always ready when the tests run.
  42. self.assertTrue(apps.ready)
  43. # Non-master app registries are populated in __init__.
  44. self.assertTrue(Apps().ready)
  45. def test_bad_app_config(self):
  46. """
  47. Tests when INSTALLED_APPS contains an incorrect app config.
  48. """
  49. with self.assertRaises(ImproperlyConfigured):
  50. with self.settings(INSTALLED_APPS=['apps.apps.BadConfig']):
  51. pass
  52. def test_not_an_app_config(self):
  53. """
  54. Tests when INSTALLED_APPS contains a class that isn't an app config.
  55. """
  56. with self.assertRaises(ImproperlyConfigured):
  57. with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
  58. pass
  59. def test_no_such_app(self):
  60. """
  61. Tests when INSTALLED_APPS contains an app that doesn't exist, either
  62. directly or via an app config.
  63. """
  64. with self.assertRaises(ImportError):
  65. with self.settings(INSTALLED_APPS=['there is no such app']):
  66. pass
  67. with self.assertRaises(ImportError):
  68. with self.settings(INSTALLED_APPS=['apps.apps.NoSuchApp']):
  69. pass
  70. def test_no_such_app_config(self):
  71. """
  72. Tests when INSTALLED_APPS contains an entry that doesn't exist.
  73. """
  74. with self.assertRaises(ImportError):
  75. with self.settings(INSTALLED_APPS=['apps.apps.NoSuchConfig']):
  76. pass
  77. def test_default_app_config(self):
  78. with self.settings(INSTALLED_APPS=['apps.default_config_app']):
  79. config = apps.get_app_config('default_config_app')
  80. self.assertIsInstance(config, CustomConfig)
  81. @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
  82. def test_get_app_configs(self):
  83. """
  84. Tests get_app_configs().
  85. """
  86. app_configs = apps.get_app_configs()
  87. self.assertListEqual(
  88. [app_config.name for app_config in app_configs],
  89. SOME_INSTALLED_APPS_NAMES)
  90. @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
  91. def test_get_app_config(self):
  92. """
  93. Tests get_app_config().
  94. """
  95. app_config = apps.get_app_config('admin')
  96. self.assertEqual(app_config.name, 'django.contrib.admin')
  97. app_config = apps.get_app_config('staticfiles')
  98. self.assertEqual(app_config.name, 'django.contrib.staticfiles')
  99. with self.assertRaises(LookupError):
  100. apps.get_app_config('webdesign')
  101. @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
  102. def test_is_installed(self):
  103. self.assertTrue(apps.is_installed('django.contrib.admin'))
  104. self.assertTrue(apps.is_installed('django.contrib.auth'))
  105. self.assertTrue(apps.is_installed('django.contrib.staticfiles'))
  106. self.assertFalse(apps.is_installed('django.contrib.webdesign'))
  107. @override_settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig'])
  108. def test_relabeling(self):
  109. self.assertEqual(apps.get_app_config('relabeled').name, 'apps')
  110. def test_duplicate_labels(self):
  111. with six.assertRaisesRegex(self, ImproperlyConfigured, "Application labels aren't unique"):
  112. with self.settings(INSTALLED_APPS=['apps.apps.PlainAppsConfig', 'apps']):
  113. pass
  114. def test_duplicate_names(self):
  115. with six.assertRaisesRegex(self, ImproperlyConfigured, "Application names aren't unique"):
  116. with self.settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig', 'apps']):
  117. pass
  118. def test_models_py(self):
  119. """
  120. Tests that the models in the models.py file were loaded correctly.
  121. """
  122. self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
  123. with self.assertRaises(LookupError):
  124. apps.get_model("apps", "SoAlternative")
  125. with self.assertRaises(LookupError):
  126. new_apps.get_model("apps", "TotallyNormal")
  127. self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)
  128. def test_dynamic_load(self):
  129. """
  130. Makes a new model at runtime and ensures it goes into the right place.
  131. """
  132. old_models = list(apps.get_app_config("apps").get_models())
  133. # Construct a new model in a new app registry
  134. body = {}
  135. new_apps = Apps(["apps"])
  136. meta_contents = {
  137. 'app_label': "apps",
  138. 'apps': new_apps,
  139. }
  140. meta = type(str("Meta"), tuple(), meta_contents)
  141. body['Meta'] = meta
  142. body['__module__'] = TotallyNormal.__module__
  143. temp_model = type(str("SouthPonies"), (models.Model,), body)
  144. # Make sure it appeared in the right place!
  145. self.assertListEqual(list(apps.get_app_config("apps").get_models()), old_models)
  146. with self.assertRaises(LookupError):
  147. apps.get_model("apps", "SouthPonies")
  148. self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)
  149. @skipUnless(
  150. sys.version_info > (3, 3, 0),
  151. "Namespace packages sans __init__.py were added in Python 3.3")
  152. class NamespacePackageAppTests(TestCase):
  153. # We need nsapp to be top-level so our multiple-paths tests can add another
  154. # location for it (if its inside a normal package with an __init__.py that
  155. # isn't possible). In order to avoid cluttering the already-full tests/ dir
  156. # (which is on sys.path), we add these new entries to sys.path temporarily.
  157. base_location = os.path.join(HERE, 'namespace_package_base')
  158. other_location = os.path.join(HERE, 'namespace_package_other_base')
  159. app_path = os.path.join(base_location, 'nsapp')
  160. def test_single_path(self):
  161. """
  162. A Py3.3+ namespace package can be an app if it has only one path.
  163. """
  164. with extend_sys_path(self.base_location):
  165. with self.settings(INSTALLED_APPS=['nsapp']):
  166. app_config = apps.get_app_config('nsapp')
  167. self.assertEqual(app_config.path, upath(self.app_path))
  168. def test_multiple_paths(self):
  169. """
  170. A Py3.3+ namespace package with multiple locations cannot be an app.
  171. (Because then we wouldn't know where to load its templates, static
  172. assets, etc from.)
  173. """
  174. # Temporarily add two directories to sys.path that both contain
  175. # components of the "nsapp" package.
  176. with extend_sys_path(self.base_location, self.other_location):
  177. with self.assertRaises(ImproperlyConfigured):
  178. with self.settings(INSTALLED_APPS=['nsapp']):
  179. pass
  180. def test_multiple_paths_explicit_path(self):
  181. """
  182. Multiple locations are ok only if app-config has explicit path.
  183. """
  184. # Temporarily add two directories to sys.path that both contain
  185. # components of the "nsapp" package.
  186. with extend_sys_path(self.base_location, self.other_location):
  187. with self.settings(INSTALLED_APPS=['nsapp.apps.NSAppConfig']):
  188. app_config = apps.get_app_config('nsapp')
  189. self.assertEqual(app_config.path, upath(self.app_path))