Commit f25fa9d8 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Deprecated load_app().

Adjusted several tests that used it to add apps to the app cache and
then attempted to remove them by manipulating attributes directly.

Also renamed invalid_models to invalid_models_tests to avoid clashing
application labels between the outer and the inner invalid_models
applications.
parent 439b364e
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -133,16 +133,6 @@ class AppCache(object):

                self._models_loaded = True

    def load_app(self, app_name):
        """
        Loads the app with the provided fully qualified name, and returns the
        model module.
        """
        app_config = AppConfig(app_name)
        app_config.import_models(self.all_models[app_config.label])
        self.app_configs[app_config.label] = app_config
        return app_config.models_module

    def app_cache_ready(self):
        """
        Returns true if the model cache is fully populated.
@@ -377,6 +367,19 @@ class AppCache(object):

    ### DEPRECATED METHODS GO BELOW THIS LINE ###

    def load_app(self, app_name):
        """
        Loads the app with the provided fully qualified name, and returns the
        model module.
        """
        warnings.warn(
            "load_app(app_name) is deprecated.",
            PendingDeprecationWarning, stacklevel=2)
        app_config = AppConfig(app_name)
        app_config.import_models(self.all_models[app_config.label])
        self.app_configs[app_config.label] = app_config
        return app_config.models_module

    def get_app(self, app_label):
        """
        Returns the module containing the models for the given app_label.
@@ -447,7 +450,7 @@ class AppCache(object):
        Register a set of models as belonging to an app.
        """
        warnings.warn(
            "register_models(app_label, models) is deprecated.",
            "register_models(app_label, *models) is deprecated.",
            PendingDeprecationWarning, stacklevel=2)
        for model in models:
            self.register_model(app_label, model)
+1 −1
Original line number Diff line number Diff line
@@ -1094,7 +1094,7 @@ class ManageValidate(AdminScriptTestCase):
        self.assertOutput(err, 'ImportError')

    def test_complex_app(self):
        "manage.py validate does not raise an ImportError validating a complex app with nested calls to load_app"
        "manage.py validate does not raise an ImportError validating a complex app"
        self.write_settings('settings.py',
            apps=['admin_scripts.complex_app', 'admin_scripts.simple_app'],
            sdict={'DEBUG': True})
+16 −19
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ from django.core.apps import app_cache
from django.core.apps.cache import AppCache
from django.test.utils import override_settings
from django.utils._os import upath
from django.utils import six


class EggLoadingTest(TestCase):
@@ -31,45 +32,41 @@ class EggLoadingTest(TestCase):
        """Models module can be loaded from an app in an egg"""
        egg_name = '%s/modelapp.egg' % self.egg_dir
        sys.path.append(egg_name)
        models = app_cache.load_app('app_with_models')
        self.assertFalse(models is None)
        with app_cache._with_app('app_with_models'):
            models_module = app_cache.get_app_config('app_with_models').models_module
            self.assertIsNotNone(models_module)

    def test_egg2(self):
        """Loading an app from an egg that has no models returns no models (and no error)"""
        egg_name = '%s/nomodelapp.egg' % self.egg_dir
        sys.path.append(egg_name)
        models = app_cache.load_app('app_no_models')
        self.assertTrue(models is None)
        with app_cache._with_app('app_no_models'):
            models_module = app_cache.get_app_config('app_no_models').models_module
            self.assertIsNone(models_module)

    def test_egg3(self):
        """Models module can be loaded from an app located under an egg's top-level package"""
        egg_name = '%s/omelet.egg' % self.egg_dir
        sys.path.append(egg_name)
        models = app_cache.load_app('omelet.app_with_models')
        self.assertFalse(models is None)
        with app_cache._with_app('omelet.app_with_models'):
            models_module = app_cache.get_app_config('app_with_models').models_module
            self.assertIsNotNone(models_module)

    def test_egg4(self):
        """Loading an app with no models from under the top-level egg package generates no error"""
        egg_name = '%s/omelet.egg' % self.egg_dir
        sys.path.append(egg_name)
        models = app_cache.load_app('omelet.app_no_models')
        self.assertTrue(models is None)
        with app_cache._with_app('omelet.app_no_models'):
            models_module = app_cache.get_app_config('app_no_models').models_module
            self.assertIsNone(models_module)

    def test_egg5(self):
        """Loading an app from an egg that has an import error in its models module raises that error"""
        egg_name = '%s/brokenapp.egg' % self.egg_dir
        sys.path.append(egg_name)
        self.assertRaises(ImportError, app_cache.load_app, 'broken_app')
        raised = None
        try:
            app_cache.load_app('broken_app')
        except ImportError as e:
            raised = e

        # Make sure the message is indicating the actual
        # problem in the broken app.
        self.assertTrue(raised is not None)
        self.assertTrue("modelz" in raised.args[0])
        with six.assertRaisesRegex(self, ImportError, 'modelz'):
            with app_cache._with_app('broken_app'):
                app_cache.get_app_config('omelet.app_no_models').models_module

    def test_missing_app(self):
        """
Loading