2014-01-26 02:37:05 +00:00
|
|
|
import os
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.apps import AppConfig, apps
|
2013-12-24 11:25:17 +00:00
|
|
|
from django.apps.registry import Apps
|
2014-01-26 11:46:28 +00:00
|
|
|
from django.contrib.admin.models import LogEntry
|
2015-02-08 18:38:09 +00:00
|
|
|
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
|
2013-12-24 11:25:17 +00:00
|
|
|
from django.db import models
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-11-17 05:39:28 +00:00
|
|
|
from django.test.utils import extend_sys_path, isolate_apps
|
2013-12-24 11:25:17 +00:00
|
|
|
|
2014-01-24 21:43:00 +00:00
|
|
|
from .default_config_app.apps import CustomConfig
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import SoAlternative, TotallyNormal, new_apps
|
2013-12-24 11:25:17 +00:00
|
|
|
|
2013-12-26 17:03:17 +00: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 13:01:02 +00:00
|
|
|
HERE = os.path.dirname(__file__)
|
2014-01-26 02:37:05 +00:00
|
|
|
|
2013-12-26 18:20:38 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class AppsTests(SimpleTestCase):
|
2013-12-24 11:25:17 +00:00
|
|
|
|
2013-12-26 14:04:58 +00:00
|
|
|
def test_singleton_master(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Only one master registry can exist.
|
2013-12-26 14:04:58 +00:00
|
|
|
"""
|
|
|
|
with self.assertRaises(RuntimeError):
|
2013-12-30 15:03:06 +00:00
|
|
|
Apps(installed_apps=None)
|
2013-12-26 14:04:58 +00:00
|
|
|
|
|
|
|
def test_ready(self):
|
|
|
|
"""
|
|
|
|
Tests the ready property of the master registry.
|
|
|
|
"""
|
|
|
|
# The master app registry is always ready when the tests run.
|
|
|
|
self.assertTrue(apps.ready)
|
2013-12-30 15:03:06 +00:00
|
|
|
# Non-master app registries are populated in __init__.
|
|
|
|
self.assertTrue(Apps().ready)
|
2013-12-26 14:04:58 +00:00
|
|
|
|
2013-12-26 18:20:38 +00:00
|
|
|
def test_bad_app_config(self):
|
|
|
|
"""
|
|
|
|
Tests when INSTALLED_APPS contains an incorrect app config.
|
|
|
|
"""
|
2017-05-28 19:37:21 +00:00
|
|
|
msg = "'apps.apps.BadConfig' must supply a name attribute."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2013-12-26 18:20:38 +00: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-28 19:37:21 +00:00
|
|
|
msg = "'apps.apps.NotAConfig' isn't a subclass of AppConfig."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2013-12-26 18:20:38 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_no_such_app(self):
|
|
|
|
"""
|
2013-12-26 18:29:32 +00:00
|
|
|
Tests when INSTALLED_APPS contains an app that doesn't exist, either
|
|
|
|
directly or via an app config.
|
2013-12-26 18:20:38 +00:00
|
|
|
"""
|
2013-12-26 18:29:32 +00:00
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
with self.settings(INSTALLED_APPS=['there is no such app']):
|
|
|
|
pass
|
2016-06-01 20:08:59 +00:00
|
|
|
msg = "Cannot import 'there is no such app'. Check that 'apps.apps.NoSuchApp.name' is correct."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2013-12-26 18:20:38 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.NoSuchApp']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_no_such_app_config(self):
|
|
|
|
"""
|
|
|
|
Tests when INSTALLED_APPS contains an entry that doesn't exist.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.NoSuchConfig']):
|
|
|
|
pass
|
|
|
|
|
2014-01-24 21:43:00 +00:00
|
|
|
def test_default_app_config(self):
|
|
|
|
with self.settings(INSTALLED_APPS=['apps.default_config_app']):
|
|
|
|
config = apps.get_app_config('default_config_app')
|
|
|
|
self.assertIsInstance(config, CustomConfig)
|
|
|
|
|
2013-12-26 17:03:17 +00:00
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
|
|
|
def test_get_app_configs(self):
|
|
|
|
"""
|
2014-01-26 11:46:28 +00:00
|
|
|
Tests apps.get_app_configs().
|
2013-12-26 17:03:17 +00:00
|
|
|
"""
|
|
|
|
app_configs = apps.get_app_configs()
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual([app_config.name for app_config in app_configs], SOME_INSTALLED_APPS_NAMES)
|
2013-12-26 17:03:17 +00:00
|
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
|
|
|
def test_get_app_config(self):
|
|
|
|
"""
|
2014-01-26 11:46:28 +00:00
|
|
|
Tests apps.get_app_config().
|
2013-12-26 17:03:17 +00: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 13:01:59 +00:00
|
|
|
apps.get_app_config('admindocs')
|
2013-12-26 17:03:17 +00:00
|
|
|
|
2015-05-12 09:02:23 +00: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-26 19:04:06 +00:00
|
|
|
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
|
2014-01-06 21:48:41 +00:00
|
|
|
def test_is_installed(self):
|
2014-01-26 11:46:28 +00:00
|
|
|
"""
|
|
|
|
Tests apps.is_installed().
|
|
|
|
"""
|
2014-01-06 21:48:41 +00:00
|
|
|
self.assertTrue(apps.is_installed('django.contrib.admin'))
|
2014-01-25 03:07:14 +00:00
|
|
|
self.assertTrue(apps.is_installed('django.contrib.auth'))
|
2014-01-06 21:48:41 +00:00
|
|
|
self.assertTrue(apps.is_installed('django.contrib.staticfiles'))
|
2015-08-17 13:01:59 +00:00
|
|
|
self.assertFalse(apps.is_installed('django.contrib.admindocs'))
|
2013-12-26 19:04:06 +00:00
|
|
|
|
2014-01-26 11:46:28 +00: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 15:23:42 +00:00
|
|
|
@override_settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig'])
|
|
|
|
def test_relabeling(self):
|
|
|
|
self.assertEqual(apps.get_app_config('relabeled').name, 'apps')
|
|
|
|
|
2013-12-31 16:25:57 +00:00
|
|
|
def test_duplicate_labels(self):
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, "Application labels aren't unique"):
|
2013-12-31 16:25:57 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.PlainAppsConfig', 'apps']):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_duplicate_names(self):
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, "Application names aren't unique"):
|
2013-12-31 16:25:57 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig', 'apps']):
|
|
|
|
pass
|
|
|
|
|
2014-08-31 16:51:55 +00:00
|
|
|
def test_import_exception_is_not_masked(self):
|
|
|
|
"""
|
|
|
|
App discovery should preserve stack traces. Regression test for #22920.
|
|
|
|
"""
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(ImportError, "Oops"):
|
2015-02-03 23:02:59 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['import_error_package']):
|
2014-08-31 16:51:55 +00:00
|
|
|
pass
|
|
|
|
|
2013-12-24 11:25:17 +00:00
|
|
|
def test_models_py(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The models in the models.py file were loaded correctly.
|
2013-12-24 11:25:17 +00:00
|
|
|
"""
|
|
|
|
self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
|
2013-12-28 13:55:54 +00:00
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model("apps", "SoAlternative")
|
2013-12-24 11:25:17 +00:00
|
|
|
|
2013-12-28 13:55:54 +00:00
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
new_apps.get_model("apps", "TotallyNormal")
|
2013-12-24 11:25:17 +00:00
|
|
|
self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)
|
|
|
|
|
|
|
|
def test_dynamic_load(self):
|
|
|
|
"""
|
|
|
|
Makes a new model at runtime and ensures it goes into the right place.
|
|
|
|
"""
|
2013-12-29 20:47:55 +00:00
|
|
|
old_models = list(apps.get_app_config("apps").get_models())
|
2013-12-24 11:25:17 +00:00
|
|
|
# Construct a new model in a new app registry
|
|
|
|
body = {}
|
2013-12-30 15:03:06 +00:00
|
|
|
new_apps = Apps(["apps"])
|
2013-12-24 11:25:17 +00:00
|
|
|
meta_contents = {
|
|
|
|
'app_label': "apps",
|
|
|
|
'apps': new_apps,
|
|
|
|
}
|
2017-06-01 23:08:59 +00:00
|
|
|
meta = type("Meta", (), meta_contents)
|
2013-12-24 11:25:17 +00:00
|
|
|
body['Meta'] = meta
|
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2017-01-19 14:48:01 +00:00
|
|
|
temp_model = type("SouthPonies", (models.Model,), body)
|
2013-12-24 11:25:17 +00:00
|
|
|
# Make sure it appeared in the right place!
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(list(apps.get_app_config("apps").get_models()), old_models)
|
2013-12-28 13:55:54 +00:00
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
apps.get_model("apps", "SouthPonies")
|
2013-12-24 11:25:17 +00:00
|
|
|
self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)
|
2014-01-26 02:37:05 +00:00
|
|
|
|
2014-10-22 16:16:32 +00: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-01 23:08:59 +00:00
|
|
|
body['Meta'] = type("Meta", (), meta_contents)
|
2014-10-22 16:16:32 +00:00
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2017-01-19 14:48:01 +00:00
|
|
|
type("SouthPonies", (models.Model,), body)
|
2014-10-22 16:16:32 +00: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-01 23:08:59 +00:00
|
|
|
body['Meta'] = type("Meta", (), meta_contents)
|
2014-10-22 16:16:32 +00:00
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2015-11-13 20:54:05 +00: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 14:48:01 +00:00
|
|
|
type("SouthPonies", (models.Model,), body)
|
2014-10-22 16:16:32 +00:00
|
|
|
|
|
|
|
# If it doesn't appear to be a reloaded module then we expect
|
|
|
|
# a RuntimeError.
|
|
|
|
body = {}
|
2017-06-01 23:08:59 +00:00
|
|
|
body['Meta'] = type("Meta", (), meta_contents)
|
2014-10-22 16:16:32 +00:00
|
|
|
body['__module__'] = TotallyNormal.__module__ + '.whatever'
|
2016-04-08 02:04:45 +00:00
|
|
|
with self.assertRaisesMessage(RuntimeError, "Conflicting 'southponies' models in application 'apps':"):
|
2017-01-19 14:48:01 +00:00
|
|
|
type("SouthPonies", (models.Model,), body)
|
2014-01-26 02:37:05 +00:00
|
|
|
|
2015-02-08 18:38:09 +00: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 05:39:28 +00:00
|
|
|
@isolate_apps('apps', kwarg_name='apps')
|
|
|
|
def test_lazy_model_operation(self, apps):
|
2015-03-03 08:43:56 +00: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 11:51:48 +00:00
|
|
|
self.assertEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')})
|
2015-03-03 08:43:56 +00:00
|
|
|
|
2016-10-27 07:53:39 +00:00
|
|
|
# Multiple operations can wait on the same model
|
2015-03-03 08:43:56 +00:00
|
|
|
apps.lazy_model_operation(test_func, ('apps', 'lazyb'))
|
|
|
|
|
|
|
|
class LazyB(models.Model):
|
|
|
|
pass
|
|
|
|
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(model_classes, [LazyB])
|
2015-03-03 08:43:56 +00:00
|
|
|
|
|
|
|
# Now we are just waiting on LazyModelC.
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')})
|
2015-03-03 08:43:56 +00:00
|
|
|
|
|
|
|
class LazyC(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Everything should be loaded - make sure the callback was executed properly.
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
|
2015-03-03 08:43:56 +00:00
|
|
|
|
2014-10-23 01:21:02 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Stub:
|
2014-01-27 20:28:53 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.__dict__.update(kwargs)
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class AppConfigTests(SimpleTestCase):
|
2014-01-27 20:28:53 +00: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 00:07:00 +00: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-26 22:16:13 +00:00
|
|
|
def test_repr(self):
|
|
|
|
ac = AppConfig('label', Stub(__path__=['a']))
|
|
|
|
self.assertEqual(repr(ac), '<AppConfig: label>')
|
|
|
|
|
2014-01-27 20:28:53 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class NamespacePackageAppTests(SimpleTestCase):
|
2014-01-26 02:37:05 +00: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 05:50:40 +00:00
|
|
|
with extend_sys_path(self.base_location):
|
2014-01-26 02:37:05 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['nsapp']):
|
|
|
|
app_config = apps.get_app_config('nsapp')
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertEqual(app_config.path, self.app_path)
|
2014-01-26 02:37:05 +00: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-10 16:48:16 +00:00
|
|
|
assets, etc. from.)
|
2014-01-26 02:37:05 +00:00
|
|
|
"""
|
|
|
|
# Temporarily add two directories to sys.path that both contain
|
|
|
|
# components of the "nsapp" package.
|
2014-01-26 05:50:40 +00:00
|
|
|
with extend_sys_path(self.base_location, self.other_location):
|
2014-01-26 02:37:05 +00: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 05:50:40 +00:00
|
|
|
with extend_sys_path(self.base_location, self.other_location):
|
2014-01-26 02:37:05 +00:00
|
|
|
with self.settings(INSTALLED_APPS=['nsapp.apps.NSAppConfig']):
|
|
|
|
app_config = apps.get_app_config('nsapp')
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertEqual(app_config.path, self.app_path)
|