Files
Sheerka-Old/tests/cache/test_DictionaryCache.py
2021-01-11 15:36:03 +01:00

163 lines
5.7 KiB
Python

import pytest
from cache.DictionaryCache import DictionaryCache
from core.global_symbols import NotFound
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
from tests.cache import FakeSdp
class TestDictionaryCache(TestUsingMemoryBasedSheerka):
def test_i_can_put_and_retrieve_value_from_dictionary_cache(self):
cache = DictionaryCache()
# key must be None
with pytest.raises(KeyError):
cache.put("key", None)
# value must be a dictionary
with pytest.raises(ValueError):
cache.put(True, "value")
entry = {"key": "value", "key2": ["value21", "value22"]}
cache.put(False, entry)
assert len(cache) == 3
assert id(cache._cache) == id(entry)
assert cache.get("key") == "value"
assert cache.get("key2") == ["value21", "value22"]
# I can append values
cache.put(True, {"key": "another_value", "key3": "value3"})
assert len(cache) == 4
assert cache.get("key") == "another_value"
assert cache.get("key2") == ["value21", "value22"]
assert cache.get("key3") == "value3"
# I can reset
entry = {"key": "value", "key2": ["value21", "value22"]}
cache.put(False, entry)
assert len(cache) == 3
assert id(cache._cache) == id(entry)
assert cache.get("key") == "value"
assert cache.get("key2") == ["value21", "value22"]
assert cache.copy() == {'key': 'value', 'key2': ['value21', 'value22']}
@pytest.mark.parametrize("key", [
None,
"something"
])
def test_keys_have_constraints_when_dictionary_cache(self, key):
cache = DictionaryCache()
with pytest.raises(KeyError):
cache.put(key, None)
def test_i_can_sync_with_remote_repository(self):
cache = DictionaryCache()
entry = {"key": "value", "key2": ["value21", "value22"]}
cache.put(False, entry)
assert len(cache) == 3
assert id(cache._cache) == id(entry)
assert cache.get("key") == "value"
assert cache.get("key2") == ["value21", "value22"]
def test_i_can_get_a_value_that_does_not_exist_without_compromising_the_cache(self):
cache = DictionaryCache()
cache.put(False, {"key": "value"})
assert cache.get("key2") is NotFound
assert cache.copy() == {"key": "value"}
@pytest.mark.parametrize("value", [
None,
"something"
])
def test_values_have_constraints_when_dictionary_cache(self, value):
cache = DictionaryCache()
with pytest.raises(ValueError):
cache.put(True, value)
def test_i_can_append_to_a_dictionary_cache_even_if_it_is_new(self):
cache = DictionaryCache()
entry = {"key": "value", "key2": ["value21", "value22"]}
cache.put(True, entry)
assert len(cache) == 3
assert id(cache._cache) != id(entry)
assert cache.get("key") == "value"
assert cache.get("key2") == ["value21", "value22"]
def test_exists_in_dictionary_cache(self):
cache = DictionaryCache()
assert not cache.exists("key")
cache.put(True, {"key": "value"})
assert cache.exists("key")
def test_default_for_dictionary_cache(self):
cache = DictionaryCache(default={"key": "value", "key2": "value2"})
# cache is fully set when the value is found
assert cache.get("key") == "value"
assert cache.copy() == {"key": "value", "key2": "value2"}
# cache is fully set when the value is not found
cache.test_only_reset()
assert cache.get("key3") is NotFound
assert cache.copy() == {"key": "value", "key2": "value2"}
# cache is not corrupted when value is found
cache.put(True, {"key3": "value3", "key4": "value4"})
assert cache.get("key3") == "value3"
assert cache.copy() == {"key": "value", "key2": "value2", "key3": "value3", "key4": "value4"}
# cache is not corrupted when value is not found
cache._cache["key"] = "another value" # operation that is normally not possible
assert cache.get("key5") is NotFound
assert cache.copy() == {"key": "value", "key2": "value2", "key3": "value3", "key4": "value4"}
def test_default_callable_for_dictionary_cache(self):
cache = DictionaryCache(default=lambda k: {"key": "value", "key2": "value2"})
assert cache.get("key") == "value"
assert "key2" in cache
assert len(cache) == 2
cache.clear()
assert cache.get("key3") is NotFound
assert len(cache) == 2
assert "key" in cache
assert "key2" in cache
def test_default_callable_with_internal_sdp_for_dictionary_cache(self):
cache = DictionaryCache(default=lambda sdp, key: sdp.get("cache_name", key),
sdp=FakeSdp(lambda entry, k: {"key": "value", "key2": "value2"}))
assert cache.get("key") == "value"
assert "key2" in cache
assert len(cache) == 2
cache.clear()
assert cache.get("key3") is NotFound
assert len(cache) == 2
assert "key" in cache
assert "key2" in cache
def test_dictionary_cache_cannot_be_null(self):
cache = DictionaryCache(default=lambda k: NotFound)
assert cache.get("key") is NotFound
assert cache._cache == {}
cache = DictionaryCache(default=NotFound)
assert cache.get("key") is NotFound
assert cache._cache == {}
cache = DictionaryCache(default=lambda k: None)
assert cache.get("key") is NotFound
assert cache._cache == {}
cache = DictionaryCache(default=None)
assert cache.get("key") is NotFound
assert cache._cache == {}