1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

[soc2010/app-loading] update tests

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13810 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Arthur Koziel 2010-09-12 21:16:09 +00:00
parent 11a32d6011
commit 3273307490
8 changed files with 76 additions and 77 deletions

View File

@ -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

View File

@ -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 File

@ -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 File

@ -1,6 +0,0 @@
from django.core.apps import App
class MyApp(App):
def __repr__(self):
return '<MyApp: %s>' % self.name

View File

@ -0,0 +1,4 @@
from django.core.apps import App
class MyApp(App):
pass

View File

@ -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,9 +259,22 @@ class LoadAppTests(AppCacheTestCase):
self.assertEqual(app.models_module.__name__, 'model_app.models')
self.assertEqual(rv.__name__, 'model_app.models')
def test_with_custom_models(self):
"""
Test that custom models are imported correctly, if the App specifies
an models_path attribute
"""
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(app.models_module.__name__, 'model_app.models')
self.assertTrue(isinstance(app, MyApp))
self.assertEqual(rv.__name__, 'model_app.models')
def test_without_models(self):
"""
Test that an app instance is created when there are no models
Test that an app instance is created even when there are
no models provided
"""
rv = cache.load_app('nomodel_app')
app = cache.app_instances[0]
@ -264,32 +282,10 @@ class LoadAppTests(AppCacheTestCase):
self.assertEqual(app.name, 'nomodel_app')
self.assertEqual(rv, None)
def test_custom_app(self):
def test_loading_the_same_app_twice(self):
"""
Test that a custom app instance is created if the function
gets passed a custom app class
"""
from nomodel_app import MyApp
rv = cache.load_app('nomodel_app', False, MyApp)
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):
"""
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()

View File

@ -1,6 +0,0 @@
from django.core.apps import App
class MyApp(App):
def __repr__(self):
return '<MyApp: %s>' % self.name

View File

@ -0,0 +1,4 @@
from django.core.apps import App
class MyApp(App):
pass