def test_cache_set_many(cache: Cache): """Test that cache.set_many() sets multiple cache key/values.""" items = {"a": 1, "b": 2, "c": 3} cache.set_many(items) for key, value in items.items(): assert cache.get(key) == value
def test_cache_clear(cache: Cache): """Test that cache.clear() deletes all cache keys.""" items = {"a": 1, "b": 2, "c": 3} cache.set_many(items) assert len(cache) == len(items) cache.clear() assert len(cache) == 0
def test_cache_copy(cache: Cache): """Test that cache.copy() returns a copy of the cache.""" items = {"a": 1, "b": 2, "c": 3} cache.set_many(items) copied = cache.copy() assert copied == items assert copied is not cache._cache
def test_cache_delete_many(cache: Cache, items: dict, iteratee: t.Union[list, str, t.Pattern, t.Callable], expected: dict): """Test that cache.delete_many() deletes multiple cache key/values filtered by an iteratee.""" cache.set_many(items) cache.delete_many(iteratee) assert dict(cache.items()) == expected
def test_cache_iter(cache: Cache): """Test that iterating over cache yields each cache key.""" items: dict = {"a": 1, "b": 2, "c": 3} cache.set_many(items) keys = [] for key in cache: assert cache.get(key) == items[key] keys.append(key) assert set(keys) == set(items)
def test_cache_get_many__ttl_expires_during_call(items: dict, iteratee: t.Union[list, str, t.Pattern, t.Callable], expected: dict): """Test that cache.get_many() returns without error when cache keys expire during call.""" cache = Cache(ttl=1, timer=lambda: 0) cache.set_many(items) assert cache.get_many(iteratee) == expected cache.timer = lambda: 100 assert cache.get_many(iteratee) == {}
@cache.memoize() def func(a, b): pass func.uncached(1, 2) assert cache.copy() == {1: "foobar", 2: ("foo", "bar", "baz")} cache.delete(1) assert cache.get(1) is None cache.clear() assert len(cache) == 0 cache.set_many({"a": 1, "b": 2, "c": 3}) assert cache.get_many(["a", "b", "c"]) == {"a": 1, "b": 2, "c": 3} cache.delete_many(["a", "b", "c"]) assert cache.count() == 0 import re cache.set_many({"a_1": 1, "a_2": 2, "123": 3, "b": 4}) # func.uncached(1, 2) # assert cache.copy() == {1: "foobar", 2: ("foo", "bar", "baz")} cache.set_many({"a": 1, "b": 2, "c": 3}) assert list(cache.keys()) == ["a", "b", "c"] assert list(cache.values()) == [1, 2, 3]
def test_cache_items(cache: Cache): """Test that cache.items() returns all cache key/values.""" items = {"a": 1, "b": 2, "c": 3} cache.set_many(items) assert set(cache.items()) == set(items.items())
def test_cache_values(cache: Cache): """Test that cache.values() returns all cache values.""" items = {"a": 1, "b": 2, "c": 3} cache.set_many(items) assert sorted(cache.values()) == sorted(items.values())
def test_cache_get_many(cache: Cache, items: dict, iteratee: t.Union[list, str, t.Pattern, t.Callable], expected: dict): """Test that cache.get_many() returns multiple cache key/values filtered by an iteratee.""" cache.set_many(items) assert cache.get_many(iteratee) == expected
# func.uncached(1, 2) # 复制机制 # assert cache.copy() == {1: 'foobar', 2: ('foo', 'bar', 'baz')} # 删除缓存中的一个键值对 cache.delete(1) assert cache.get(1) is None # 清除整个缓存 print(len(cache)) cache.clear() assert len(cache) == 0 # 为get, set, delete设置了批量方法 cache.set_many({"a": 1, "b": 2, "c": 3}) assert cache.get_many(["a", "b", "c"]) print(len(cache)) assert cache.delete_many(["a", "b", "c"]) print(len(cache)) # 重置已经初始化的缓存对象 cache.configure(maxsize=1000, ttl=5 * 60) cache.set_many({'a': 1, 'b': 2, 'c': 3}) assert list(cache.keys()) == ['a', 'b', 'c'] assert list(cache.values()) == [1, 2, 3] assert list(cache.items()) == [('a', 1), ('b', 2), ('c', 3)] # 迭代整个缓存的key for key in cache: print(key, cache.get(key))