Author: aaugustin
Date: 2011-10-26 15:47:04 -0700 (Wed, 26 Oct 2011)
New Revision: 17042

Modified:
   django/trunk/django/utils/cache.py
   django/trunk/tests/regressiontests/cache/tests.py
Log:
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.



Modified: django/trunk/django/utils/cache.py
===================================================================
--- django/trunk/django/utils/cache.py  2011-10-26 21:07:12 UTC (rev 17041)
+++ django/trunk/django/utils/cache.py  2011-10-26 22:47:04 UTC (rev 17042)
@@ -174,7 +174,7 @@
             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):

Modified: django/trunk/tests/regressiontests/cache/tests.py
===================================================================
--- django/trunk/tests/regressiontests/cache/tests.py   2011-10-26 21:07:12 UTC 
(rev 17041)
+++ django/trunk/tests/regressiontests/cache/tests.py   2011-10-26 22:47:04 UTC 
(rev 17042)
@@ -941,12 +941,16 @@
         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()
         request.META = {
@@ -1006,7 +1010,7 @@
         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,11 +1058,15 @@
 )(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()
         request.META = {
@@ -1112,17 +1120,22 @@
 )(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,10 +1180,10 @@
     )
     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')
@@ -1198,8 +1211,8 @@
         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,14 +1225,14 @@
         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',
@@ -1248,11 +1261,17 @@
     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):
         """
         Ensure the constructor is correctly distinguishing between usage of 
CacheMiddleware as
@@ -1488,7 +1507,11 @@
     """
     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()
         request.META = {
@@ -1561,9 +1584,14 @@
         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)
 
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to