5
5
from django .conf import settings
6
6
from django .utils .datastructures import SortedDict
7
7
from django .core .exceptions import ImproperlyConfigured
8
- from django .core .apps import MultipleInstancesReturned
8
+ from django .core .apps import cache , MultipleInstancesReturned
9
9
10
10
# remove when tests are integrated into the django testsuite
11
11
settings .configure ()
12
12
13
- from django .db .models .loading import cache
14
13
15
14
class AppCacheTestCase (unittest .TestCase ):
16
15
"""
@@ -41,17 +40,15 @@ def tearDown(self):
41
40
42
41
# we cannot copy() the whole cache.__dict__ in the setUp function
43
42
# because thread.RLock is un(deep)copyable
44
- cache .app_store = SortedDict ()
45
43
cache .app_models = SortedDict ()
46
- cache .app_errors = {}
44
+ cache .app_instances = []
45
+
47
46
cache .loaded = False
48
47
cache .handled = {}
49
48
cache .postponed = []
50
49
cache .nesting_level = 0
51
50
cache .write_lock = threading .RLock ()
52
51
cache ._get_models_cache = {}
53
-
54
- cache .app_instances = []
55
52
56
53
class AppCacheReadyTests (AppCacheTestCase ):
57
54
"""
@@ -65,22 +62,26 @@ def test_not_initialized(self):
65
62
66
63
def test_load_app (self ):
67
64
"""Should return False after executing the load_app function"""
68
- cache .load_app ('django.contrib.comments ' )
65
+ cache .load_app ('nomodel_app ' )
69
66
self .assertFalse (cache .app_cache_ready ())
70
- cache .load_app ('django.contrib.comments ' , can_postpone = True )
67
+ cache .load_app ('nomodel_app ' , can_postpone = True )
71
68
self .assertFalse (cache .app_cache_ready ())
72
69
73
70
class GetAppsTests (AppCacheTestCase ):
74
71
"""Tests for the get_apps function"""
75
72
76
73
def test_get_apps (self ):
77
74
"""Test that the correct models modules are returned"""
78
- settings .INSTALLED_APPS = ('django.contrib.auth' ,
75
+ settings .INSTALLED_APPS = ('django.contrib.sites' ,
76
+ 'django.contrib.contenttypes' ,
77
+ 'django.contrib.auth' ,
79
78
'django.contrib.flatpages' ,)
80
79
apps = cache .get_apps ()
81
- self .assertEqual (len (apps ), 2 )
80
+ self .assertEqual (len (apps ), 4 )
82
81
self .assertTrue (apps [0 ], 'django.contrib.auth.models' )
83
82
self .assertTrue (apps [1 ], 'django.contrib.flatpages.models' )
83
+ self .assertTrue (apps [2 ], 'django.contrib.sites.models' )
84
+ self .assertTrue (apps [3 ], 'django.contrib.contenttypes.models' )
84
85
self .assertTrue (cache .app_cache_ready ())
85
86
86
87
def test_empty_models (self ):
@@ -94,7 +95,8 @@ class GetAppTests(AppCacheTestCase):
94
95
95
96
def test_get_app (self ):
96
97
"""Test that the correct module is returned"""
97
- settings .INSTALLED_APPS = ('django.contrib.auth' ,)
98
+ settings .INSTALLED_APPS = ('django.contrib.contenttypes' ,
99
+ 'django.contrib.auth' ,)
98
100
module = cache .get_app ('auth' )
99
101
self .assertTrue (module , 'django.contrib.auth.models' )
100
102
self .assertTrue (cache .app_cache_ready ())
@@ -141,9 +143,10 @@ class GetModelsTests(AppCacheTestCase):
141
143
142
144
def test_get_models (self ):
143
145
"""Test that the correct model classes are returned"""
144
- settings .INSTALLED_APPS = ('django.contrib.flatpages' ,)
145
- from django .contrib .flatpages . models import Site , FlatPage
146
+ settings .INSTALLED_APPS = ('django.contrib.sites' ,
147
+ ' django.contrib.flatpages' ,)
146
148
models = cache .get_models ()
149
+ from django .contrib .flatpages .models import Site , FlatPage
147
150
self .assertEqual (len (models ), 2 )
148
151
self .assertEqual (models [0 ], Site )
149
152
self .assertEqual (models [1 ], FlatPage )
@@ -154,7 +157,11 @@ def test_app_mod(self):
154
157
Test that the correct model classes are returned if an
155
158
app module is specified
156
159
"""
157
- settings .INSTALLED_APPS = ('django.contrib.flatpages' ,)
160
+ settings .INSTALLED_APPS = ('django.contrib.sites' ,
161
+ 'django.contrib.flatpages' ,)
162
+ # populate cache
163
+ cache .get_app_errors ()
164
+
158
165
from django .contrib .flatpages import models
159
166
from django .contrib .flatpages .models import FlatPage
160
167
rv = cache .get_models (app_mod = models )
@@ -164,9 +171,10 @@ def test_app_mod(self):
164
171
165
172
def test_include_auto_created (self ):
166
173
"""Test that auto created models are included if specified"""
167
- settings .INSTALLED_APPS = ('django.contrib.flatpages' ,)
168
- from django .contrib .flatpages . models import Site , FlatPage
174
+ settings .INSTALLED_APPS = ('django.contrib.sites' ,
175
+ ' django.contrib.flatpages' ,)
169
176
models = cache .get_models (include_auto_created = True )
177
+ from django .contrib .flatpages .models import Site , FlatPage
170
178
self .assertEqual (len (models ), 3 )
171
179
self .assertEqual (models [0 ], Site )
172
180
self .assertEqual (models [1 ].__name__ , 'FlatPage_sites' )
@@ -181,9 +189,11 @@ class GetModelTests(AppCacheTestCase):
181
189
182
190
def test_get_model (self ):
183
191
"""Test that the correct model is returned"""
184
- settings .INSTALLED_APPS = ('django.contrib.flatpages' ,)
192
+ settings .INSTALLED_APPS = ('django.contrib.sites' ,
193
+ 'django.contrib.flatpages' ,)
194
+ rv = cache .get_model ('flatpages' , 'FlatPage' )
185
195
from django .contrib .flatpages .models import FlatPage
186
- self .assertEqual (cache . get_model ( 'flatpages' , 'FlatPage' ) , FlatPage )
196
+ self .assertEqual (rv , FlatPage )
187
197
self .assertTrue (cache .app_cache_ready ())
188
198
189
199
def test_invalid (self ):
@@ -278,16 +288,17 @@ def test_register_models(self):
278
288
self .assertEqual (len (cache .app_instances ), 1 )
279
289
self .assertEqual (app .models [0 ].__name__ , 'Person' )
280
290
281
- def test_new_instance (self ):
291
+ def test_app_not_installed (self ):
282
292
"""
283
- Test a new app instance is created if one doesn't exist, and the
284
- models are attached to it .
293
+ Test that an exception is raised if models are tried to be registered
294
+ to an app that isn't listed in INSTALLED_APPS .
285
295
"""
286
- from model_app .models import Person
287
- app = cache .app_instances [0 ]
288
- self .assertEqual (len (cache .app_instances ), 1 )
289
- self .assertEqual (app .name , 'model_app' )
290
- self .assertEqual (app .models [0 ].__name__ , 'Person' )
296
+ try :
297
+ from model_app .models import Person
298
+ except ImproperlyConfigured :
299
+ pass
300
+ else :
301
+ self .fail ('ImproperlyConfigured not raised' )
291
302
292
303
if __name__ == '__main__' :
293
304
unittest .main ()
0 commit comments