Files
Sheerka/tests/caching/test_FastCache.py

151 lines
4.1 KiB
Python

from caching.FastCache import FastCache
from common.global_symbols import NotFound
def test_i_can_put_an_retrieve_values():
cache = FastCache()
cache.put("key", "value")
assert cache.get("key") == "value"
assert cache.cache == {"key": "value"}
assert cache.lru == ["key"]
def test_i_can_put_and_retrieve_multiple_items():
cache = FastCache()
cache.put("key1", "value1")
cache.put("key2", "value2")
cache.put("key3", "value3")
assert cache.cache == {"key1": "value1", "key2": "value2", "key3": "value3"}
assert cache.lru == ["key1", "key2", "key3"]
def test_i_the_least_used_is_remove_first():
cache = FastCache(3)
cache.put("key1", "value1")
cache.put("key2", "value2")
cache.put("key3", "value3")
cache.put("key4", "value4")
assert cache.cache == {"key4": "value4", "key2": "value2", "key3": "value3"}
assert cache.lru == ["key2", "key3", "key4"]
cache.put("key5", "value5")
assert cache.cache == {"key4": "value4", "key5": "value5", "key3": "value3"}
assert cache.lru == ["key3", "key4", "key5"]
def test_i_can_put_the_same_key_several_times():
cache = FastCache()
cache.put("key1", "value1")
cache.put("key2", "value2")
cache.put("key1", "value3")
assert cache.cache == {"key1": "value3", "key2": "value2"}
assert cache.lru == ["key2", "key1"]
def test_not_found_is_returned_when_not_found():
cache = FastCache()
assert cache.get("foo") is NotFound
def test_i_can_remove_an_item():
cache = FastCache()
cache.put("key1", "value1")
cache.put("to_keep1", "to_keep_value1")
cache.remove("key1")
assert cache.cache == {"to_keep1": "to_keep_value1"}
def test_i_can_evict_by_key():
cache = FastCache()
cache.put("key1", "value1")
cache.put("to_keep1", "to_keep_value1")
cache.put("key2", "value2")
cache.put("to_keep2", "to_keep_value2")
cache.put("key3", "value3")
cache.put("to_keep3", "to_keep_value3")
cache.evict_by_key(lambda k: k.startswith("key"))
assert cache.cache == {"to_keep1": "to_keep_value1",
"to_keep2": "to_keep_value2",
"to_keep3": "to_keep_value3"}
assert cache.lru == ["to_keep1", "to_keep2", "to_keep3"]
def test_i_can_get_default_value():
cache = FastCache(max_size=3, default=lambda key: key + 1)
assert cache.get(1) == 2
assert cache.get(2) == 3
assert cache.get(3) == 4
assert cache.get(4) == 5
assert cache.cache == {2: 3, 3: 4, 4: 5} # only 3 values
def test_i_can_iter_on_entries():
cache = FastCache()
cache.put("key1", "value1")
cache.put("key2", "value2")
cache.put("key3", "value3")
res = []
for k in cache:
assert k in cache
res.append(k)
assert res == ["key1", "key2", "key3"]
def test_i_can_count():
cache = FastCache()
cache.put("key1", "value1")
cache.put("key2", "value2")
cache.put("key3", "value3")
assert len(cache) == 3
def test_i_can_copy():
cache = FastCache()
cache.put("key1", "value1")
cache.put("key2", "value2")
cache.put("key3", "value3")
assert cache.copy() == {"key1": "value1", "key2": "value2", "key3": "value3"}
def test_i_can_take_snapshots_and_revert():
# Test that I can create restoration points
# and come back later to them
cache = FastCache()
cache.put("key1", "value1")
cache.snapshot()
cache.put("key2", "value2")
cache.put("key3", "value3")
cache.snapshot()
cache.put("key4", "value4")
cache.put("key5", "value5")
assert cache.cache == {"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5"}
cache.revert_snapshot()
assert cache.cache == {"key1": "value1",
"key2": "value2",
"key3": "value3"}
cache.revert_snapshot()
assert cache.cache == {"key1": "value1"}
cache.revert_snapshot() # no effect if nothing to revert
assert cache.cache == {"key1": "value1"}