Refs #19914 -- Split the test_invalid_keys cache test into two.

The first half of the test fails when using pylibmc (so will need
to be skipped).
This commit is contained in:
Ed Morley 2016-08-28 02:59:28 +01:00 committed by Tim Graham
parent 488b3d2b38
commit cfd1f93d55
1 changed files with 24 additions and 25 deletions

49
tests/cache/tests.py vendored
View File

@ -572,7 +572,7 @@ class BaseCacheTests(object):
def test_zero_cull(self): def test_zero_cull(self):
self._perform_cull_test(caches['zero_cull'], 50, 19) self._perform_cull_test(caches['zero_cull'], 50, 19)
def test_invalid_keys(self): def _perform_invalid_key_test(self, key, expected_warning):
""" """
All the builtin backends (except memcached, see below) should warn on All the builtin backends (except memcached, see below) should warn on
keys that would be refused by memcached. This encourages portable keys that would be refused by memcached. This encourages portable
@ -590,35 +590,31 @@ class BaseCacheTests(object):
try: try:
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always") warnings.simplefilter("always")
# memcached does not allow whitespace or control characters in keys
key = 'key with spaces and 清'
cache.set(key, 'value') cache.set(key, 'value')
self.assertEqual(len(w), 1) self.assertEqual(len(w), 1)
self.assertIsInstance(w[0].message, CacheKeyWarning) self.assertIsInstance(w[0].message, CacheKeyWarning)
self.assertEqual( self.assertEqual(str(w[0].message.args[0]), expected_warning)
# warnings.warn() crashes on Python 2 if message isn't
# coercible to str.
str(w[0].message.args[0]),
"Cache key contains characters that will cause errors if used "
"with memcached: %r" % key,
)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
# memcached limits key length to 250
key = ('a' * 250) + ''
cache.set(key, 'value')
self.assertEqual(len(w), 1)
self.assertIsInstance(w[0].message, CacheKeyWarning)
self.assertEqual(
# warnings.warn() crashes on Python 2 if message isn't
# coercible to str.
str(w[0].message.args[0]),
'Cache key will cause errors if used with memcached: '
'%r (longer than %s)' % (key, 250),
)
finally: finally:
cache.key_func = old_func cache.key_func = old_func
def test_invalid_key_characters(self):
# memcached doesn't allow whitespace or control characters in keys.
key = 'key with spaces and 清'
expected_warning = (
"Cache key contains characters that will cause errors if used "
"with memcached: %r" % key
)
self._perform_invalid_key_test(key, expected_warning)
def test_invalid_key_length(self):
# memcached limits key length to 250.
key = ('a' * 250) + ''
expected_warning = (
'Cache key will cause errors if used with memcached: '
'%r (longer than %s)' % (key, 250)
)
self._perform_invalid_key_test(key, expected_warning)
def test_cache_versioning_get_set(self): def test_cache_versioning_get_set(self):
# set, using default version = 1 # set, using default version = 1
cache.set('answer1', 42) cache.set('answer1', 42)
@ -1162,7 +1158,7 @@ memcached_excluded_caches = {'cull', 'zero_cull'}
)) ))
class MemcachedCacheTests(BaseCacheTests, TestCase): class MemcachedCacheTests(BaseCacheTests, TestCase):
def test_invalid_keys(self): def test_invalid_key_characters(self):
""" """
On memcached, we don't introduce a duplicate key validation On memcached, we don't introduce a duplicate key validation
step (for speed reasons), we just let the memcached API step (for speed reasons), we just let the memcached API
@ -1172,8 +1168,11 @@ class MemcachedCacheTests(BaseCacheTests, TestCase):
that a generic exception of some kind is raised. that a generic exception of some kind is raised.
""" """
# memcached does not allow whitespace or control characters in keys # memcached does not allow whitespace or control characters in keys
# when using the ascii protocol.
with self.assertRaises(Exception): with self.assertRaises(Exception):
cache.set('key with spaces', 'value') cache.set('key with spaces', 'value')
def test_invalid_key_length(self):
# memcached limits key length to 250 # memcached limits key length to 250
with self.assertRaises(Exception): with self.assertRaises(Exception):
cache.set('a' * 251, 'value') cache.set('a' * 251, 'value')