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
This commit is contained in:
Aymeric Augustin 2011-10-26 22:47:04 +00:00
parent 233cdcf424
commit 0cf139d201
2 changed files with 51 additions and 23 deletions

View File

@ -174,7 +174,7 @@ def _generate_cache_key(request, method, headerlist, key_prefix):
ctx.update(value)
path = hashlib.md5(iri_to_uri(request.get_full_path()))
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)
def _generate_cache_header_key(key_prefix, request):

View File

@ -941,11 +941,15 @@ class GetCacheTests(unittest.TestCase):
self.assertRaises(InvalidCacheBackendError, get_cache, 'does_not_exist')
class CacheUtils(unittest.TestCase):
class CacheUtils(TestCase):
"""TestCase for django.utils.cache functions."""
def setUp(self):
self.path = '/cache/test/'
self.cache = get_cache('default')
def tearDown(self):
self.cache.clear()
def _get_request(self, path, method='GET'):
request = HttpRequest()
@ -1006,7 +1010,7 @@ class CacheUtils(unittest.TestCase):
response['Vary'] = 'Pony'
# Make sure that the Vary header is added to the key hash
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):
tests = (
@ -1054,10 +1058,14 @@ PrefixedCacheUtils = override_settings(
)(CacheUtils)
class CacheHEADTest(unittest.TestCase):
class CacheHEADTest(TestCase):
def setUp(self):
self.path = '/cache/test/'
self.cache = get_cache('default')
def tearDown(self):
self.cache.clear()
def _get_request(self, method):
request = HttpRequest()
@ -1112,17 +1120,22 @@ CacheHEADTest = override_settings(
)(CacheHEADTest)
class CacheI18nTest(unittest.TestCase):
class CacheI18nTest(TestCase):
def setUp(self):
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.META = {
'SERVER_NAME': 'testserver',
'SERVER_PORT': 80,
}
request.method = method
request.path = request.path_info = self.path
return request
@ -1167,9 +1180,9 @@ class CacheI18nTest(unittest.TestCase):
)
def test_middleware(self):
def set_cache(request, lang, msg):
with translation.override(lang):
translation.activate(lang)
response = HttpResponse()
response.content= msg
response.content = msg
return UpdateCacheMiddleware().process_response(request, response)
# cache with non empty request.GET
@ -1198,8 +1211,8 @@ class CacheI18nTest(unittest.TestCase):
set_cache(request, 'en', en_message)
get_cache_data = FetchFromCacheMiddleware().process_request(request)
# Check that we can recover the cache
self.assertNotEqual(get_cache_data.content, None)
self.assertEqual(en_message, get_cache_data.content)
self.assertNotEqual(get_cache_data, None)
self.assertEqual(get_cache_data.content, en_message)
# Check that we use etags
self.assertTrue(get_cache_data.has_header('ETag'))
# Check that we can disable etags
@ -1212,12 +1225,12 @@ class CacheI18nTest(unittest.TestCase):
request = self._get_request_cache()
set_cache(request, 'es', es_message)
# change again the language
with translation.override('en'):
translation.activate('en')
# retrieve the content from cache
get_cache_data = FetchFromCacheMiddleware().process_request(request)
self.assertEqual(get_cache_data.content, en_message)
# change again the language
with translation.override('es'):
translation.activate('es')
get_cache_data = FetchFromCacheMiddleware().process_request(request)
self.assertEqual(get_cache_data.content, es_message)
@ -1248,10 +1261,16 @@ def hello_world_view(request, value):
return HttpResponse('Hello World %s' % value)
class CacheMiddlewareTest(unittest.TestCase):
class CacheMiddlewareTest(TestCase):
def setUp(self):
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):
"""
@ -1488,6 +1507,10 @@ class TestWithTemplateResponse(TestCase):
"""
def setUp(self):
self.path = '/cache/test/'
self.cache = get_cache('default')
def tearDown(self):
self.cache.clear()
def _get_request(self, path, method='GET'):
request = HttpRequest()
@ -1563,6 +1586,11 @@ class TestWithTemplateResponse(TestCase):
TestWithTemplateResponse = override_settings(
CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
CACHE_MIDDLEWARE_SECONDS=1,
CACHES={
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
},
USE_I18N=False,
)(TestWithTemplateResponse)