Skip to content
Browse files

[soc2010/app-loading] update tests

  • Loading branch information...
1 parent 11a32d6 commit 32733074906e220c0f9f63f22450c41e81494435 @arthurk arthurk committed Sep 12, 2010
View
22 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 @@ def load_app(self, app_name, can_postpone=False, app_class=App):
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 @@ def load_app(self, app_name, can_postpone=False, app_class=App):
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
View
12 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'
View
9 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'
View
6 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
View
4 tests/appcachetests/nomodel_app/apps.py
@@ -0,0 +1,4 @@
+from django.core.apps import App
+
+class MyApp(App):
+ pass
View
86 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 @@ def test_db_prefix_exception(self):
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 @@ def test_with_models(self):
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 @@ def test_importerror(self):
"""
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 @@ def test_unseeded_cache(self):
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()
View
6 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
View
4 tests/appcachetests/same_label/nomodel_app/apps.py
@@ -0,0 +1,4 @@
+from django.core.apps import App
+
+class MyApp(App):
+ pass

0 comments on commit 3273307

Please sign in to comment.
Something went wrong with that request. Please try again.