Optimized some cache tests with set_many().

This commit is contained in:
oliver 2018-08-16 22:02:21 +09:00 committed by Tim Graham
parent 7f6b013bf6
commit 3ca24fd1c0
1 changed files with 6 additions and 17 deletions

23
tests/cache/tests.py vendored
View File

@ -92,10 +92,7 @@ class DummyCacheTests(SimpleTestCase):
def test_get_many(self): def test_get_many(self):
"get_many returns nothing for the dummy cache backend" "get_many returns nothing for the dummy cache backend"
cache.set('a', 'a') cache.set_many({'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd'})
cache.set('b', 'b')
cache.set('c', 'c')
cache.set('d', 'd')
self.assertEqual(cache.get_many(['a', 'c', 'd']), {}) self.assertEqual(cache.get_many(['a', 'c', 'd']), {})
self.assertEqual(cache.get_many(['a', 'b', 'e']), {}) self.assertEqual(cache.get_many(['a', 'b', 'e']), {})
@ -105,8 +102,7 @@ class DummyCacheTests(SimpleTestCase):
def test_delete(self): def test_delete(self):
"Cache deletion is transparently ignored on the dummy cache backend" "Cache deletion is transparently ignored on the dummy cache backend"
cache.set("key1", "spam") cache.set_many({'key1': 'spam', 'key2': 'eggs'})
cache.set("key2", "eggs")
self.assertIsNone(cache.get("key1")) self.assertIsNone(cache.get("key1"))
cache.delete("key1") cache.delete("key1")
self.assertIsNone(cache.get("key1")) self.assertIsNone(cache.get("key1"))
@ -306,18 +302,14 @@ class BaseCacheTests:
def test_get_many(self): def test_get_many(self):
# Multiple cache keys can be returned using get_many # Multiple cache keys can be returned using get_many
cache.set('a', 'a') cache.set_many({'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd'})
cache.set('b', 'b')
cache.set('c', 'c')
cache.set('d', 'd')
self.assertEqual(cache.get_many(['a', 'c', 'd']), {'a': 'a', 'c': 'c', 'd': 'd'}) self.assertEqual(cache.get_many(['a', 'c', 'd']), {'a': 'a', 'c': 'c', 'd': 'd'})
self.assertEqual(cache.get_many(['a', 'b', 'e']), {'a': 'a', 'b': 'b'}) self.assertEqual(cache.get_many(['a', 'b', 'e']), {'a': 'a', 'b': 'b'})
self.assertEqual(cache.get_many(iter(['a', 'b', 'e'])), {'a': 'a', 'b': 'b'}) self.assertEqual(cache.get_many(iter(['a', 'b', 'e'])), {'a': 'a', 'b': 'b'})
def test_delete(self): def test_delete(self):
# Cache keys can be deleted # Cache keys can be deleted
cache.set("key1", "spam") cache.set_many({'key1': 'spam', 'key2': 'eggs'})
cache.set("key2", "eggs")
self.assertEqual(cache.get("key1"), "spam") self.assertEqual(cache.get("key1"), "spam")
cache.delete("key1") cache.delete("key1")
self.assertIsNone(cache.get("key1")) self.assertIsNone(cache.get("key1"))
@ -521,9 +513,7 @@ class BaseCacheTests:
def test_delete_many(self): def test_delete_many(self):
# Multiple keys can be deleted using delete_many # Multiple keys can be deleted using delete_many
cache.set("key1", "spam") cache.set_many({'key1': 'spam', 'key2': 'eggs', 'key3': 'ham'})
cache.set("key2", "eggs")
cache.set("key3", "ham")
cache.delete_many(["key1", "key2"]) cache.delete_many(["key1", "key2"])
self.assertIsNone(cache.get("key1")) self.assertIsNone(cache.get("key1"))
self.assertIsNone(cache.get("key2")) self.assertIsNone(cache.get("key2"))
@ -531,8 +521,7 @@ class BaseCacheTests:
def test_clear(self): def test_clear(self):
# The cache can be emptied using clear # The cache can be emptied using clear
cache.set("key1", "spam") cache.set_many({'key1': 'spam', 'key2': 'eggs'})
cache.set("key2", "eggs")
cache.clear() cache.clear()
self.assertIsNone(cache.get("key1")) self.assertIsNone(cache.get("key1"))
self.assertIsNone(cache.get("key2")) self.assertIsNone(cache.get("key2"))