@@ -32,15 +32,21 @@ def tearDown(self):
32
32
# To detect which model modules have been imported, we go through
33
33
# all loaded model classes and remove their respective module
34
34
# from sys.modules
35
- for app in cache .app_models .itervalues ():
35
+ for app in cache .unbound_models .itervalues ():
36
36
for name in app .itervalues ():
37
37
module = name .__module__
38
38
if module in sys .modules :
39
39
del sys .modules [module ]
40
40
41
+ for app in cache .app_instances :
42
+ for model in app .models :
43
+ module = model .__module__
44
+ if module in sys .modules :
45
+ del sys .modules [module ]
46
+
41
47
# we cannot copy() the whole cache.__dict__ in the setUp function
42
48
# because thread.RLock is un(deep)copyable
43
- cache .app_models = SortedDict ()
49
+ cache .unbound_models = {}
44
50
cache .app_instances = []
45
51
cache .installed_apps = []
46
52
@@ -178,34 +184,47 @@ def test_include_auto_created(self):
178
184
from django .contrib .flatpages .models import Site , FlatPage
179
185
self .assertEqual (len (models ), 3 )
180
186
self .assertEqual (models [0 ], Site )
181
- self .assertEqual (models [1 ]. __name__ , 'FlatPage_sites' )
182
- self .assertEqual (models [2 ], FlatPage )
187
+ self .assertEqual (models [1 ], FlatPage )
188
+ self .assertEqual (models [2 ]. __name__ , 'FlatPage_sites' )
183
189
self .assertTrue (cache .app_cache_ready ())
184
190
185
- def test_include_deferred (self ):
186
- """TODO!"""
187
-
188
191
class GetModelTests (AppCacheTestCase ):
189
192
"""Tests for the get_model function"""
190
193
191
- def test_get_model (self ):
192
- """Test that the correct model is returned"""
193
- settings . INSTALLED_APPS = ( 'django.contrib.sites' ,
194
- 'django.contrib.flatpages' ,)
195
- rv = cache . get_model ( 'flatpages' , 'FlatPage' )
196
- from django . contrib . flatpages . models import FlatPage
197
- self .assertEqual (rv , FlatPage )
194
+ def test_seeded (self ):
195
+ """
196
+ Test that the correct model is returned when the cache is seeded
197
+ """
198
+ settings . INSTALLED_APPS = ( 'model_app' , )
199
+ rv = cache . get_model ( 'model_app' , 'Person' )
200
+ self .assertEqual (rv . __name__ , 'Person' )
198
201
self .assertTrue (cache .app_cache_ready ())
199
202
200
- def test_invalid (self ):
201
- """Test that None is returned if an app/model does not exist"""
202
- self .assertEqual (cache .get_model ('foo' , 'bar' ), None )
203
+ def test_seeded_invalid (self ):
204
+ """
205
+ Test that None is returned if a model was not registered
206
+ with the seeded cache
207
+ """
208
+ rv = cache .get_model ('model_app' , 'Person' )
209
+ self .assertEqual (rv , None )
203
210
self .assertTrue (cache .app_cache_ready ())
204
211
205
- def test_without_seeding (self ):
206
- """Test that None is returned if the cache is not seeded"""
207
- settings .INSTALLED_APPS = ('django.contrib.flatpages' ,)
208
- rv = cache .get_model ('flatpages' , 'FlatPage' , seed_cache = False )
212
+ def test_unseeded (self ):
213
+ """
214
+ Test that the correct model is returned when the cache is
215
+ unseeded (but the model was registered using register_models)
216
+ """
217
+ from model_app .models import Person
218
+ rv = cache .get_model ('model_app' , 'Person' , seed_cache = False )
219
+ self .assertEqual (rv .__name__ , 'Person' )
220
+ self .assertFalse (cache .app_cache_ready ())
221
+
222
+ def test_unseeded_invalid (self ):
223
+ """
224
+ Test that None is returned if a model was not registered
225
+ with the unseeded cache
226
+ """
227
+ rv = cache .get_model ('model_app' , 'Person' , seed_cache = False )
209
228
self .assertEqual (rv , None )
210
229
self .assertFalse (cache .app_cache_ready ())
211
230
@@ -285,30 +304,37 @@ def test_installed_apps(self):
285
304
class RegisterModelsTests (AppCacheTestCase ):
286
305
"""Tests for the register_models function"""
287
306
288
- def test_register_models (self ):
307
+ def test_seeded_cache (self ):
289
308
"""
290
- Test that register_models attaches the models to an existing
291
- app instance
309
+ Test that the models are attached to the correct app instance
310
+ in a seeded cache
292
311
"""
293
- # We don't need to call the register_models method. Importing the
294
- # models.py file will suffice. This is done in the load_app function
295
- # The ModelBase will call the register_models method
296
- cache .load_app ('model_app' )
297
- app = cache .app_instances [0 ]
298
- self .assertEqual (len (cache .app_instances ), 1 )
299
- self .assertEqual (app .models [0 ].__name__ , 'Person' )
312
+ settings .INSTALLED_APPS = ('model_app' ,)
313
+ cache .get_app_errors ()
314
+ self .assertTrue (cache .app_cache_ready ())
315
+ app_models = cache .app_instances [0 ].models
316
+ self .assertEqual (len (app_models ), 1 )
317
+ self .assertEqual (app_models [0 ].__name__ , 'Person' )
300
318
301
- def test_app_not_installed (self ):
319
+ def test_seeded_cache_invalid_app (self ):
302
320
"""
303
- Test that an exception is raised if models are tried to be registered
304
- to an app that isn 't listed in INSTALLED_APPS.
321
+ Test that an exception is raised if the cache is seeded and models
322
+ are tried to be attached to an app instance that doesn 't exist
305
323
"""
306
- try :
307
- from model_app .models import Person
308
- except ImproperlyConfigured :
309
- pass
310
- else :
311
- self .fail ('ImproperlyConfigured not raised' )
324
+ settings .INSTALLED_APPS = ('model_app' ,)
325
+ cache .get_app_errors ()
326
+ self .assertTrue (cache .app_cache_ready ())
327
+ from model_app .models import Person
328
+ self .assertRaises (ImproperlyConfigured , cache .register_models ,
329
+ 'model_app_NONEXISTENT' , * (Person ,))
330
+
331
+ def test_unseeded_cache (self ):
332
+ """
333
+ Test that models can be registered with an unseeded cache
334
+ """
335
+ from model_app .models import Person
336
+ self .assertFalse (cache .app_cache_ready ())
337
+ self .assertEquals (cache .unbound_models ['model_app' ]['person' ], Person )
312
338
313
339
if __name__ == '__main__' :
314
340
unittest .main ()
0 commit comments