Permalink
Browse files

Fixed several problems that hid one another in the cache tests and code.

1 - Used django.test.TestCase instead of unittest.TestCase so that the override_settings decorator works.
2 - Reverted parts of r17039 that caused failures (masked until 1).
3 - Isolated tests by clearing the cache in tear down (masked until 1). Refs #11505.
4 - Fixed a bug in cache key generation (revealed by 3).
5 - Fixed a test that relied on this bug -- hardcoding the generated cache keys in tests isn't a very good idea anyway.
6 - Uniformized some parts of the cache tests.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@17042 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
aaugustin committed Oct 26, 2011
1 parent 233cdcf commit 0cf139d20196cd055cae47b302370ec5442eac66
Showing with 51 additions and 23 deletions.
  1. +1 −1 django/utils/cache.py
  2. +50 −22 tests/regressiontests/cache/tests.py
View
@@ -174,7 +174,7 @@ def _generate_cache_key(request, method, headerlist, key_prefix):
ctx.update(value) ctx.update(value)
path = hashlib.md5(iri_to_uri(request.get_full_path())) path = hashlib.md5(iri_to_uri(request.get_full_path()))
cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % ( cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
key_prefix, request.method, path.hexdigest(), ctx.hexdigest()) key_prefix, method, path.hexdigest(), ctx.hexdigest())
return _i18n_cache_key_suffix(request, cache_key) return _i18n_cache_key_suffix(request, cache_key)
def _generate_cache_header_key(key_prefix, request): def _generate_cache_header_key(key_prefix, request):
@@ -941,11 +941,15 @@ def test_simple(self):
self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist') self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist')
class CacheUtils(unittest.TestCase): class CacheUtils(TestCase):
"""TestCase for django.utils.cache functions.""" """TestCase for django.utils.cache functions."""
def setUp(self): def setUp(self):
self.path = '/cache/test/' self.path = '/cache/test/'
self.cache = get_cache('default')
def tearDown(self):
self.cache.clear()
def _get_request(self, path, method='GET'): def _get_request(self, path, method='GET'):
request = HttpRequest() request = HttpRequest()
@@ -1006,7 +1010,7 @@ def test_learn_cache_key(self):
response['Vary'] = 'Pony' response['Vary'] = 'Pony'
# Make sure that the Vary header is added to the key hash # Make sure that the Vary header is added to the key hash
learn_cache_key(request, response) learn_cache_key(request, response)
self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.HEAD.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e') self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.GET.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
def test_patch_cache_control(self): def test_patch_cache_control(self):
tests = ( tests = (
@@ -1054,10 +1058,14 @@ def test_patch_cache_control(self):
)(CacheUtils) )(CacheUtils)
class CacheHEADTest(unittest.TestCase): class CacheHEADTest(TestCase):
def setUp(self): def setUp(self):
self.path = '/cache/test/' self.path = '/cache/test/'
self.cache = get_cache('default')
def tearDown(self):
self.cache.clear()
def _get_request(self, method): def _get_request(self, method):
request = HttpRequest() request = HttpRequest()
@@ -1112,17 +1120,22 @@ def test_head_with_cached_get(self):
)(CacheHEADTest) )(CacheHEADTest)
class CacheI18nTest(unittest.TestCase): class CacheI18nTest(TestCase):
def setUp(self): def setUp(self):
self.path = '/cache/test/' self.path = '/cache/test/'
self.cache = get_cache('default')
def _get_request(self): def tearDown(self):
self.cache.clear()
def _get_request(self, method='GET'):
request = HttpRequest() request = HttpRequest()
request.META = { request.META = {
'SERVER_NAME': 'testserver', 'SERVER_NAME': 'testserver',
'SERVER_PORT': 80, 'SERVER_PORT': 80,
} }
request.method = method
request.path = request.path_info = self.path request.path = request.path_info = self.path
return request return request
@@ -1167,10 +1180,10 @@ def test_cache_key_no_i18n (self):
) )
def test_middleware(self): def test_middleware(self):
def set_cache(request, lang, msg): def set_cache(request, lang, msg):
with translation.override(lang): translation.activate(lang)
response = HttpResponse() response = HttpResponse()
response.content= msg response.content = msg
return UpdateCacheMiddleware().process_response(request, response) return UpdateCacheMiddleware().process_response(request, response)
# cache with non empty request.GET # cache with non empty request.GET
request = self._get_request_cache(query_string='foo=bar&other=true') request = self._get_request_cache(query_string='foo=bar&other=true')
@@ -1198,8 +1211,8 @@ def set_cache(request, lang, msg):
set_cache(request, 'en', en_message) set_cache(request, 'en', en_message)
get_cache_data = FetchFromCacheMiddleware().process_request(request) get_cache_data = FetchFromCacheMiddleware().process_request(request)
# Check that we can recover the cache # Check that we can recover the cache
self.assertNotEqual(get_cache_data.content, None) self.assertNotEqual(get_cache_data, None)
self.assertEqual(en_message, get_cache_data.content) self.assertEqual(get_cache_data.content, en_message)
# Check that we use etags # Check that we use etags
self.assertTrue(get_cache_data.has_header('ETag')) self.assertTrue(get_cache_data.has_header('ETag'))
# Check that we can disable etags # Check that we can disable etags
@@ -1212,14 +1225,14 @@ def set_cache(request, lang, msg):
request = self._get_request_cache() request = self._get_request_cache()
set_cache(request, 'es', es_message) set_cache(request, 'es', es_message)
# change again the language # change again the language
with translation.override('en'): translation.activate('en')
# retrieve the content from cache # retrieve the content from cache
get_cache_data = FetchFromCacheMiddleware().process_request(request) get_cache_data = FetchFromCacheMiddleware().process_request(request)
self.assertEqual(get_cache_data.content, en_message) self.assertEqual(get_cache_data.content, en_message)
# change again the language # change again the language
with translation.override('es'): translation.activate('es')
get_cache_data = FetchFromCacheMiddleware().process_request(request) get_cache_data = FetchFromCacheMiddleware().process_request(request)
self.assertEqual(get_cache_data.content, es_message) self.assertEqual(get_cache_data.content, es_message)
CacheI18nTest = override_settings( CacheI18nTest = override_settings(
CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix', CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
@@ -1248,10 +1261,16 @@ def hello_world_view(request, value):
return HttpResponse('Hello World %s' % value) return HttpResponse('Hello World %s' % value)
class CacheMiddlewareTest(unittest.TestCase): class CacheMiddlewareTest(TestCase):
def setUp(self): def setUp(self):
self.factory = RequestFactory() self.factory = RequestFactory()
self.default_cache = get_cache('default')
self.other_cache = get_cache('other')
def tearDown(self):
self.default_cache.clear()
self.other_cache.clear()
def test_constructor(self): def test_constructor(self):
""" """
@@ -1488,6 +1507,10 @@ class TestWithTemplateResponse(TestCase):
""" """
def setUp(self): def setUp(self):
self.path = '/cache/test/' self.path = '/cache/test/'
self.cache = get_cache('default')
def tearDown(self):
self.cache.clear()
def _get_request(self, path, method='GET'): def _get_request(self, path, method='GET'):
request = HttpRequest() request = HttpRequest()
@@ -1561,9 +1584,14 @@ def test_with_etag(self):
self.assertTrue(response.has_header('ETag')) self.assertTrue(response.has_header('ETag'))
TestWithTemplateResponse = override_settings( TestWithTemplateResponse = override_settings(
CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix', CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
CACHE_MIDDLEWARE_SECONDS=1, CACHE_MIDDLEWARE_SECONDS=1,
USE_I18N=False, CACHES={
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
},
USE_I18N=False,
)(TestWithTemplateResponse) )(TestWithTemplateResponse)

0 comments on commit 0cf139d

Please sign in to comment.