From 9e04b242eedc42d8d77431ef71ffa5f23eadacdb Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Thu, 20 Aug 2020 17:18:25 +0100 Subject: [PATCH] Refs #31907 -- Added cache key validation tests for cache operations. --- tests/cache/tests.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 60a4ce51bbc..260717c0b63 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -650,10 +650,23 @@ class BaseCacheTests: old_func = cache.key_func cache.key_func = func + tests = [ + ('add', [key, 1]), + ('get', [key]), + ('set', [key, 1]), + ('incr', [key]), + ('decr', [key]), + ('delete', [key]), + ('get_many', [[key, 'b']]), + ('set_many', [{key: 1, 'b': 2}]), + ('delete_many', [{key: 1, 'b': 2}]), + ] try: - with self.assertWarns(CacheKeyWarning) as cm: - cache.set(key, 'value') - self.assertEqual(str(cm.warning), expected_warning) + for operation, args in tests: + with self.subTest(operation=operation): + with self.assertWarns(CacheKeyWarning) as cm: + getattr(cache, operation)(*args) + self.assertEqual(str(cm.warning), expected_warning) finally: cache.key_func = old_func @@ -1291,9 +1304,19 @@ class BaseMemcachedTests(BaseCacheTests): key. """ msg = expected_warning.replace(key, cache.make_key(key)) - with self.assertRaises(InvalidCacheKey) as cm: - cache.set(key, 'value') - self.assertEqual(str(cm.exception), msg) + tests = [ + ('add', [key, 1]), + ('set', [key, 1]), + ('incr', [key]), + ('decr', [key]), + ('get_many', [[key, 'b']]), + ('set_many', [{key: 1, 'b': 2}]), + ] + for operation, args in tests: + with self.subTest(operation=operation): + with self.assertRaises(InvalidCacheKey) as cm: + getattr(cache, operation)(*args) + self.assertEqual(str(cm.exception), msg) def test_default_never_expiring_timeout(self): # Regression test for #22845