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
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test.utils import extend_sys_path, isolate_apps
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils._os import upath
|
2013-12-24 19:25:17 +08:00
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
from .default_config_app.apps import CustomConfig
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import SoAlternative, TotallyNormal, new_apps
|
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:]
|
|
|
|
|
2015-02-15 08:47:07 +08:00
|
|
|
HERE = os.path.dirname(upath(__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.
|
|
|
|
self.assertTrue(apps.ready)
|
2013-12-30 23:03:06 +08:00
|
|
|
# Non-master app registries are populated in __init__.
|
|
|
|
self.assertTrue(Apps().ready)
|
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.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
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-25 05:43:00 +08: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-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()
|
|
|
|
self.assertListEqual(
|
|
|
|
[app_config.name for app_config in app_configs],
|
|
|
|
SOME_INSTALLED_APPS_NAMES)
|
|
|
|
|
|
|
|
@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().
|
|
|
|
"""
|
2014-01-07 05:48:41 +08:00
|
|
|
self.assertTrue(apps.is_installed('django.contrib.admin'))
|
2014-01-25 11:07:14 +08:00
|
|
|
self.assertTrue(apps.is_installed('django.contrib.auth'))
|
2014-01-07 05:48:41 +08:00
|
|
|
self.assertTrue(apps.is_installed('django.contrib.staticfiles'))
|
2015-08-17 21:01:59 +08:00
|
|
|
self.assertFalse(apps.is_installed('django.contrib.admindocs'))
|
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)
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2013-12-27 01:03:17 +08:00
|
|
|
meta = type(str("Meta"), tuple(), meta_contents)
|
2013-12-24 19:25:17 +08:00
|
|
|
body['Meta'] = meta
|
|
|
|
body['__module__'] = TotallyNormal.__module__
|
2013-12-27 01:03:17 +08:00
|
|
|
temp_model = type(str("SouthPonies"), (models.Model,), body)
|
2013-12-24 19:25:17 +08:00
|
|
|
# Make sure it appeared in the right place!
|
2013-12-30 04:47:55 +08:00
|
|
|
self.assertListEqual(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 = {}
|
|
|
|
body['Meta'] = type(str("Meta"), tuple(), meta_contents)
|
|
|
|
body['__module__'] = TotallyNormal.__module__
|
|
|
|
type(str("SouthPonies"), (models.Model,), body)
|
|
|
|
|
|
|
|
# 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 = {}
|
|
|
|
body['Meta'] = type(str("Meta"), tuple(), meta_contents)
|
|
|
|
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):
|
2014-10-23 00:16:32 +08:00
|
|
|
type(str("SouthPonies"), (models.Model,), body)
|
|
|
|
|
|
|
|
# If it doesn't appear to be a reloaded module then we expect
|
|
|
|
# a RuntimeError.
|
|
|
|
body = {}
|
|
|
|
body['Meta'] = type(str("Meta"), tuple(), meta_contents)
|
|
|
|
body['__module__'] = TotallyNormal.__module__ + '.whatever'
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.assertRaisesMessage(RuntimeError, "Conflicting 'southponies' models in application 'apps':"):
|
2014-10-23 00:16:32 +08:00
|
|
|
type(str("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.
|
|
|
|
self.assertSetEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')})
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self.assertListEqual(model_classes, [LazyB])
|
|
|
|
|
|
|
|
# Now we are just waiting on LazyModelC.
|
|
|
|
self.assertSetEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')})
|
|
|
|
|
|
|
|
class LazyC(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Everything should be loaded - make sure the callback was executed properly.
|
|
|
|
self.assertListEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
|
|
|
|
|
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')
|
|
|
|
|
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')
|
|
|
|
self.assertEqual(app_config.path, upath(self.app_path))
|
|
|
|
|
|
|
|
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')
|
|
|
|
self.assertEqual(app_config.path, upath(self.app_path))
|