Implemented SheerkaOntology
This commit is contained in:
Vendored
+84
@@ -0,0 +1,84 @@
|
||||
from cache.IncCache import IncCache
|
||||
from core.global_symbols import NotFound, Removed
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
from tests.cache import FakeSdp
|
||||
|
||||
|
||||
class FakeIncSdp:
|
||||
def __init__(self, init_value1, init_value2):
|
||||
self.internals = [IncCache(), IncCache()]
|
||||
if init_value1:
|
||||
self.internals[0].put("key", init_value1)
|
||||
|
||||
if init_value2:
|
||||
self.internals[1].put("key", init_value2)
|
||||
|
||||
def alt_get(self, cache_name, key):
|
||||
for cache in self.internals:
|
||||
value = cache.alt_get(key)
|
||||
if value is not NotFound:
|
||||
return value
|
||||
|
||||
return NotFound
|
||||
|
||||
|
||||
class TestIncCache(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_put_and_retrieve_values_from_inc_cache(self):
|
||||
cache = IncCache()
|
||||
|
||||
assert cache.get("key") == 1
|
||||
assert cache.get("key") == 2
|
||||
assert cache.get("key") == 3
|
||||
assert cache.get("key2") == 1
|
||||
assert cache.get("key2") == 2
|
||||
|
||||
cache.put("key", 100)
|
||||
assert cache.get("key") == 101
|
||||
|
||||
assert cache.copy() == {'key': 101, 'key2': 2}
|
||||
|
||||
def test_i_can_alt_get(self):
|
||||
cache = IncCache()
|
||||
|
||||
assert cache.get("key") == 1
|
||||
assert cache.get("key") == 2
|
||||
assert cache.alt_get("key") == 2
|
||||
assert cache.alt_get("key") == 2
|
||||
assert cache.get("key") == 3
|
||||
|
||||
def test_current_cache_takes_precedence_over_alt_sdp(self):
|
||||
cache = IncCache(sdp=FakeSdp(get_value=lambda cache_name, key: NotFound)).auto_configure("cache_name")
|
||||
|
||||
assert cache.get("key") == 1
|
||||
|
||||
alt_sdp = FakeSdp(get_alt_value=lambda cache_name, key: 10)
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 2
|
||||
|
||||
def test_remote_repository_takes_precedence_over_alt_sdp(self):
|
||||
cache = IncCache(sdp=FakeSdp(get_value=lambda cache_name, key: 5)).auto_configure("cache_name")
|
||||
|
||||
alt_sdp = FakeSdp(get_alt_value=lambda cache_name, key: 10)
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 6
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 7 # then we use the value from the cache
|
||||
|
||||
def test_i_can_take_value_from_alt_sdp(self):
|
||||
cache = IncCache(sdp=FakeSdp(get_value=lambda cache_name, key: NotFound)).auto_configure("cache_name")
|
||||
|
||||
alt_sdp = FakeIncSdp(10, NotFound)
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 11
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 12 # then we use the value from the cache
|
||||
|
||||
def test_i_can_get_when_alt_sdp_and_cache_is_cleared(self):
|
||||
cache = IncCache(sdp=FakeSdp(get_value=lambda cache_name, key: NotFound)).auto_configure("cache_name")
|
||||
cache.clear()
|
||||
|
||||
alt_sdp = FakeIncSdp(10, NotFound)
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 1
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 2 # then we use the value from the cache
|
||||
|
||||
def test_i_can_manage_when_the_value_from_alt_sdp_is_removed(self):
|
||||
cache = IncCache(sdp=FakeSdp(get_value=lambda cache_name, key: NotFound)).auto_configure("cache_name")
|
||||
|
||||
alt_sdp = FakeIncSdp(Removed, 10)
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 1
|
||||
assert cache.get("key", alt_sdp=alt_sdp) == 2 # then we use the value from the cache
|
||||
Reference in New Issue
Block a user