2014-01-26 10:37:05 +08:00
|
|
|
import os
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.apps import AppConfig, apps
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps.registry import Apps
|
2014-01-26 19:46:28 +08:00
|
|
|
from django.contrib.admin.models import LogEntry
|
2015-02-09 02:38:09 +08:00
|
|
|
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.db import models
|
2020-07-21 16:35:12 +08:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings, override_settings
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test.utils import extend_sys_path, isolate_apps
|
2020-07-21 16:35:12 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango41Warning
|
2013-12-24 19:25:17 +08:00
|
|
|
|
2020-07-21 16:35:12 +08:00
|
|
|
from .explicit_default_config_app.apps import ExplicitDefaultConfig
|
2020-08-11 02:16:45 +08:00
|
|
|
from .explicit_default_config_empty_apps import ExplicitDefaultConfigEmptyApps
|
2020-07-21 16:35:12 +08:00
|
|
|
from .explicit_default_config_mismatch_app.not_apps import (
|
|
|
|
ExplicitDefaultConfigMismatch,
|
|
|
|
)
|
2020-08-11 02:16:45 +08:00
|
|
|
from .explicit_default_config_without_apps import (
|
|
|
|
ExplicitDefaultConfigWithoutApps,
|
|
|
|
)
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import SoAlternative, TotallyNormal, new_apps
|
2020-07-21 16:35:12 +08:00
|
|
|
from .one_config_app.apps import OneConfig
|
|
|
|
from .two_configs_one_default_app.apps import TwoConfig
|
2013-12-24 19:25:17 +08:00
|
|
|
|
2013-12-27 01:03:17 +08:00
|
|
|
# Small list with a variety of cases for tests that iterate on installed apps.
|
|
|
|
# Intentionally not in alphabetical order to check if the order is preserved.
|
|
|
|
|
|
|
|
SOME_INSTALLED_APPS = [
|
|
|
|
'apps.apps.MyAdmin',
|
|
|
|
'apps.apps.MyAuth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
'django.contrib.sessions',
|
|
|
|
'django.contrib.messages',
|
|
|
|
'django.contrib.staticfiles',
|
|
|
|
]
|
|
|
|
|
|
|
|
SOME_INSTALLED_APPS_NAMES = [
|
|
|
|
'django.contrib.admin',
|
|
|
|
'django.contrib.auth',
|
|
|
|
] + SOME_INSTALLED_APPS[2:]
|
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
HERE = os.path.dirname(__file__)
|
2014-01-26 10:37:05 +08:00
|
|
|
|
2013-12-27 02:20:38 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AppsTests(SimpleTestCase):
|
2013-12-24 19:25:17 +08:00
|
|
|
|
2013-12-26 22:04:58 +08:00
|
|
|
def test_singleton_master(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Only one master registry can exist.
|
2013-12-26 22:04:58 +08:00
|
|
|
"""
|
|
|
|
with self.assertRaises(RuntimeError):
|
2013-12-30 23:03:06 +08:00
|
|
|
Apps(installed_apps=None)
|
2013-12-26 22:04:58 +08:00
|
|
|
|
|
|
|
def test_ready(self):
|
|
|
|
"""
|
|
|
|
Tests the ready property of the master registry.
|
|
|
|
"""
|
|
|
|
# The master app registry is always ready when the tests run.
|
2018-01-30 23:44:58 +08:00
|
|
|
self.assertIs(apps.ready, True)
|
2013-12-30 23:03:06 +08:00
|
|
|
# Non-master app registries are populated in __init__.
|
2018-01-30 23:44:58 +08:00
|
|
|
self.assertIs(Apps().ready, True)
|
2019-01-14 09:33:47 +08:00
|
|
|
# The condition is set when apps are ready
|
|
|
|
self.assertIs(apps.ready_event.is_set(), True)
|
|
|
|
self.assertIs(Apps().ready_event.is_set(), True)
|
2013-12-26 22:04:58 +08:00
|
|
|
|
2013-12-27 02:20:38 +08:00
|
|
|
def test_bad_app_config(self):
|
|
|
|
"""
|
|
|
|
Tests when INSTALLED_APPS contains an incorrect app config.
|
|
|
|
"""
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "'apps.apps.BadConfig' must supply a name attribute."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2013-12-27 02:20:38 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.BadConfig']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_not_an_app_config(self):
|
|
|
|
"""
|
|
|
|
Tests when INSTALLED_APPS contains a class that isn't an app config.
|
|
|
|
"""
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "'apps.apps.NotAConfig' isn't a subclass of AppConfig."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2013-12-27 02:20:38 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_no_such_app(self):
|
|
|
|
"""
|
2013-12-27 02:29:32 +08:00
|
|
|
Tests when INSTALLED_APPS contains an app that doesn't exist, either
|
|
|
|
directly or via an app config.
|
2013-12-27 02:20:38 +08:00
|
|
|
"""
|
2013-12-27 02:29:32 +08:00
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
with self.settings(INSTALLED_APPS=['there is no such app']):
|
|
|
|
pass
|
2016-06-02 04:08:59 +08:00
|
|
|
msg = "Cannot import 'there is no such app'. Check that 'apps.apps.NoSuchApp.name' is correct."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2013-12-27 02:20:38 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.NoSuchApp']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_no_such_app_config(self):
|
2020-07-21 16:35:12 +08:00
|
|
|
msg = "Module 'apps' does not contain a 'NoSuchConfig' class."
|
2018-09-19 00:19:18 +08:00
|
|
|
with self.assertRaisesMessage(ImportError, msg):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.NoSuchConfig']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_no_such_app_config_with_choices(self):
|
|
|
|
msg = (
|
2020-07-21 16:35:12 +08:00
|
|
|
"Module 'apps.apps' does not contain a 'NoSuchConfig' class. "
|
2020-07-12 20:59:57 +08:00
|
|
|
"Choices are: 'BadConfig', 'ModelPKAppsConfig', 'MyAdmin', "
|
|
|
|
"'MyAuth', 'NoSuchApp', 'PlainAppsConfig', 'RelabeledAppsConfig'."
|
2018-09-19 00:19:18 +08:00
|
|
|
)
|
2020-07-21 16:35:12 +08:00
|
|
|
with self.assertRaisesMessage(ImportError, msg):
|
2013-12-27 02:20:38 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.NoSuchConfig']):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 16:35:12 +08:00
|
|
|
def test_no_config_app(self):
|
|
|
|
"""Load an app that doesn't provide an AppConfig class."""
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.no_config_app']):
|
|
|
|
config = apps.get_app_config('no_config_app')
|
|
|
|
self.assertIsInstance(config, AppConfig)
|
|
|
|
|
|
|
|
def test_one_config_app(self):
|
|
|
|
"""Load an app that provides an AppConfig class."""
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.one_config_app']):
|
|
|
|
config = apps.get_app_config('one_config_app')
|
|
|
|
self.assertIsInstance(config, OneConfig)
|
|
|
|
|
|
|
|
def test_two_configs_app(self):
|
|
|
|
"""Load an app that provides two AppConfig classes."""
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.two_configs_app']):
|
|
|
|
config = apps.get_app_config('two_configs_app')
|
|
|
|
self.assertIsInstance(config, AppConfig)
|
|
|
|
|
|
|
|
def test_two_default_configs_app(self):
|
|
|
|
"""Load an app that provides two default AppConfig classes."""
|
|
|
|
msg = (
|
|
|
|
"'apps.two_default_configs_app.apps' declares more than one "
|
|
|
|
"default AppConfig: 'TwoConfig', 'TwoConfigBis'."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RuntimeError, msg):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.two_default_configs_app']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_two_configs_one_default_app(self):
|
|
|
|
"""
|
|
|
|
Load an app that provides two AppConfig classes, one being the default.
|
|
|
|
"""
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.two_configs_one_default_app']):
|
|
|
|
config = apps.get_app_config('two_configs_one_default_app')
|
|
|
|
self.assertIsInstance(config, TwoConfig)
|
2014-01-25 05:43:00 +08:00
|
|
|
|
2013-12-27 01:03:17 +08:00
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
|
|
|
def test_get_app_configs(self):
|
|
|
|
"""
|
2014-01-26 19:46:28 +08:00
|
|
|
Tests apps.get_app_configs().
|
2013-12-27 01:03:17 +08:00
|
|
|
"""
|
|
|
|
app_configs = apps.get_app_configs()
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual([app_config.name for app_config in app_configs], SOME_INSTALLED_APPS_NAMES)
|
2013-12-27 01:03:17 +08:00
|
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
|
|
|
def test_get_app_config(self):
|
|
|
|
"""
|
2014-01-26 19:46:28 +08:00
|
|
|
Tests apps.get_app_config().
|
2013-12-27 01:03:17 +08:00
|
|
|
"""
|
|
|
|
app_config = apps.get_app_config('admin')
|
|
|
|
self.assertEqual(app_config.name, 'django.contrib.admin')
|
|
|
|
|
|
|
|
app_config = apps.get_app_config('staticfiles')
|
|
|
|
self.assertEqual(app_config.name, 'django.contrib.staticfiles')
|
|
|
|
|
|
|
|
with self.assertRaises(LookupError):
|
2015-08-17 21:01:59 +08:00
|
|
|
apps.get_app_config('admindocs')
|
2013-12-27 01:03:17 +08:00
|
|
|
|
2015-05-12 17:02:23 +08:00
|
|
|
msg = "No installed app with label 'django.contrib.auth'. Did you mean 'myauth'"
|
|
|
|
with self.assertRaisesMessage(LookupError, msg):
|
|
|
|
apps.get_app_config('django.contrib.auth')
|
|
|
|
|
2013-12-27 03:04:06 +08:00
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
2014-01-07 05:48:41 +08:00
|
|
|
def test_is_installed(self):
|
2014-01-26 19:46:28 +08:00
|
|
|
"""
|
|
|
|
Tests apps.is_installed().
|
|
|
|
"""
|
2018-01-30 23:44:58 +08:00
|
|
|
self.assertIs(apps.is_installed('django.contrib.admin'), True)
|
|
|
|
self.assertIs(apps.is_installed('django.contrib.auth'), True)
|
|
|
|
self.assertIs(apps.is_installed('django.contrib.staticfiles'), True)
|
|
|
|
self.assertIs(apps.is_installed('django.contrib.admindocs'), False)
|
2013-12-27 03:04:06 +08:00
|
|
|
|
2014-01-26 19:46:28 +08:00
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
|
|
|
def test_get_model(self):
|
|
|
|
"""
|
|
|
|
Tests apps.get_model().
|
|
|
|
"""
|
|
|
|
self.assertEqual(apps.get_model('admin', 'LogEntry'), LogEntry)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model('admin', 'LogExit')
|
|
|
|
|
|
|
|
# App label is case-sensitive, Model name is case-insensitive.
|
|
|
|
self.assertEqual(apps.get_model('admin', 'loGentrY'), LogEntry)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model('Admin', 'LogEntry')
|
|
|
|
|
|
|
|
# A single argument is accepted.
|
|
|
|
self.assertEqual(apps.get_model('admin.LogEntry'), LogEntry)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model('admin.LogExit')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
apps.get_model('admin_LogEntry')
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig'])
|
|
|
|
def test_relabeling(self):
|
|
|
|
self.assertEqual(apps.get_app_config('relabeled').name, 'apps')
|
|
|
|
|
2014-01-01 00:25:57 +08:00
|
|
|
def test_duplicate_labels(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, "Application labels aren't unique"):
|
2014-01-01 00:25:57 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.PlainAppsConfig', 'apps']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_duplicate_names(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, "Application names aren't unique"):
|
2014-01-01 00:25:57 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig', 'apps']):
|
|
|
|
pass
|
|
|
|
|
2014-09-01 00:51:55 +08:00
|
|
|
def test_import_exception_is_not_masked(self):
|
|
|
|
"""
|
|
|
|
App discovery should preserve stack traces. Regression test for #22920.
|
|
|
|
"""
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(ImportError, "Oops"):
|
2015-02-04 07:02:59 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['import_error_package']):
|
2014-09-01 00:51:55 +08:00
|
|
|
pass
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
def test_models_py(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The models in the models.py file were loaded correctly.
|
2013-12-24 19:25:17 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
|
2013-12-28 21:55:54 +08:00
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model("apps", "SoAlternative")
|
2013-12-24 19:25:17 +08:00
|
|
|
|
2013-12-28 21:55:54 +08:00
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
new_apps.get_model("apps", "TotallyNormal")
|
2013-12-24 19:25:17 +08:00
|
|
|
self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)
|
|
|
|
|
2018-01-30 23:44:58 +08:00
|
|
|
def test_models_not_loaded(self):
|
|
|
|
"""
|
|
|
|
apps.get_models() raises an exception if apps.models_ready isn't True.
|
|
|
|
"""
|
|
|
|
apps.models_ready = False
|
|
|
|
try:
|
|
|
|
# The cache must be cleared to trigger the exception.
|
|
|
|
apps.get_models.cache_clear()
|
|
|
|
with self.assertRaisesMessage(AppRegistryNotReady, "Models aren't loaded yet."):
|
|
|
|
apps.get_models()
|
|
|
|
finally:
|
|
|
|
apps.models_ready = True
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
def test_dynamic_load(self):
|
|
|
|
"""
|
|
|
|
Makes a new model at runtime and ensures it goes into the right place.
|
|
|
|
"""
|
2013-12-30 04:47:55 +08:00
|
|
|
old_models = list(apps.get_app_config("apps").get_models())
|
2013-12-24 19:25:17 +08:00
|
|
|
# Construct a new model in a new app registry
|
|
|
|
body = {}
|
2013-12-30 23:03:06 +08:00
|
|
|
new_apps = Apps(["apps"])
|
2013-12-24 19:25:17 +08:00
|
|
|
meta_contents = {
|
|
|
|
'app_label': "apps",
|
|
|
|
'apps': new_apps,
|
|
|
|
}
|
2017-06-02 07:08:59 +08:00
|
|
|
meta = type("Meta", (), meta_contents)
|
2013-12-24 19:25:17 +08:00
|
|
|
body['Meta'] = meta
|
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2017-01-19 22:48:01 +08:00
|
|
|
temp_model = type("SouthPonies", (models.Model,), body)
|
2013-12-24 19:25:17 +08:00
|
|
|
# Make sure it appeared in the right place!
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(list(apps.get_app_config("apps").get_models()), old_models)
|
2013-12-28 21:55:54 +08:00
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model("apps", "SouthPonies")
|
2013-12-24 19:25:17 +08:00
|
|
|
self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)
|
2014-01-26 10:37:05 +08:00
|
|
|
|
2014-10-23 00:16:32 +08:00
|
|
|
def test_model_clash(self):
|
|
|
|
"""
|
|
|
|
Test for behavior when two models clash in the app registry.
|
|
|
|
"""
|
|
|
|
new_apps = Apps(["apps"])
|
|
|
|
meta_contents = {
|
|
|
|
'app_label': "apps",
|
|
|
|
'apps': new_apps,
|
|
|
|
}
|
|
|
|
|
|
|
|
body = {}
|
2017-06-02 07:08:59 +08:00
|
|
|
body['Meta'] = type("Meta", (), meta_contents)
|
2014-10-23 00:16:32 +08:00
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2017-01-19 22:48:01 +08:00
|
|
|
type("SouthPonies", (models.Model,), body)
|
2014-10-23 00:16:32 +08:00
|
|
|
|
|
|
|
# When __name__ and __module__ match we assume the module
|
|
|
|
# was reloaded and issue a warning. This use-case is
|
|
|
|
# useful for REPL. Refs #23621.
|
|
|
|
body = {}
|
2017-06-02 07:08:59 +08:00
|
|
|
body['Meta'] = type("Meta", (), meta_contents)
|
2014-10-23 00:16:32 +08:00
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2015-11-14 04:54:05 +08:00
|
|
|
msg = (
|
|
|
|
"Model 'apps.southponies' was already registered. "
|
|
|
|
"Reloading models is not advised as it can lead to inconsistencies, "
|
|
|
|
"most notably with related models."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RuntimeWarning, msg):
|
2017-01-19 22:48:01 +08:00
|
|
|
type("SouthPonies", (models.Model,), body)
|
2014-10-23 00:16:32 +08:00
|
|
|
|
|
|
|
# If it doesn't appear to be a reloaded module then we expect
|
|
|
|
# a RuntimeError.
|
|
|
|
body = {}
|
2017-06-02 07:08:59 +08:00
|
|
|
body['Meta'] = type("Meta", (), meta_contents)
|
2014-10-23 00:16:32 +08:00
|
|
|
body['__module__'] = TotallyNormal.__module__ + '.whatever'
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.assertRaisesMessage(RuntimeError, "Conflicting 'southponies' models in application 'apps':"):
|
2017-01-19 22:48:01 +08:00
|
|
|
type("SouthPonies", (models.Model,), body)
|
2014-01-26 10:37:05 +08:00
|
|
|
|
2015-02-09 02:38:09 +08:00
|
|
|
def test_get_containing_app_config_apps_not_ready(self):
|
|
|
|
"""
|
|
|
|
apps.get_containing_app_config() should raise an exception if
|
|
|
|
apps.apps_ready isn't True.
|
|
|
|
"""
|
|
|
|
apps.apps_ready = False
|
|
|
|
try:
|
|
|
|
with self.assertRaisesMessage(AppRegistryNotReady, "Apps aren't loaded yet"):
|
|
|
|
apps.get_containing_app_config('foo')
|
|
|
|
finally:
|
|
|
|
apps.apps_ready = True
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('apps', kwarg_name='apps')
|
|
|
|
def test_lazy_model_operation(self, apps):
|
2015-03-03 16:43:56 +08:00
|
|
|
"""
|
|
|
|
Tests apps.lazy_model_operation().
|
|
|
|
"""
|
|
|
|
model_classes = []
|
|
|
|
initial_pending = set(apps._pending_operations)
|
|
|
|
|
|
|
|
def test_func(*models):
|
|
|
|
model_classes[:] = models
|
|
|
|
|
|
|
|
class LazyA(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Test models appearing twice, and models appearing consecutively
|
|
|
|
model_keys = [('apps', model_name) for model_name in ['lazya', 'lazyb', 'lazyb', 'lazyc', 'lazya']]
|
|
|
|
apps.lazy_model_operation(test_func, *model_keys)
|
|
|
|
|
|
|
|
# LazyModelA shouldn't be waited on since it's already registered,
|
|
|
|
# and LazyModelC shouldn't be waited on until LazyModelB exists.
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')})
|
2015-03-03 16:43:56 +08:00
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Multiple operations can wait on the same model
|
2015-03-03 16:43:56 +08:00
|
|
|
apps.lazy_model_operation(test_func, ('apps', 'lazyb'))
|
|
|
|
|
|
|
|
class LazyB(models.Model):
|
|
|
|
pass
|
|
|
|
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(model_classes, [LazyB])
|
2015-03-03 16:43:56 +08:00
|
|
|
|
|
|
|
# Now we are just waiting on LazyModelC.
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')})
|
2015-03-03 16:43:56 +08:00
|
|
|
|
|
|
|
class LazyC(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Everything should be loaded - make sure the callback was executed properly.
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
|
2015-03-03 16:43:56 +08:00
|
|
|
|
2014-10-23 09:21:02 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Stub:
|
2014-01-28 04:28:53 +08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.__dict__.update(kwargs)
|
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AppConfigTests(SimpleTestCase):
|
2014-01-28 04:28:53 +08:00
|
|
|
"""Unit tests for AppConfig class."""
|
|
|
|
def test_path_set_explicitly(self):
|
|
|
|
"""If subclass sets path as class attr, no module attributes needed."""
|
|
|
|
class MyAppConfig(AppConfig):
|
|
|
|
path = 'foo'
|
|
|
|
|
|
|
|
ac = MyAppConfig('label', Stub())
|
|
|
|
|
|
|
|
self.assertEqual(ac.path, 'foo')
|
|
|
|
|
|
|
|
def test_explicit_path_overrides(self):
|
|
|
|
"""If path set as class attr, overrides __path__ and __file__."""
|
|
|
|
class MyAppConfig(AppConfig):
|
|
|
|
path = 'foo'
|
|
|
|
|
|
|
|
ac = MyAppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))
|
|
|
|
|
|
|
|
self.assertEqual(ac.path, 'foo')
|
|
|
|
|
|
|
|
def test_dunder_path(self):
|
|
|
|
"""If single element in __path__, use it (in preference to __file__)."""
|
|
|
|
ac = AppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))
|
|
|
|
|
|
|
|
self.assertEqual(ac.path, 'a')
|
|
|
|
|
|
|
|
def test_no_dunder_path_fallback_to_dunder_file(self):
|
|
|
|
"""If there is no __path__ attr, use __file__."""
|
|
|
|
ac = AppConfig('label', Stub(__file__='b/__init__.py'))
|
|
|
|
|
|
|
|
self.assertEqual(ac.path, 'b')
|
|
|
|
|
|
|
|
def test_empty_dunder_path_fallback_to_dunder_file(self):
|
|
|
|
"""If the __path__ attr is empty, use __file__ if set."""
|
|
|
|
ac = AppConfig('label', Stub(__path__=[], __file__='b/__init__.py'))
|
|
|
|
|
|
|
|
self.assertEqual(ac.path, 'b')
|
|
|
|
|
|
|
|
def test_multiple_dunder_path_fallback_to_dunder_file(self):
|
|
|
|
"""If the __path__ attr is length>1, use __file__ if set."""
|
|
|
|
ac = AppConfig('label', Stub(__path__=['a', 'b'], __file__='c/__init__.py'))
|
|
|
|
|
|
|
|
self.assertEqual(ac.path, 'c')
|
|
|
|
|
|
|
|
def test_no_dunder_path_or_dunder_file(self):
|
|
|
|
"""If there is no __path__ or __file__, raise ImproperlyConfigured."""
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
AppConfig('label', Stub())
|
|
|
|
|
|
|
|
def test_empty_dunder_path_no_dunder_file(self):
|
|
|
|
"""If the __path__ attr is empty and there is no __file__, raise."""
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
AppConfig('label', Stub(__path__=[]))
|
|
|
|
|
|
|
|
def test_multiple_dunder_path_no_dunder_file(self):
|
|
|
|
"""If the __path__ attr is length>1 and there is no __file__, raise."""
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
AppConfig('label', Stub(__path__=['a', 'b']))
|
|
|
|
|
2015-08-24 08:07:00 +08:00
|
|
|
def test_duplicate_dunder_path_no_dunder_file(self):
|
|
|
|
"""
|
|
|
|
If the __path__ attr contains duplicate paths and there is no
|
|
|
|
__file__, they duplicates should be deduplicated (#25246).
|
|
|
|
"""
|
|
|
|
ac = AppConfig('label', Stub(__path__=['a', 'a']))
|
|
|
|
self.assertEqual(ac.path, 'a')
|
|
|
|
|
2017-01-27 06:16:13 +08:00
|
|
|
def test_repr(self):
|
|
|
|
ac = AppConfig('label', Stub(__path__=['a']))
|
|
|
|
self.assertEqual(repr(ac), '<AppConfig: label>')
|
|
|
|
|
2020-07-12 20:59:57 +08:00
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=['apps.apps.ModelPKAppsConfig'],
|
|
|
|
DEFAULT_AUTO_FIELD='django.db.models.SmallAutoField',
|
|
|
|
)
|
|
|
|
def test_app_default_auto_field(self):
|
|
|
|
apps_config = apps.get_app_config('apps')
|
|
|
|
self.assertEqual(
|
|
|
|
apps_config.default_auto_field,
|
|
|
|
'django.db.models.BigAutoField',
|
|
|
|
)
|
|
|
|
self.assertIs(apps_config._is_default_auto_field_overridden, True)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=['apps.apps.PlainAppsConfig'],
|
|
|
|
DEFAULT_AUTO_FIELD='django.db.models.SmallAutoField',
|
|
|
|
)
|
|
|
|
def test_default_auto_field_setting(self):
|
|
|
|
apps_config = apps.get_app_config('apps')
|
|
|
|
self.assertEqual(
|
|
|
|
apps_config.default_auto_field,
|
|
|
|
'django.db.models.SmallAutoField',
|
|
|
|
)
|
|
|
|
self.assertIs(apps_config._is_default_auto_field_overridden, False)
|
|
|
|
|
2014-01-28 04:28:53 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class NamespacePackageAppTests(SimpleTestCase):
|
2014-01-26 10:37:05 +08:00
|
|
|
# We need nsapp to be top-level so our multiple-paths tests can add another
|
|
|
|
# location for it (if its inside a normal package with an __init__.py that
|
|
|
|
# isn't possible). In order to avoid cluttering the already-full tests/ dir
|
|
|
|
# (which is on sys.path), we add these new entries to sys.path temporarily.
|
|
|
|
base_location = os.path.join(HERE, 'namespace_package_base')
|
|
|
|
other_location = os.path.join(HERE, 'namespace_package_other_base')
|
|
|
|
app_path = os.path.join(base_location, 'nsapp')
|
|
|
|
|
|
|
|
def test_single_path(self):
|
|
|
|
"""
|
|
|
|
A Py3.3+ namespace package can be an app if it has only one path.
|
|
|
|
"""
|
2014-01-26 13:50:40 +08:00
|
|
|
with extend_sys_path(self.base_location):
|
2014-01-26 10:37:05 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['nsapp']):
|
|
|
|
app_config = apps.get_app_config('nsapp')
|
2017-01-20 21:01:02 +08:00
|
|
|
self.assertEqual(app_config.path, self.app_path)
|
2014-01-26 10:37:05 +08:00
|
|
|
|
|
|
|
def test_multiple_paths(self):
|
|
|
|
"""
|
|
|
|
A Py3.3+ namespace package with multiple locations cannot be an app.
|
|
|
|
|
|
|
|
(Because then we wouldn't know where to load its templates, static
|
2016-01-11 00:48:16 +08:00
|
|
|
assets, etc. from.)
|
2014-01-26 10:37:05 +08:00
|
|
|
"""
|
|
|
|
# Temporarily add two directories to sys.path that both contain
|
|
|
|
# components of the "nsapp" package.
|
2014-01-26 13:50:40 +08:00
|
|
|
with extend_sys_path(self.base_location, self.other_location):
|
2014-01-26 10:37:05 +08:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
with self.settings(INSTALLED_APPS=['nsapp']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_multiple_paths_explicit_path(self):
|
|
|
|
"""
|
|
|
|
Multiple locations are ok only if app-config has explicit path.
|
|
|
|
"""
|
|
|
|
# Temporarily add two directories to sys.path that both contain
|
|
|
|
# components of the "nsapp" package.
|
2014-01-26 13:50:40 +08:00
|
|
|
with extend_sys_path(self.base_location, self.other_location):
|
2014-01-26 10:37:05 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['nsapp.apps.NSAppConfig']):
|
|
|
|
app_config = apps.get_app_config('nsapp')
|
2017-01-20 21:01:02 +08:00
|
|
|
self.assertEqual(app_config.path, self.app_path)
|
2020-07-21 16:35:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DeprecationTests(SimpleTestCase):
|
|
|
|
@ignore_warnings(category=RemovedInDjango41Warning)
|
|
|
|
def test_explicit_default_app_config(self):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_app']):
|
|
|
|
config = apps.get_app_config('explicit_default_config_app')
|
|
|
|
self.assertIsInstance(config, ExplicitDefaultConfig)
|
|
|
|
|
|
|
|
def test_explicit_default_app_config_warning(self):
|
|
|
|
"""
|
|
|
|
Load an app that specifies a default AppConfig class matching the
|
|
|
|
autodetected one.
|
|
|
|
"""
|
|
|
|
msg = (
|
|
|
|
"'apps.explicit_default_config_app' defines default_app_config = "
|
|
|
|
"'apps.explicit_default_config_app.apps.ExplicitDefaultConfig'. "
|
|
|
|
"Django now detects this configuration automatically. You can "
|
|
|
|
"remove default_app_config."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_app']):
|
2020-08-11 16:31:09 +08:00
|
|
|
pass
|
|
|
|
with ignore_warnings(category=RemovedInDjango41Warning):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_app']):
|
|
|
|
self.assertIsInstance(
|
|
|
|
apps.get_app_config('explicit_default_config_app'),
|
|
|
|
ExplicitDefaultConfig,
|
|
|
|
)
|
2020-07-21 16:35:12 +08:00
|
|
|
|
|
|
|
def test_explicit_default_app_config_mismatch(self):
|
|
|
|
"""
|
|
|
|
Load an app that specifies a default AppConfig class not matching the
|
|
|
|
autodetected one.
|
|
|
|
"""
|
|
|
|
msg = (
|
|
|
|
"'apps.explicit_default_config_mismatch_app' defines "
|
|
|
|
"default_app_config = 'apps.explicit_default_config_mismatch_app."
|
|
|
|
"not_apps.ExplicitDefaultConfigMismatch'. However, Django's "
|
|
|
|
"automatic detection picked another configuration, 'apps."
|
|
|
|
"explicit_default_config_mismatch_app.apps."
|
|
|
|
"ImplicitDefaultConfigMismatch'. You should move the default "
|
|
|
|
"config class to the apps submodule of your application and, if "
|
|
|
|
"this module defines several config classes, mark the default one "
|
|
|
|
"with default = True."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_mismatch_app']):
|
2020-08-11 16:31:09 +08:00
|
|
|
pass
|
|
|
|
with ignore_warnings(category=RemovedInDjango41Warning):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_mismatch_app']):
|
|
|
|
self.assertIsInstance(
|
|
|
|
apps.get_app_config('explicit_default_config_mismatch_app'),
|
|
|
|
ExplicitDefaultConfigMismatch,
|
|
|
|
)
|
2020-08-11 02:16:45 +08:00
|
|
|
|
|
|
|
def test_explicit_default_app_config_empty_apps(self):
|
|
|
|
"""
|
|
|
|
Load an app that specifies a default AppConfig class in __init__ and
|
|
|
|
have an empty apps module.
|
|
|
|
"""
|
|
|
|
msg = (
|
|
|
|
"'apps.explicit_default_config_empty_apps' defines "
|
|
|
|
"default_app_config = 'apps.explicit_default_config_empty_apps."
|
|
|
|
"ExplicitDefaultConfigEmptyApps'. However, Django's automatic "
|
|
|
|
"detection did not find this configuration. You should move the "
|
|
|
|
"default config class to the apps submodule of your application "
|
|
|
|
"and, if this module defines several config classes, mark the "
|
|
|
|
"default one with default = True."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_empty_apps']):
|
|
|
|
pass
|
|
|
|
with ignore_warnings(category=RemovedInDjango41Warning):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_empty_apps']):
|
|
|
|
self.assertIsInstance(
|
|
|
|
apps.get_app_config('explicit_default_config_empty_apps'),
|
|
|
|
ExplicitDefaultConfigEmptyApps,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_explicit_default_app_config_without_apps(self):
|
|
|
|
"""
|
|
|
|
Load an app that specifies a default AppConfig class in __init__ and do
|
|
|
|
not have an apps module.
|
|
|
|
"""
|
|
|
|
msg = (
|
|
|
|
"'apps.explicit_default_config_without_apps' defines "
|
|
|
|
"default_app_config = 'apps.explicit_default_config_without_apps."
|
|
|
|
"ExplicitDefaultConfigWithoutApps'. However, Django's automatic "
|
|
|
|
"detection did not find this configuration. You should move the "
|
|
|
|
"default config class to the apps submodule of your application "
|
|
|
|
"and, if this module defines several config classes, mark the "
|
|
|
|
"default one with default = True."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_without_apps']):
|
|
|
|
pass
|
|
|
|
with ignore_warnings(category=RemovedInDjango41Warning):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_without_apps']):
|
|
|
|
self.assertIsInstance(
|
|
|
|
apps.get_app_config('explicit_default_config_without_apps'),
|
|
|
|
ExplicitDefaultConfigWithoutApps,
|
|
|
|
)
|