Added first version of DebugManager. Implemented draft of the rule engine

This commit is contained in:
2020-11-20 13:41:45 +01:00
parent cd066881b4
commit 315f8ea09b
156 changed files with 8388 additions and 2852 deletions
+67
View File
@@ -0,0 +1,67 @@
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"]
+32
View File
@@ -565,3 +565,35 @@ class TestCache(TestUsingMemoryBasedSheerka):
cache.get(str(MAX_INITIALIZED_KEY + 1))
assert len(cache._initialized_keys) == 1
def test_i_can_populate(self):
items = [("1", "1"), ("2", "2"), ("3", "3")]
cache = Cache()
cache.populate(lambda: items, lambda item: item[0])
assert len(cache) == 3
assert cache.get("1") == ("1", "1")
assert cache.get("2") == ("2", "2")
assert cache.get("3") == ("3", "3")
def test_max_size_is_respected_when_populate(self):
items = [("1", "1"), ("2", "2"), ("3", "3"), ("4", "4"), ("5", "5")]
cache = Cache(max_size=3)
cache.populate(lambda: items, lambda item: item[0])
assert len(cache) == 3
assert cache.get("3") == ("3", "3")
assert cache.get("4") == ("4", "4")
assert cache.get("5") == ("5", "5")
def test_i_can_get_all(self):
items = [("1", "1"), ("2", "2"), ("3", "3")]
cache = Cache()
cache.populate(lambda: items, lambda item: item[0])
res = cache.get_all()
assert len(res) == 3
assert list(res) == [('1', '1'), ('2', '2'), ('3', '3')]