8000 add appcache tests · ddriddle/django@ff3c1c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff3c1c6

Browse files
committed
add appcache tests
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13386 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent c362007 commit ff3c1c6

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed

tests/appcachetests.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import copy
2+
import sys
3+
import unittest
4+
import threading
5+
from django.conf import settings
6+
from django.utils.datastructures import SortedDict
7+
from django.core.exceptions import ImproperlyConfigured
8+
9+
# remove when tests are integrated into the django testsuite
10+
settings.configure()
11+
12+
from django.db.models.loading import cache
13+
14+
class AppCacheTestCase(unittest.TestCase):
15+
"""
16+
TestCase that resets the AppCache after each test.
17+
"""
18+
def setUp(self):
19+
self.old_installed_apps = settings.INSTALLED_APPS
20+
settings.INSTALLED_APPS = ()
21+
22+
def tearDown(self):
23+
settings.INSTALLED_APPS = self.old_installed_apps
24+
25+
# The appcache imports models modules. We need to delete the
26+
# imported module from sys.modules after the test has run.
27+
# If the module is imported again, the ModelBase.__new__ can
28+
# register the models with the appcache anew.
29+
# Some models modules import other models modules (for example
30+
# django.contrib.auth relies on django.contrib.contenttypes).
31+
# To detect which model modules have been imported, we go through
32+
# all loaded model classes and remove their respective module
33+
# from sys.modules
34+
for app in cache.app_models.itervalues():
35+
for name in app.itervalues():
36+
module = name.__module__
37+
if module in sys.modules:
38+
del sys.modules[module]
39+
40+
# we cannot copy() the whole cache.__dict__ in the setUp function
41+
# because thread.RLock is un(deep)copyable
42+
cache.app_store = SortedDict()
43+
cache.app_models = SortedDict()
44+
cache.app_errors = {}
45+
cache.loaded = False
46+
cache.handled = {}
47+
cache.postponed = []
48+
cache.nesting_level = 0
49+
cache.write_lock = threading.RLock()
50+
cache._get_models_cache = {}
51+
52+
class AppCacheReadyTests(AppCacheTestCase):
53+
"""
54+
Tests for the app_cache_ready function that indicates if the cache
55+
is fully populated.
56+
"""
57+
def test_not_initialized(self):
58+
"""Should return False if the AppCache hasn't been initialized"""
59+
self.assertFalse(cache.app_cache_ready())
60+
61+
def test_load_app(self):
62+
"""Should return False after executing the load_app function"""
63+
cache.load_app('django.contrib.comments')
64+
self.assertFalse(cache.app_cache_ready())
65+
cache.load_app('django.contrib.comments', can_postpone=True)
66+
self.assertFalse(cache.app_cache_ready())
67+
68+
class GetAppsTests(AppCacheTestCase):
69+
"""Tests for the get_apps function"""
70+
def test_get_apps(self):
71+
"""Test that the correct models modules are returned"""
72+
settings.INSTALLED_APPS = ('django.contrib.auth',
73+
'django.contrib.flatpages',)
74+
apps = cache.get_apps()
75+
self.assertEqual(len(apps), 2)
76+
self.assertTrue(apps[0], 'django.contrib.auth.models')
77+
self.assertTrue(apps[1], 'django.contrib.flatpages.models')
78+
self.assertTrue(cache.app_cache_ready())
79+
80+
def test_empty_models(self):
81+
"""Test that modules that don't contain models are not returned"""
82+
settings.INSTALLED_APPS = ('django.contrib.csrf',)
83+
self.assertEqual(cache.get_apps(), [])
84+
self.assertTrue(cache.app_cache_ready())
85+
86+
class GetAppTests(AppCacheTestCase):
87+
"""Tests for the get_app function"""
88+
def test_get_app(self):
89+
"""Test that the correct module is returned"""
90+
settings.INSTALLED_APPS = ('django.contrib.auth',)
91+
module = cache.get_app('auth')
92+
self.assertTrue(module, 'django.contrib.auth.models')
93+
self.assertTrue(cache.app_cache_ready())
94+
95+
def test_not_found_exception(self):
96+
"""
97+
Test that an ImproperlyConfigured exception is raised if an app
98+
could not be found
99+
"""
100+
self.assertRaises(ImproperlyConfigured, cache.get_app,
101+
'django.contrib.auth')
102+
self.assertTrue(cache.app_cache_ready())
103+
104+
def test_emptyOK(self):
105+
"""
106+
Test that None is returned if emptyOK is True and the module
107+
has no models
108+
"""
109+
settings.INSTALLED_APPS = ('django.contrib.csrf',)
110+
module = cache.get_app('csrf', emptyOK=True)
111+
self.failUnless(module is None)
112+
self.assertTrue(cache.app_cache_ready())
113+
114+
def test_load_app_modules(self):
115+
"""
116+
Test that only apps that are listed in the INSTALLED_APPS setting
117+
are searched (unlike the get_apps function, which also searches
118+
apps that are loaded via load_app)
119+
"""
120+
cache.load_app('django.contrib.sites')
121+
self.assertRaises(ImproperlyConfigured, cache.get_app, 'sites')
122+
self.assertTrue(cache.app_cache_ready())
123+
124+
class GetAppErrorsTests(AppCacheTestCase):
125+
"""Tests for the get_app_errors function"""
126+
def test_get_app_errors(self):
127+
"""Test that the function returns an empty dict"""
128+
self.assertEqual(cache.get_app_errors(), {})
129+
self.assertTrue(cache.app_cache_ready())
130+
131+
class GetModelsTests(AppCacheTestCase):
132+
"""Tests for the get_models function"""
133+
def test_get_models(self):
134+
"""Test that the correct model classes are returned"""
135+
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
136+
from django.contrib.flatpages.models import Site, FlatPage
137+
models = cache.get_models()
138+
self.assertEqual(len(models), 2)
139+
self.assertEqual(models[0], Site)
140+
self.assertEqual(models[1], FlatPage)
141+
self.assertTrue(cache.app_cache_ready())
142+
143+
def test_app_mod(self):
144+
"""
145+
Test that the correct model classes are returned if an
146+
app module is specified
147+
"""
148+
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
149+
from django.contrib.flatpages import models
150+
from django.contrib.flatpages.models import FlatPage
151+
rv = cache.get_models(app_mod=models)
152+
self.assertEqual(len(rv), 1)
153+
self.assertEqual(rv[0], FlatPage)
154+
self.assertTrue(cache.app_cache_ready())
155+
156+
def test_include_auto_created(self):
157+
"""Test that auto created models are included if specified"""
158+
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
159+
from django.contrib.flatpages.models import Site, FlatPage
160+
models = cache.get_models(include_auto_created=True)
161+
self.assertEqual(len(models), 3)
162+
self.assertEqual(models[0], Site)
163+
self.assertEqual(models[1].__name__, 'FlatPage_sites')
164+
self.assertEqual(models[2], FlatPage)
165+
self.assertTrue(cache.app_cache_ready())
166+
167+
def test_include_deferred(self):
168+
"""TODO!"""
169+
170+
class GetModelTests(AppCacheTestCase):
171+
"""Tests for the get_model function"""
172+
def test_get_model(self):
173+
"""Test that the correct model is returned"""
174+
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
175+
from django.contrib.flatpages.models import FlatPage
176+
self.assertEqual(cache.get_model('flatpages', 'FlatPage'), FlatPage)
177+
self.assertTrue(cache.app_cache_ready())
178+
179+
def test_invalid(self):
180+
"""Test that None is returned if an app/model does not exist"""
181+
self.assertEqual(cache.get_model('foo', 'bar'), None)
182+
self.assertTrue(cache.app_cache_ready())
183+
184+
def test_without_seeding(self):
185+
"""Test that None is returned if the cache is not seeded"""
186+
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
187+
rv = cache.get_model('flatpages', 'FlatPage', seed_cache=False)
188+
self.assertEqual(rv, None)
189+
self.assertFalse(cache.app_cache_ready())
190+
191+
class RegisterModelsTests(AppCacheTestCase):
192+
"""Tests for the register_models function"""
193+
def test_register_models(self):
194+
from django.contrib.flatpages.models import FlatPage, Site
195+
cache.register_models('foo', *(FlatPage, Site,))
196+
self.assertFalse(cache.app_cache_ready())
197+
rv = cache.get_models()
198+
self.assertEqual(len(rv), 4)
199+
self.assertEqual(rv[0], Site)
200+
self.assertEqual(rv[1], FlatPage)
201+
self.assertEqual(rv[2], FlatPage)
202+
self.assertEqual(rv[3], Site)
203+
204+
if __name__ == '__main__':
205+
unittest.main()
206+

0 commit comments

Comments
 (0)
0