Commits

Anonymous committed da2fe74

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.

Comments (0)

Files changed (2)

django/utils/cache.py

             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):

tests/regressiontests/cache/tests.py

         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()
         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 = (
 )(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()
 )(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
 
     )
     def test_middleware(self):
         def set_cache(request, lang, msg):
-            with translation.override(lang):
-                response = HttpResponse()
-                response.content= msg
-                return UpdateCacheMiddleware().process_response(request, response)
+            translation.activate(lang)
+            response = HttpResponse()
+            response.content = msg
+            return UpdateCacheMiddleware().process_response(request, response)
 
         # cache with non empty request.GET
         request = self._get_request_cache(query_string='foo=bar&other=true')
         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
         request = self._get_request_cache()
         set_cache(request, 'es', es_message)
         # change again the language
-        with translation.override('en'):
-            # retrieve the content from cache
-            get_cache_data = FetchFromCacheMiddleware().process_request(request)
-            self.assertEqual(get_cache_data.content, en_message)
+        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'):
-            get_cache_data = FetchFromCacheMiddleware().process_request(request)
-            self.assertEqual(get_cache_data.content, es_message)
+        translation.activate('es')
+        get_cache_data = FetchFromCacheMiddleware().process_request(request)
+        self.assertEqual(get_cache_data.content, es_message)
 
 CacheI18nTest = override_settings(
         CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
     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):
         """
     """
     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()
         self.assertTrue(response.has_header('ETag'))
 
 TestWithTemplateResponse = override_settings(
-    CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
-    CACHE_MIDDLEWARE_SECONDS=1,
-    USE_I18N=False,
+        CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
+        CACHE_MIDDLEWARE_SECONDS=1,
+        CACHES={
+            'default': {
+                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
+            },
+        },
+        USE_I18N=False,
 )(TestWithTemplateResponse)