1. import os
    
  2. 
    
  3. from django.apps import AppConfig, apps
    
  4. from django.apps.registry import Apps
    
  5. from django.contrib.admin.models import LogEntry
    
  6. from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
    
  7. from django.db import models
    
  8. from django.test import SimpleTestCase, override_settings
    
  9. from django.test.utils import extend_sys_path, isolate_apps
    
  10. 
    
  11. from .models import SoAlternative, TotallyNormal, new_apps
    
  12. from .one_config_app.apps import OneConfig
    
  13. from .two_configs_one_default_app.apps import TwoConfig
    
  14. 
    
  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. 
    
  18. SOME_INSTALLED_APPS = [
    
  19.     "apps.apps.MyAdmin",
    
  20.     "apps.apps.MyAuth",
    
  21.     "django.contrib.contenttypes",
    
  22.     "django.contrib.sessions",
    
  23.     "django.contrib.messages",
    
  24.     "django.contrib.staticfiles",
    
  25. ]
    
  26. 
    
  27. SOME_INSTALLED_APPS_NAMES = [
    
  28.     "django.contrib.admin",
    
  29.     "django.contrib.auth",
    
  30. ] + SOME_INSTALLED_APPS[2:]
    
  31. 
    
  32. HERE = os.path.dirname(__file__)
    
  33. 
    
  34. 
    
  35. class AppsTests(SimpleTestCase):
    
  36.     def test_singleton_main(self):
    
  37.         """
    
  38.         Only one main registry can exist.
    
  39.         """
    
  40.         with self.assertRaises(RuntimeError):
    
  41.             Apps(installed_apps=None)
    
  42. 
    
  43.     def test_ready(self):
    
  44.         """
    
  45.         Tests the ready property of the main registry.
    
  46.         """
    
  47.         # The main app registry is always ready when the tests run.
    
  48.         self.assertIs(apps.ready, True)
    
  49.         # Non-main app registries are populated in __init__.
    
  50.         self.assertIs(Apps().ready, True)
    
  51.         # The condition is set when apps are ready
    
  52.         self.assertIs(apps.ready_event.is_set(), True)
    
  53.         self.assertIs(Apps().ready_event.is_set(), True)
    
  54. 
    
  55.     def test_bad_app_config(self):
    
  56.         """
    
  57.         Tests when INSTALLED_APPS contains an incorrect app config.
    
  58.         """
    
  59.         msg = "'apps.apps.BadConfig' must supply a name attribute."
    
  60.         with self.assertRaisesMessage(ImproperlyConfigured, msg):
    
  61.             with self.settings(INSTALLED_APPS=["apps.apps.BadConfig"]):
    
  62.                 pass
    
  63. 
    
  64.     def test_not_an_app_config(self):
    
  65.         """
    
  66.         Tests when INSTALLED_APPS contains a class that isn't an app config.
    
  67.         """
    
  68.         msg = "'apps.apps.NotAConfig' isn't a subclass of AppConfig."
    
  69.         with self.assertRaisesMessage(ImproperlyConfigured, msg):
    
  70.             with self.settings(INSTALLED_APPS=["apps.apps.NotAConfig"]):
    
  71.                 pass
    
  72. 
    
  73.     def test_no_such_app(self):
    
  74.         """
    
  75.         Tests when INSTALLED_APPS contains an app that doesn't exist, either
    
  76.         directly or via an app config.
    
  77.         """
    
  78.         with self.assertRaises(ImportError):
    
  79.             with self.settings(INSTALLED_APPS=["there is no such app"]):
    
  80.                 pass
    
  81.         msg = (
    
  82.             "Cannot import 'there is no such app'. Check that "
    
  83.             "'apps.apps.NoSuchApp.name' is correct."
    
  84.         )
    
  85.         with self.assertRaisesMessage(ImproperlyConfigured, msg):
    
  86.             with self.settings(INSTALLED_APPS=["apps.apps.NoSuchApp"]):
    
  87.                 pass
    
  88. 
    
  89.     def test_no_such_app_config(self):
    
  90.         msg = "Module 'apps' does not contain a 'NoSuchConfig' class."
    
  91.         with self.assertRaisesMessage(ImportError, msg):
    
  92.             with self.settings(INSTALLED_APPS=["apps.NoSuchConfig"]):
    
  93.                 pass
    
  94. 
    
  95.     def test_no_such_app_config_with_choices(self):
    
  96.         msg = (
    
  97.             "Module 'apps.apps' does not contain a 'NoSuchConfig' class. "
    
  98.             "Choices are: 'BadConfig', 'ModelPKAppsConfig', 'MyAdmin', "
    
  99.             "'MyAuth', 'NoSuchApp', 'PlainAppsConfig', 'RelabeledAppsConfig'."
    
  100.         )
    
  101.         with self.assertRaisesMessage(ImportError, msg):
    
  102.             with self.settings(INSTALLED_APPS=["apps.apps.NoSuchConfig"]):
    
  103.                 pass
    
  104. 
    
  105.     def test_no_config_app(self):
    
  106.         """Load an app that doesn't provide an AppConfig class."""
    
  107.         with self.settings(INSTALLED_APPS=["apps.no_config_app"]):
    
  108.             config = apps.get_app_config("no_config_app")
    
  109.         self.assertIsInstance(config, AppConfig)
    
  110. 
    
  111.     def test_one_config_app(self):
    
  112.         """Load an app that provides an AppConfig class."""
    
  113.         with self.settings(INSTALLED_APPS=["apps.one_config_app"]):
    
  114.             config = apps.get_app_config("one_config_app")
    
  115.         self.assertIsInstance(config, OneConfig)
    
  116. 
    
  117.     def test_two_configs_app(self):
    
  118.         """Load an app that provides two AppConfig classes."""
    
  119.         with self.settings(INSTALLED_APPS=["apps.two_configs_app"]):
    
  120.             config = apps.get_app_config("two_configs_app")
    
  121.         self.assertIsInstance(config, AppConfig)
    
  122. 
    
  123.     def test_two_default_configs_app(self):
    
  124.         """Load an app that provides two default AppConfig classes."""
    
  125.         msg = (
    
  126.             "'apps.two_default_configs_app.apps' declares more than one "
    
  127.             "default AppConfig: 'TwoConfig', 'TwoConfigBis'."
    
  128.         )
    
  129.         with self.assertRaisesMessage(RuntimeError, msg):
    
  130.             with self.settings(INSTALLED_APPS=["apps.two_default_configs_app"]):
    
  131.                 pass
    
  132. 
    
  133.     def test_two_configs_one_default_app(self):
    
  134.         """
    
  135.         Load an app that provides two AppConfig classes, one being the default.
    
  136.         """
    
  137.         with self.settings(INSTALLED_APPS=["apps.two_configs_one_default_app"]):
    
  138.             config = apps.get_app_config("two_configs_one_default_app")
    
  139.         self.assertIsInstance(config, TwoConfig)
    
  140. 
    
  141.     @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    
  142.     def test_get_app_configs(self):
    
  143.         """
    
  144.         Tests apps.get_app_configs().
    
  145.         """
    
  146.         app_configs = apps.get_app_configs()
    
  147.         self.assertEqual(
    
  148.             [app_config.name for app_config in app_configs], SOME_INSTALLED_APPS_NAMES
    
  149.         )
    
  150. 
    
  151.     @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    
  152.     def test_get_app_config(self):
    
  153.         """
    
  154.         Tests apps.get_app_config().
    
  155.         """
    
  156.         app_config = apps.get_app_config("admin")
    
  157.         self.assertEqual(app_config.name, "django.contrib.admin")
    
  158. 
    
  159.         app_config = apps.get_app_config("staticfiles")
    
  160.         self.assertEqual(app_config.name, "django.contrib.staticfiles")
    
  161. 
    
  162.         with self.assertRaises(LookupError):
    
  163.             apps.get_app_config("admindocs")
    
  164. 
    
  165.         msg = "No installed app with label 'django.contrib.auth'. Did you mean 'myauth'"
    
  166.         with self.assertRaisesMessage(LookupError, msg):
    
  167.             apps.get_app_config("django.contrib.auth")
    
  168. 
    
  169.     @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    
  170.     def test_is_installed(self):
    
  171.         """
    
  172.         Tests apps.is_installed().
    
  173.         """
    
  174.         self.assertIs(apps.is_installed("django.contrib.admin"), True)
    
  175.         self.assertIs(apps.is_installed("django.contrib.auth"), True)
    
  176.         self.assertIs(apps.is_installed("django.contrib.staticfiles"), True)
    
  177.         self.assertIs(apps.is_installed("django.contrib.admindocs"), False)
    
  178. 
    
  179.     @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    
  180.     def test_get_model(self):
    
  181.         """
    
  182.         Tests apps.get_model().
    
  183.         """
    
  184.         self.assertEqual(apps.get_model("admin", "LogEntry"), LogEntry)
    
  185.         with self.assertRaises(LookupError):
    
  186.             apps.get_model("admin", "LogExit")
    
  187. 
    
  188.         # App label is case-sensitive, Model name is case-insensitive.
    
  189.         self.assertEqual(apps.get_model("admin", "loGentrY"), LogEntry)
    
  190.         with self.assertRaises(LookupError):
    
  191.             apps.get_model("Admin", "LogEntry")
    
  192. 
    
  193.         # A single argument is accepted.
    
  194.         self.assertEqual(apps.get_model("admin.LogEntry"), LogEntry)
    
  195.         with self.assertRaises(LookupError):
    
  196.             apps.get_model("admin.LogExit")
    
  197.         with self.assertRaises(ValueError):
    
  198.             apps.get_model("admin_LogEntry")
    
  199. 
    
  200.     @override_settings(INSTALLED_APPS=["apps.apps.RelabeledAppsConfig"])
    
  201.     def test_relabeling(self):
    
  202.         self.assertEqual(apps.get_app_config("relabeled").name, "apps")
    
  203. 
    
  204.     def test_duplicate_labels(self):
    
  205.         with self.assertRaisesMessage(
    
  206.             ImproperlyConfigured, "Application labels aren't unique"
    
  207.         ):
    
  208.             with self.settings(INSTALLED_APPS=["apps.apps.PlainAppsConfig", "apps"]):
    
  209.                 pass
    
  210. 
    
  211.     def test_duplicate_names(self):
    
  212.         with self.assertRaisesMessage(
    
  213.             ImproperlyConfigured, "Application names aren't unique"
    
  214.         ):
    
  215.             with self.settings(
    
  216.                 INSTALLED_APPS=["apps.apps.RelabeledAppsConfig", "apps"]
    
  217.             ):
    
  218.                 pass
    
  219. 
    
  220.     def test_import_exception_is_not_masked(self):
    
  221.         """
    
  222.         App discovery should preserve stack traces. Regression test for #22920.
    
  223.         """
    
  224.         with self.assertRaisesMessage(ImportError, "Oops"):
    
  225.             with self.settings(INSTALLED_APPS=["import_error_package"]):
    
  226.                 pass
    
  227. 
    
  228.     def test_models_py(self):
    
  229.         """
    
  230.         The models in the models.py file were loaded correctly.
    
  231.         """
    
  232.         self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
    
  233.         with self.assertRaises(LookupError):
    
  234.             apps.get_model("apps", "SoAlternative")
    
  235. 
    
  236.         with self.assertRaises(LookupError):
    
  237.             new_apps.get_model("apps", "TotallyNormal")
    
  238.         self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)
    
  239. 
    
  240.     def test_models_not_loaded(self):
    
  241.         """
    
  242.         apps.get_models() raises an exception if apps.models_ready isn't True.
    
  243.         """
    
  244.         apps.models_ready = False
    
  245.         try:
    
  246.             # The cache must be cleared to trigger the exception.
    
  247.             apps.get_models.cache_clear()
    
  248.             with self.assertRaisesMessage(
    
  249.                 AppRegistryNotReady, "Models aren't loaded yet."
    
  250.             ):
    
  251.                 apps.get_models()
    
  252.         finally:
    
  253.             apps.models_ready = True
    
  254. 
    
  255.     def test_dynamic_load(self):
    
  256.         """
    
  257.         Makes a new model at runtime and ensures it goes into the right place.
    
  258.         """
    
  259.         old_models = list(apps.get_app_config("apps").get_models())
    
  260.         # Construct a new model in a new app registry
    
  261.         body = {}
    
  262.         new_apps = Apps(["apps"])
    
  263.         meta_contents = {
    
  264.             "app_label": "apps",
    
  265.             "apps": new_apps,
    
  266.         }
    
  267.         meta = type("Meta", (), meta_contents)
    
  268.         body["Meta"] = meta
    
  269.         body["__module__"] = TotallyNormal.__module__
    
  270.         temp_model = type("SouthPonies", (models.Model,), body)
    
  271.         # Make sure it appeared in the right place!
    
  272.         self.assertEqual(list(apps.get_app_config("apps").get_models()), old_models)
    
  273.         with self.assertRaises(LookupError):
    
  274.             apps.get_model("apps", "SouthPonies")
    
  275.         self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)
    
  276. 
    
  277.     def test_model_clash(self):
    
  278.         """
    
  279.         Test for behavior when two models clash in the app registry.
    
  280.         """
    
  281.         new_apps = Apps(["apps"])
    
  282.         meta_contents = {
    
  283.             "app_label": "apps",
    
  284.             "apps": new_apps,
    
  285.         }
    
  286. 
    
  287.         body = {}
    
  288.         body["Meta"] = type("Meta", (), meta_contents)
    
  289.         body["__module__"] = TotallyNormal.__module__
    
  290.         type("SouthPonies", (models.Model,), body)
    
  291. 
    
  292.         # When __name__ and __module__ match we assume the module
    
  293.         # was reloaded and issue a warning. This use-case is
    
  294.         # useful for REPL. Refs #23621.
    
  295.         body = {}
    
  296.         body["Meta"] = type("Meta", (), meta_contents)
    
  297.         body["__module__"] = TotallyNormal.__module__
    
  298.         msg = (
    
  299.             "Model 'apps.southponies' was already registered. "
    
  300.             "Reloading models is not advised as it can lead to inconsistencies, "
    
  301.             "most notably with related models."
    
  302.         )
    
  303.         with self.assertRaisesMessage(RuntimeWarning, msg):
    
  304.             type("SouthPonies", (models.Model,), body)
    
  305. 
    
  306.         # If it doesn't appear to be a reloaded module then we expect
    
  307.         # a RuntimeError.
    
  308.         body = {}
    
  309.         body["Meta"] = type("Meta", (), meta_contents)
    
  310.         body["__module__"] = TotallyNormal.__module__ + ".whatever"
    
  311.         with self.assertRaisesMessage(
    
  312.             RuntimeError, "Conflicting 'southponies' models in application 'apps':"
    
  313.         ):
    
  314.             type("SouthPonies", (models.Model,), body)
    
  315. 
    
  316.     def test_get_containing_app_config_apps_not_ready(self):
    
  317.         """
    
  318.         apps.get_containing_app_config() should raise an exception if
    
  319.         apps.apps_ready isn't True.
    
  320.         """
    
  321.         apps.apps_ready = False
    
  322.         try:
    
  323.             with self.assertRaisesMessage(
    
  324.                 AppRegistryNotReady, "Apps aren't loaded yet"
    
  325.             ):
    
  326.                 apps.get_containing_app_config("foo")
    
  327.         finally:
    
  328.             apps.apps_ready = True
    
  329. 
    
  330.     @isolate_apps("apps", kwarg_name="apps")
    
  331.     def test_lazy_model_operation(self, apps):
    
  332.         """
    
  333.         Tests apps.lazy_model_operation().
    
  334.         """
    
  335.         model_classes = []
    
  336.         initial_pending = set(apps._pending_operations)
    
  337. 
    
  338.         def test_func(*models):
    
  339.             model_classes[:] = models
    
  340. 
    
  341.         class LazyA(models.Model):
    
  342.             pass
    
  343. 
    
  344.         # Test models appearing twice, and models appearing consecutively
    
  345.         model_keys = [
    
  346.             ("apps", model_name)
    
  347.             for model_name in ["lazya", "lazyb", "lazyb", "lazyc", "lazya"]
    
  348.         ]
    
  349.         apps.lazy_model_operation(test_func, *model_keys)
    
  350. 
    
  351.         # LazyModelA shouldn't be waited on since it's already registered,
    
  352.         # and LazyModelC shouldn't be waited on until LazyModelB exists.
    
  353.         self.assertEqual(
    
  354.             set(apps._pending_operations) - initial_pending, {("apps", "lazyb")}
    
  355.         )
    
  356. 
    
  357.         # Multiple operations can wait on the same model
    
  358.         apps.lazy_model_operation(test_func, ("apps", "lazyb"))
    
  359. 
    
  360.         class LazyB(models.Model):
    
  361.             pass
    
  362. 
    
  363.         self.assertEqual(model_classes, [LazyB])
    
  364. 
    
  365.         # Now we are just waiting on LazyModelC.
    
  366.         self.assertEqual(
    
  367.             set(apps._pending_operations) - initial_pending, {("apps", "lazyc")}
    
  368.         )
    
  369. 
    
  370.         class LazyC(models.Model):
    
  371.             pass
    
  372. 
    
  373.         # Everything should be loaded - make sure the callback was executed properly.
    
  374.         self.assertEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
    
  375. 
    
  376. 
    
  377. class Stub:
    
  378.     def __init__(self, **kwargs):
    
  379.         self.__dict__.update(kwargs)
    
  380. 
    
  381. 
    
  382. class AppConfigTests(SimpleTestCase):
    
  383.     """Unit tests for AppConfig class."""
    
  384. 
    
  385.     def test_path_set_explicitly(self):
    
  386.         """If subclass sets path as class attr, no module attributes needed."""
    
  387. 
    
  388.         class MyAppConfig(AppConfig):
    
  389.             path = "foo"
    
  390. 
    
  391.         ac = MyAppConfig("label", Stub())
    
  392. 
    
  393.         self.assertEqual(ac.path, "foo")
    
  394. 
    
  395.     def test_explicit_path_overrides(self):
    
  396.         """If path set as class attr, overrides __path__ and __file__."""
    
  397. 
    
  398.         class MyAppConfig(AppConfig):
    
  399.             path = "foo"
    
  400. 
    
  401.         ac = MyAppConfig("label", Stub(__path__=["a"], __file__="b/__init__.py"))
    
  402. 
    
  403.         self.assertEqual(ac.path, "foo")
    
  404. 
    
  405.     def test_dunder_path(self):
    
  406.         """If single element in __path__, use it (in preference to __file__)."""
    
  407.         ac = AppConfig("label", Stub(__path__=["a"], __file__="b/__init__.py"))
    
  408. 
    
  409.         self.assertEqual(ac.path, "a")
    
  410. 
    
  411.     def test_no_dunder_path_fallback_to_dunder_file(self):
    
  412.         """If there is no __path__ attr, use __file__."""
    
  413.         ac = AppConfig("label", Stub(__file__="b/__init__.py"))
    
  414. 
    
  415.         self.assertEqual(ac.path, "b")
    
  416. 
    
  417.     def test_empty_dunder_path_fallback_to_dunder_file(self):
    
  418.         """If the __path__ attr is empty, use __file__ if set."""
    
  419.         ac = AppConfig("label", Stub(__path__=[], __file__="b/__init__.py"))
    
  420. 
    
  421.         self.assertEqual(ac.path, "b")
    
  422. 
    
  423.     def test_multiple_dunder_path_fallback_to_dunder_file(self):
    
  424.         """If the __path__ attr is length>1, use __file__ if set."""
    
  425.         ac = AppConfig("label", Stub(__path__=["a", "b"], __file__="c/__init__.py"))
    
  426. 
    
  427.         self.assertEqual(ac.path, "c")
    
  428. 
    
  429.     def test_no_dunder_path_or_dunder_file(self):
    
  430.         """If there is no __path__ or __file__, raise ImproperlyConfigured."""
    
  431.         with self.assertRaises(ImproperlyConfigured):
    
  432.             AppConfig("label", Stub())
    
  433. 
    
  434.     def test_empty_dunder_path_no_dunder_file(self):
    
  435.         """If the __path__ attr is empty and there is no __file__, raise."""
    
  436.         with self.assertRaises(ImproperlyConfigured):
    
  437.             AppConfig("label", Stub(__path__=[]))
    
  438. 
    
  439.     def test_multiple_dunder_path_no_dunder_file(self):
    
  440.         """If the __path__ attr is length>1 and there is no __file__, raise."""
    
  441.         with self.assertRaises(ImproperlyConfigured):
    
  442.             AppConfig("label", Stub(__path__=["a", "b"]))
    
  443. 
    
  444.     def test_duplicate_dunder_path_no_dunder_file(self):
    
  445.         """
    
  446.         If the __path__ attr contains duplicate paths and there is no
    
  447.         __file__, they duplicates should be deduplicated (#25246).
    
  448.         """
    
  449.         ac = AppConfig("label", Stub(__path__=["a", "a"]))
    
  450.         self.assertEqual(ac.path, "a")
    
  451. 
    
  452.     def test_repr(self):
    
  453.         ac = AppConfig("label", Stub(__path__=["a"]))
    
  454.         self.assertEqual(repr(ac), "<AppConfig: label>")
    
  455. 
    
  456.     def test_invalid_label(self):
    
  457.         class MyAppConfig(AppConfig):
    
  458.             label = "invalid.label"
    
  459. 
    
  460.         msg = "The app label 'invalid.label' is not a valid Python identifier."
    
  461.         with self.assertRaisesMessage(ImproperlyConfigured, msg):
    
  462.             MyAppConfig("test_app", Stub())
    
  463. 
    
  464.     @override_settings(
    
  465.         INSTALLED_APPS=["apps.apps.ModelPKAppsConfig"],
    
  466.         DEFAULT_AUTO_FIELD="django.db.models.SmallAutoField",
    
  467.     )
    
  468.     def test_app_default_auto_field(self):
    
  469.         apps_config = apps.get_app_config("apps")
    
  470.         self.assertEqual(
    
  471.             apps_config.default_auto_field,
    
  472.             "django.db.models.BigAutoField",
    
  473.         )
    
  474.         self.assertIs(apps_config._is_default_auto_field_overridden, True)
    
  475. 
    
  476.     @override_settings(
    
  477.         INSTALLED_APPS=["apps.apps.PlainAppsConfig"],
    
  478.         DEFAULT_AUTO_FIELD="django.db.models.SmallAutoField",
    
  479.     )
    
  480.     def test_default_auto_field_setting(self):
    
  481.         apps_config = apps.get_app_config("apps")
    
  482.         self.assertEqual(
    
  483.             apps_config.default_auto_field,
    
  484.             "django.db.models.SmallAutoField",
    
  485.         )
    
  486.         self.assertIs(apps_config._is_default_auto_field_overridden, False)
    
  487. 
    
  488. 
    
  489. class NamespacePackageAppTests(SimpleTestCase):
    
  490.     # We need nsapp to be top-level so our multiple-paths tests can add another
    
  491.     # location for it (if its inside a normal package with an __init__.py that
    
  492.     # isn't possible). In order to avoid cluttering the already-full tests/ dir
    
  493.     # (which is on sys.path), we add these new entries to sys.path temporarily.
    
  494.     base_location = os.path.join(HERE, "namespace_package_base")
    
  495.     other_location = os.path.join(HERE, "namespace_package_other_base")
    
  496.     app_path = os.path.join(base_location, "nsapp")
    
  497. 
    
  498.     def test_single_path(self):
    
  499.         """
    
  500.         A Py3.3+ namespace package can be an app if it has only one path.
    
  501.         """
    
  502.         with extend_sys_path(self.base_location):
    
  503.             with self.settings(INSTALLED_APPS=["nsapp"]):
    
  504.                 app_config = apps.get_app_config("nsapp")
    
  505.                 self.assertEqual(app_config.path, self.app_path)
    
  506. 
    
  507.     def test_multiple_paths(self):
    
  508.         """
    
  509.         A Py3.3+ namespace package with multiple locations cannot be an app.
    
  510. 
    
  511.         (Because then we wouldn't know where to load its templates, static
    
  512.         assets, etc. from.)
    
  513.         """
    
  514.         # Temporarily add two directories to sys.path that both contain
    
  515.         # components of the "nsapp" package.
    
  516.         with extend_sys_path(self.base_location, self.other_location):
    
  517.             with self.assertRaises(ImproperlyConfigured):
    
  518.                 with self.settings(INSTALLED_APPS=["nsapp"]):
    
  519.                     pass
    
  520. 
    
  521.     def test_multiple_paths_explicit_path(self):
    
  522.         """
    
  523.         Multiple locations are ok only if app-config has explicit path.
    
  524.         """
    
  525.         # Temporarily add two directories to sys.path that both contain
    
  526.         # components of the "nsapp" package.
    
  527.         with extend_sys_path(self.base_location, self.other_location):
    
  528.             with self.settings(INSTALLED_APPS=["nsapp.apps.NSAppConfig"]):
    
  529.                 app_config = apps.get_app_config("nsapp")
    
  530.                 self.assertEqual(app_config.path, self.app_path)