Skip to content

Commit

Permalink
Deprecated load_app().
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aaugustin committed Dec 22, 2013
1 parent 439b364 commit f25fa9d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 64 deletions.
25 changes: 14 additions & 11 deletions django/core/apps/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ def populate_models(self):

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.
Expand Down Expand Up @@ -377,6 +367,19 @@ def _empty(self):

### 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.
Expand Down Expand Up @@ -447,7 +450,7 @@ def register_models(self, app_label, *models):
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)
Expand Down
2 changes: 1 addition & 1 deletion tests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ def test_broken_app(self):
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})
Expand Down
35 changes: 16 additions & 19 deletions tests/app_loading/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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):
Expand All @@ -31,45 +32,41 @@ def test_egg1(self):
"""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):
"""
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ def setUp(self):
self.stdout = StringIO()
sys.stdout = self.stdout

# The models need to be removed after the test in order to prevent bad
# interactions with the flush operation in other tests.
self._old_models = app_cache.app_configs['invalid_models'].models.copy()

def tearDown(self):
app_cache.app_configs['invalid_models'].models = self._old_models
app_cache.all_models['invalid_models'] = self._old_models
app_cache._get_models_cache = {}
sys.stdout = self.old_stdout

# Technically, this isn't an override -- TEST_SWAPPED_MODEL must be
Expand All @@ -38,12 +31,10 @@ def tearDown(self):
TEST_SWAPPED_MODEL_BAD_MODEL='not_an_app.Target',
)
def test_invalid_models(self):
try:
module = app_cache.load_app("invalid_models.invalid_models")
except Exception:
self.fail('Unable to load invalid model module')
with app_cache._with_app("invalid_models_tests.invalid_models"):
module = app_cache.get_app_config("invalid_models").models_module
get_validation_errors(self.stdout, module)

get_validation_errors(self.stdout, module)
self.stdout.seek(0)
error_log = self.stdout.read()
actual = error_log.split('\n')
Expand Down
10 changes: 5 additions & 5 deletions tests/migrations/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def test_migration_path(self):

with override_settings(INSTALLED_APPS=test_apps):
for app in test_apps:
app_cache.load_app(app)
migration = migrations.Migration('0001_initial', app.split('.')[-1])
expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py']))
writer = MigrationWriter(migration)
self.assertEqual(writer.path, expected_path)
with app_cache._with_app(app):
migration = migrations.Migration('0001_initial', app.split('.')[-1])
expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py']))
writer = MigrationWriter(migration)
self.assertEqual(writer.path, expected_path)
21 changes: 6 additions & 15 deletions tests/proxy_model_inheritance/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import sys

from django.conf import settings
from django.core.apps import app_cache
from django.core.management import call_command
from django.test import TestCase, TransactionTestCase
Expand All @@ -21,28 +20,21 @@ class ProxyModelInheritanceTests(TransactionTestCase):
for the proxied model (as described in #12286). This test creates two dummy
apps and calls migrate, then verifies that the table has been created.
"""

available_apps = []
available_apps = ['app1', 'app2']

def setUp(self):
self.old_sys_path = sys.path[:]
sys.path.append(os.path.dirname(os.path.abspath(upath(__file__))))
for app in settings.INSTALLED_APPS:
app_cache.load_app(app)
self._with_app1 = app_cache._begin_with_app('app1')
self._with_app2 = app_cache._begin_with_app('app2')

def tearDown(self):
app_cache._end_with_app(self._with_app1)
app_cache._end_with_app(self._with_app2)
sys.path = self.old_sys_path
del app_cache.app_configs['app1']
del app_cache.app_configs['app2']
del app_cache.all_models['app1']
del app_cache.all_models['app2']

def test_table_exists(self):
try:
app_cache.set_available_apps(settings.INSTALLED_APPS)
call_command('migrate', verbosity=0)
finally:
app_cache.unset_available_apps()
call_command('migrate', verbosity=0)
from .app1.models import ProxyModel
from .app2.models import NiceModel
self.assertEqual(NiceModel.objects.all().count(), 0)
Expand All @@ -56,7 +48,6 @@ def test_model_subclass_proxy(self):
Deleting an instance of a model proxying a multi-table inherited
subclass should cascade delete down the whole inheritance chain (see
#18083).
"""
instance = ConcreteModelSubclassProxy.objects.create()
instance.delete()
Expand Down
3 changes: 2 additions & 1 deletion tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def no_available_apps(self):
if module_found_in_labels:
if verbosity >= 2:
print("Importing application %s" % module_name)
app_cache.load_app(module_label)
# HACK.
app_cache._begin_with_app(module_label)
if module_label not in settings.INSTALLED_APPS:
settings.INSTALLED_APPS.append(module_label)

Expand Down

0 comments on commit f25fa9d

Please sign in to comment.