diff options
author | Arthur Koziel <arthur@arthurkoziel.com> | 2010-09-12 21:16:09 +0000 |
---|---|---|
committer | Arthur Koziel <arthur@arthurkoziel.com> | 2010-09-12 21:16:09 +0000 |
commit | 32733074906e220c0f9f63f22450c41e81494435 (patch) | |
tree | 0fd04922cd54d392a472a76532411b14278c675f | |
parent | 11a32d6011358414a3f9179319601583e4fc6b32 (diff) | |
download | django-32733074906e220c0f9f63f22450c41e81494435.tar.gz |
[soc2010/app-loading] update tests
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13810 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r-- | django/core/apps.py | 22 | ||||
-rw-r--r-- | tests/appcachetests/model_app/__init__.py | 12 | ||||
-rw-r--r-- | tests/appcachetests/model_app/apps.py | 9 | ||||
-rw-r--r-- | tests/appcachetests/nomodel_app/__init__.py | 6 | ||||
-rw-r--r-- | tests/appcachetests/nomodel_app/apps.py | 4 | ||||
-rw-r--r-- | tests/appcachetests/runtests.py | 86 | ||||
-rw-r--r-- | tests/appcachetests/same_label/nomodel_app/__init__.py | 6 | ||||
-rw-r--r-- | tests/appcachetests/same_label/nomodel_app/apps.py | 4 |
8 files changed, 74 insertions, 75 deletions
diff --git a/django/core/apps.py b/django/core/apps.py index 35a0ea359d..df8355ca12 100644 --- a/django/core/apps.py +++ b/django/core/apps.py @@ -46,11 +46,6 @@ class AppCache(object): # List of App instances app_instances = [], - # Normalized list of INSTALLED_APPS - # This stores the module name of settings.INSTALLED_APPS - # e.g. 'django.contrib.auth' for 'django.contrib.auth.AuthApp' - installed_apps = [], - # Mapping of app_labels to a dictionary of model names to model code. unbound_models = {}, @@ -142,14 +137,11 @@ class AppCache(object): app_instance.module = app_module app_instance.path = app_name self.app_instances.append(app_instance) - self.installed_apps.append(app_name) - # Check if the app instance specifies a path to models + # Check if the app instance specifies a path to a models module # if not, we use the models.py file from the package dir - try: - models_path = app_instance.models_path - except AttributeError: - models_path = '%s.models' % app_name + models_path = getattr(app_instance, 'models_path', + '%s.models' % app_name) try: models = import_module(models_path) @@ -178,9 +170,11 @@ class AppCache(object): return models def find_app(self, name): - "Returns the App instance that matches name" - if '.' in name: - name = name.rsplit('.', 1)[1] + """ + Returns the app instance that matches name + """ + #if '.' in name: + # name = name.rsplit('.', 1)[1] for app in self.app_instances: if app.name == name: return app diff --git a/tests/appcachetests/model_app/__init__.py b/tests/appcachetests/model_app/__init__.py index 96abf9439f..e69de29bb2 100644 --- a/tests/appcachetests/model_app/__init__.py +++ b/tests/appcachetests/model_app/__init__.py @@ -1,12 +0,0 @@ -from django.core.apps import App - -class MyApp(App): - models_path = 'model_app.othermodels' - - def __repr__(self): - return '<MyApp: %s>' % self.name - -class MyOtherApp(MyApp): - def __init__(self, name): - super(MyOtherApp, self).__init__(name) - self.db_prefix = 'nomodel_app' diff --git a/tests/appcachetests/model_app/apps.py b/tests/appcachetests/model_app/apps.py new file mode 100644 index 0000000000..25bae700a2 --- /dev/null +++ b/tests/appcachetests/model_app/apps.py @@ -0,0 +1,9 @@ +from django.core.apps import App + +class MyApp(App): + models_path = 'model_app.othermodels' + +class MyOtherApp(MyApp): + def __init__(self, name): + super(MyOtherApp, self).__init__(name) + self.db_prefix = 'nomodel_app' diff --git a/tests/appcachetests/nomodel_app/__init__.py b/tests/appcachetests/nomodel_app/__init__.py index 8e455462ad..e69de29bb2 100644 --- a/tests/appcachetests/nomodel_app/__init__.py +++ b/tests/appcachetests/nomodel_app/__init__.py @@ -1,6 +0,0 @@ -from django.core.apps import App - -class MyApp(App): - - def __repr__(self): - return '<MyApp: %s>' % self.name diff --git a/tests/appcachetests/nomodel_app/apps.py b/tests/appcachetests/nomodel_app/apps.py new file mode 100644 index 0000000000..01767dfe6e --- /dev/null +++ b/tests/appcachetests/nomodel_app/apps.py @@ -0,0 +1,4 @@ +from django.core.apps import App + +class MyApp(App): + pass diff --git a/tests/appcachetests/runtests.py b/tests/appcachetests/runtests.py index 59d2eca4d0..2b2821f6a8 100644 --- a/tests/appcachetests/runtests.py +++ b/tests/appcachetests/runtests.py @@ -67,11 +67,15 @@ class AppCacheReadyTests(AppCacheTestCase): """ def test_not_initialized(self): - """Should return False if the AppCache hasn't been initialized""" + """ + Should return False if the AppCache hasn't been initialized + """ self.assertFalse(cache.app_cache_ready()) def test_load_app(self): - """Should return False after executing the load_app function""" + """ + Should return False after executing the load_app function + """ cache.load_app('nomodel_app') self.assertFalse(cache.app_cache_ready()) cache.load_app('nomodel_app', can_postpone=True) @@ -105,7 +109,8 @@ class GetAppsTests(AppCacheTestCase): Test that an exception is raised if two app instances have the same db_prefix attribute """ - settings.APP_CLASSES = ('nomodel_app.MyApp', 'model_app.MyOtherApp',) + settings.APP_CLASSES = ('nomodel_app.apps.MyApp', + 'model_app.apps.MyOtherApp',) self.assertRaises(ImproperlyConfigured, cache.get_apps) class GetAppTests(AppCacheTestCase): @@ -254,42 +259,33 @@ class LoadAppTests(AppCacheTestCase): self.assertEqual(app.models_module.__name__, 'model_app.models') self.assertEqual(rv.__name__, 'model_app.models') - def test_without_models(self): + def test_with_custom_models(self): """ - Test that an app instance is created when there are no models + Test that custom models are imported correctly, if the App specifies + an models_path attribute """ - rv = cache.load_app('nomodel_app') + from nomodel_app.apps import MyApp + rv = cache.load_app('model_app', can_postpone=False, app_class=MyApp) app = cache.app_instances[0] - self.assertEqual(len(cache.app_instances), 1) - self.assertEqual(app.name, 'nomodel_app') - self.assertEqual(rv, None) + self.assertEqual(app.models_module.__name__, 'model_app.models') + self.assertTrue(isinstance(app, MyApp)) + self.assertEqual(rv.__name__, 'model_app.models') - def test_custom_app(self): + def test_without_models(self): """ - Test that a custom app instance is created if the function - gets passed a custom app class + Test that an app instance is created even when there are + no models provided """ - from nomodel_app import MyApp - rv = cache.load_app('nomodel_app', False, MyApp) + rv = cache.load_app('nomodel_app') app = cache.app_instances[0] self.assertEqual(len(cache.app_instances), 1) self.assertEqual(app.name, 'nomodel_app') - self.assertTrue(isinstance(app, MyApp)) self.assertEqual(rv, None) - def test_custom_models_path(self): - """ - Test that custom models are imported correctly - """ - from nomodel_app import MyApp - rv = cache.load_app('model_app', False, MyApp) - app = cache.app_instances[0] - self.assertEqual(app.models_module.__name__, 'model_app.models') - self.assertEqual(rv.__name__, 'model_app.models') - - def test_twice(self): + def test_loading_the_same_app_twice(self): """ - Test that loading an app twice results in only one app instance + Test that loading the same app twice results in only one app instance + being created """ rv = cache.load_app('model_app') rv2 = cache.load_app('model_app') @@ -304,16 +300,6 @@ class LoadAppTests(AppCacheTestCase): """ self.assertRaises(ImportError, cache.load_app, 'garageland') - def test_installed_apps(self): - """ - Test that the installed_apps attribute is populated correctly - """ - settings.APP_CLASSES = ('nomodel_app.MyApp',) - settings.INSTALLED_APPS = ('model_app',) - # populate cache - cache.get_app_errors() - self.assertEqual(cache.installed_apps, ['nomodel_app', 'model_app',]) - class RegisterModelsTests(AppCacheTestCase): """Tests for the register_models function""" @@ -349,6 +335,32 @@ class RegisterModelsTests(AppCacheTestCase): self.assertFalse(cache.app_cache_ready()) self.assertEquals(cache.unbound_models['model_app']['person'], Person) +class FindAppTests(AppCacheTestCase): + """Tests for the find_app function""" + + def test_seeded(self): + """ + Test that the correct app is returned when the cache is seeded + """ + from django.core.apps import App + settings.INSTALLED_APPS = ('model_app',) + cache.get_app_errors() + self.assertTrue(cache.app_cache_ready()) + rv = cache.find_app('model_app') + self.assertEquals(rv.name, 'model_app') + self.assertTrue(isinstance(rv, App)) + + def test_unseeded(self): + """ + Test that the correct app is returned when the cache is unseeded + """ + from django.core.apps import App + cache.load_app('model_app') + self.assertFalse(cache.app_cache_ready()) + rv = cache.find_app('model_app') + self.assertEquals(rv.name, 'model_app') + self.assertTrue(isinstance(rv, App)) + if __name__ == '__main__': unittest.main() diff --git a/tests/appcachetests/same_label/nomodel_app/__init__.py b/tests/appcachetests/same_label/nomodel_app/__init__.py index 8e455462ad..e69de29bb2 100644 --- a/tests/appcachetests/same_label/nomodel_app/__init__.py +++ b/tests/appcachetests/same_label/nomodel_app/__init__.py @@ -1,6 +0,0 @@ -from django.core.apps import App - -class MyApp(App): - - def __repr__(self): - return '<MyApp: %s>' % self.name diff --git a/tests/appcachetests/same_label/nomodel_app/apps.py b/tests/appcachetests/same_label/nomodel_app/apps.py new file mode 100644 index 0000000000..01767dfe6e --- /dev/null +++ b/tests/appcachetests/same_label/nomodel_app/apps.py @@ -0,0 +1,4 @@ +from django.core.apps import App + +class MyApp(App): + pass |