From 047c1d48a613cc2a16f078a9094cc799f06e6b0c Mon Sep 17 00:00:00 2001 From: Ed Morley Date: Mon, 29 Aug 2016 15:23:54 +0100 Subject: [PATCH] Fixed #27132 -- Allowed testing MemcachedCache and PyLibMCCache during the same test run. --- tests/cache/tests.py | 76 ++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/tests/cache/tests.py b/tests/cache/tests.py index c8059ec1c8..57ac99c8e8 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -1142,21 +1142,18 @@ class LocMemCacheTests(BaseCacheTests, TestCase): # To check the memcached backend, the test settings file will # need to contain at least one cache backend setting that points at # your memcache server. -memcached_params = {} +configured_caches = {} for _cache_params in settings.CACHES.values(): - if _cache_params['BACKEND'].startswith('django.core.cache.backends.memcached.'): - memcached_params = _cache_params + configured_caches[_cache_params['BACKEND']] = _cache_params + +MemcachedCache_params = configured_caches.get('django.core.cache.backends.memcached.MemcachedCache') +PyLibMCCache_params = configured_caches.get('django.core.cache.backends.memcached.PyLibMCCache') # The memcached backends don't support cull-related options like `MAX_ENTRIES`. memcached_excluded_caches = {'cull', 'zero_cull'} -@unittest.skipUnless(memcached_params, "memcached not available") -@override_settings(CACHES=caches_setting_for_tests( - base=memcached_params, - exclude=memcached_excluded_caches, -)) -class MemcachedCacheTests(BaseCacheTests, TestCase): +class BaseMemcachedTests(BaseCacheTests): def test_invalid_key_characters(self): """ @@ -1177,36 +1174,24 @@ class MemcachedCacheTests(BaseCacheTests, TestCase): with self.assertRaises(Exception): cache.set('a' * 251, 'value') - # Explicitly display a skipped test if no configured cache uses MemcachedCache - @unittest.skipUnless( - memcached_params.get('BACKEND') == 'django.core.cache.backends.memcached.MemcachedCache', - "cache with python-memcached library not available") - def test_memcached_uses_highest_pickle_version(self): - # Regression test for #19810 - for cache_key, cache_config in settings.CACHES.items(): - if cache_config['BACKEND'] == 'django.core.cache.backends.memcached.MemcachedCache': - self.assertEqual(caches[cache_key]._cache.pickleProtocol, - pickle.HIGHEST_PROTOCOL) - - @override_settings(CACHES=caches_setting_for_tests( - base=memcached_params, - exclude=memcached_excluded_caches, - TIMEOUT=None, - )) def test_default_never_expiring_timeout(self): # Regression test for #22845 - cache.set('infinite_foo', 'bar') - self.assertEqual(cache.get('infinite_foo'), 'bar') + with self.settings(CACHES=caches_setting_for_tests( + base=self.base_params, + exclude=memcached_excluded_caches, + TIMEOUT=None)): + cache.set('infinite_foo', 'bar') + self.assertEqual(cache.get('infinite_foo'), 'bar') - @override_settings(CACHES=caches_setting_for_tests( - base=memcached_params, - exclude=memcached_excluded_caches, - TIMEOUT=31536000, # 60*60*24*365, 1 year - )) def test_default_far_future_timeout(self): # Regression test for #22845 - cache.set('future_foo', 'bar') - self.assertEqual(cache.get('future_foo'), 'bar') + with self.settings(CACHES=caches_setting_for_tests( + base=self.base_params, + exclude=memcached_excluded_caches, + # 60*60*24*365, 1 year + TIMEOUT=31536000)): + cache.set('future_foo', 'bar') + self.assertEqual(cache.get('future_foo'), 'bar') def test_cull(self): # culling isn't implemented, memcached deals with it. @@ -1235,6 +1220,29 @@ class MemcachedCacheTests(BaseCacheTests, TestCase): self.assertTrue(value is None or value == large_value) +@unittest.skipUnless(MemcachedCache_params, "MemcachedCache backend not configured") +@override_settings(CACHES=caches_setting_for_tests( + base=MemcachedCache_params, + exclude=memcached_excluded_caches, +)) +class MemcachedCacheTests(BaseMemcachedTests, TestCase): + base_params = MemcachedCache_params + + def test_memcached_uses_highest_pickle_version(self): + # Regression test for #19810 + for cache_key, cache_config in settings.CACHES.items(): + self.assertEqual(caches[cache_key]._cache.pickleProtocol, pickle.HIGHEST_PROTOCOL) + + +@unittest.skipUnless(PyLibMCCache_params, "PyLibMCCache backend not configured") +@override_settings(CACHES=caches_setting_for_tests( + base=PyLibMCCache_params, + exclude=memcached_excluded_caches, +)) +class PyLibMCCacheTests(BaseMemcachedTests, TestCase): + base_params = PyLibMCCache_params + + @override_settings(CACHES=caches_setting_for_tests( BACKEND='django.core.cache.backends.filebased.FileBasedCache', ))