Skip to content

Commit 3273307

Browse files
committed
[soc2010/app-loading] update tests
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13810 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 11a32d6 commit 3273307

File tree

8 files changed

+74
-75
lines changed

8 files changed

+74
-75
lines changed

django/core/apps.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ class AppCache(object):
4646
# List of App instances
4747
app_instances = [],
4848

49-
# Normalized list of INSTALLED_APPS
50-
# This stores the module name of settings.INSTALLED_APPS
51-
# e.g. 'django.contrib.auth' for 'django.contrib.auth.AuthApp'
52-
installed_apps = [],
53-
5449
# Mapping of app_labels to a dictionary of model names to model code.
5550
unbound_models = {},
5651

@@ -142,14 +137,11 @@ def load_app(self, app_name, can_postpone=False, app_class=App):
142137
app_instance.module = app_module
143138
app_instance.path = app_name
144139
self.app_instances.append(app_instance)
145-
self.installed_apps.append(app_name)
146140

147-
# Check if the app instance specifies a path to models
141+
# Check if the app instance specifies a path to a models module
148142
# if not, we use the models.py file from the package dir
149-
try:
150-
models_path = app_instance.models_path
151-
except AttributeError:
152-
models_path = '%s.models' % app_name
143+
models_path = getattr(app_instance, 'models_path',
144+
'%s.models' % app_name)
153145

154146
try:
155147
models = import_module(models_path)
@@ -178,9 +170,11 @@ def load_app(self, app_name, can_postpone=False, app_class=App):
178170
return models
179171

180172
def find_app(self, name):
181-
"Returns the App instance that matches name"
182-
if '.' in name:
183-
name = name.rsplit('.', 1)[1]
173+
"""
174+
Returns the app instance that matches name
175+
"""
176+
#if '.' in name:
177+
# name = name.rsplit('.', 1)[1]
184178
for app in self.app_instances:
185179
if app.name == name:
186180
return app
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +0,0 @@
1-
from django.core.apps import App
2-
3-
class MyApp(App):
4-
models_path = 'model_app.othermodels'
5-
6-
def __repr__(self):
7-
return '<MyApp: %s>' % self.name
8-
9-
class MyOtherApp(MyApp):
10-
def __init__(self, name):
11-
super(MyOtherApp, self).__init__(name)
12-
self.db_prefix = 'nomodel_app'

tests/appcachetests/model_app/apps.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.core.apps import App
2+
3+
class MyApp(App):
4+
models_path = 'model_app.othermodels'
5+
6+
class MyOtherApp(MyApp):
7+
def __init__(self, name):
8+
super(MyOtherApp, self).__init__(name)
9+
self.db_prefix = 'nomodel_app'
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
from django.core.apps import App
2-
3-
class MyApp(App):
4-
5-
def __repr__(self):
6-
return '<MyApp: %s>' % self.name
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from django.core.apps import App
2+
3+
class MyApp(App):
4+
pass

tests/appcachetests/runtests.py

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ class AppCacheReadyTests(AppCacheTestCase):
6767
"""
6868

6969
def test_not_initialized(self):
70-
"""Should return False if the AppCache hasn't been initialized"""
70+
"""
71+
Should return False if the AppCache hasn't been initialized
72+
"""
7173
self.assertFalse(cache.app_cache_ready())
7274

7375
def test_load_app(self):
74-
"""Should return False after executing the load_app function"""
76+
"""
77+
Should return False after executing the load_app function
78+
"""
7579
cache.load_app('nomodel_app')
7680
self.assertFalse(cache.app_cache_ready())
7781
cache.load_app('nomodel_app', can_postpone=True)
@@ -105,7 +109,8 @@ def test_db_prefix_exception(self):
105109
Test that an exception is raised if two app instances
106110
have the same db_prefix attribute
107111
"""
108-
settings.APP_CLASSES = ('nomodel_app.MyApp', 'model_app.MyOtherApp',)
112+
settings.APP_CLASSES = ('nomodel_app.apps.MyApp',
113+
'model_app.apps.MyOtherApp',)
109114
self.assertRaises(ImproperlyConfigured, cache.get_apps)
110115

111116
class GetAppTests(AppCacheTestCase):
@@ -254,42 +259,33 @@ def test_with_models(self):
254259
self.assertEqual(app.models_module.__name__, 'model_app.models')
255260
self.assertEqual(rv.__name__, 'model_app.models')
256261

257-
def test_without_models(self):
262+
def test_with_custom_models(self):
258263
"""
259-
Test that an app instance is created when there are no models
264+
Test that custom models are imported correctly, if the App specifies
265+
an models_path attribute
260266
"""
261-
rv = cache.load_app('nomodel_app')
267+
from nomodel_app.apps import MyApp
268+
rv = cache.load_app('model_app', can_postpone=False, app_class=MyApp)
262269
app = cache.app_instances[0]
263-
self.assertEqual(len(cache.app_instances), 1)
264-
self.assertEqual(app.name, 'nomodel_app')
265-
self.assertEqual(rv, None)
270+
self.assertEqual(app.models_module.__name__, 'model_app.models')
271+
self.assertTrue(isinstance(app, MyApp))
272+
self.assertEqual(rv.__name__, 'model_app.models')
266273

267-
def test_custom_app(self):
274+
def test_without_models(self):
268275
"""
269-
Test that a custom app instance is created if the function
270-
gets passed a custom app class
276+
Test that an app instance is created even when there are
277+
no models provided
271278
"""
272-
from nomodel_app import MyApp
273-
rv = cache.load_app('nomodel_app', False, MyApp)
279+
rv = cache.load_app('nomodel_app')
274280
app = cache.app_instances[0]
275281
self.assertEqual(len(cache.app_instances), 1)
276282
self.assertEqual(app.name, 'nomodel_app')
277-
self.assertTrue(isinstance(app, MyApp))
278283
self.assertEqual(rv, None)
279284

280-
def test_custom_models_path(self):
281-
"""
282-
Test that custom models are imported correctly
283-
"""
284-
from nomodel_app import MyApp
285-
rv = cache.load_app('model_app', False, MyApp)
286-
app = cache.app_instances[0]
287-
self.assertEqual(app.models_module.__name__, 'model_app.models')
288-
self.assertEqual(rv.__name__, 'model_app.models')
289-
290-
def test_twice(self):
285+
def test_loading_the_same_app_twice(self):
291286
"""
292-
Test that loading an app twice results in only one app instance
287+
Test that loading the same app twice results in only one app instance
288+
being created
293289
"""
294290
rv = cache.load_app('model_app')
295291
rv2 = cache.load_app('model_app')
@@ -304,16 +300,6 @@ def test_importerror(self):
304300
"""
305301
self.assertRaises(ImportError, cache.load_app, 'garageland')
306302

307-
def test_installed_apps(self):
308-
"""
309-
Test that the installed_apps attribute is populated correctly
310-
"""
311-
settings.APP_CLASSES = ('nomodel_app.MyApp',)
312-
settings.INSTALLED_APPS = ('model_app',)
313-
# populate cache
314-
cache.get_app_errors()
315-
self.assertEqual(cache.installed_apps, ['nomodel_app', 'model_app',])
316-
317303
class RegisterModelsTests(AppCacheTestCase):
318304
"""Tests for the register_models function"""
319305

@@ -349,6 +335,32 @@ def test_unseeded_cache(self):
349335
self.assertFalse(cache.app_cache_ready())
350336
self.assertEquals(cache.unbound_models['model_app']['person'], Person)
351337

338+
class FindAppTests(AppCacheTestCase):
339+
"""Tests for the find_app function"""
340+
341+
def test_seeded(self):
342+
"""
343+
Test that the correct app is returned when the cache is seeded
344+
"""
345+
from django.core.apps import App
346+
settings.INSTALLED_APPS = ('model_app',)
347+
cache.get_app_errors()
348+
self.assertTrue(cache.app_cache_ready())
349+
rv = cache.find_app('model_app')
350+
self.assertEquals(rv.name, 'model_app')
351+
self.assertTrue(isinstance(rv, App))
352+
353+
def test_unseeded(self):
354+
"""
355+
Test that the correct app is returned when the cache is unseeded
356+
"""
357+
from django.core.apps import App
358+
cache.load_app('model_app')
359+
self.assertFalse(cache.app_cache_ready())
360+
rv = cache.find_app('model_app')
361+
self.assertEquals(rv.name, 'model_app')
362+
self.assertTrue(isinstance(rv, App))
363+
352364
if __name__ == '__main__':
353365
unittest.main()
354366

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
from django.core.apps import App
2-
3-
class MyApp(App):
4-
5-
def __repr__(self):
6-
return '<MyApp: %s>' % self.name
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from django.core.apps import App
2+
3+
class MyApp(App):
4+
pass

0 commit comments

Comments
 (0)