from cache.FastCache import FastCache 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_none_is_returned_when_not_found(): cache = FastCache() assert cache.get("foo") is None 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"]