Implemented ConceptManager with concept creation, modification and deletion

This commit is contained in:
2020-12-08 15:36:21 +01:00
parent d364878ddb
commit 4b6e1dd55b
40 changed files with 1847 additions and 979 deletions
+113 -1
View File
@@ -539,9 +539,121 @@ class TestCache(TestUsingMemoryBasedSheerka):
assert cache.get("key") == "value"
cache.delete("key")
assert cache.get("value") is None
assert cache.get("key") is None
assert cache.to_remove == {"key"}
def test_i_can_delete_values_from_set_cache(self):
cache = SetCache()
cache.put("key", "value1")
cache.put("key", "value2")
cache.reset_events()
cache.delete("key", "fake_value")
assert cache.get("key") == {"value1", "value2"}
assert len(cache) == 2
assert cache.to_add == set()
assert cache.to_remove == set()
cache.delete("key", "value1")
assert cache.get("key") == {"value2"}
assert cache.to_add == {"key"}
assert len(cache) == 1
cache.delete("key", "value2")
assert cache.get("key") is None
assert cache.to_remove == {"key"}
assert len(cache) == 0
def test_i_can_delete_key_from_set_cache(self):
cache = SetCache()
cache.put("key", "value1")
cache.put("key", "value2")
cache.delete("key")
assert cache.get("key") is None
assert cache.to_remove == {"key"}
assert len(cache) == 0
def test_i_can_delete_a_key_that_does_not_exists(self):
cache = SetCache()
cache.delete("key")
assert cache.to_add == set()
assert cache.to_remove == set()
def test_i_can_delete_from_a_key_from_list_id_needed(self):
cache = ListIfNeededCache()
cache.put("key", "value1")
cache.put("key", "value11")
cache.put("key2", "value2")
cache.put("key2", "value22")
cache.put("key2", "value222")
cache.put("key3", "value3")
cache.put("key3", "value33")
cache.put("key4", "value4")
cache.reset_events()
assert len(cache) == 8
# I can remove a whole key
cache.delete("key")
assert cache.get("key") is None
assert len(cache) == 6
assert cache.to_remove == {"key"}
assert cache.to_add == set()
# I can remove an element while a list is remaining
cache.reset_events()
cache.delete("key2", "value22")
assert cache.get("key2") == ["value2", "value222"]
assert len(cache) == 5
assert cache.to_add == {"key2"}
assert cache.to_remove == set()
# I can remove an element while a single element is remaining
cache.reset_events()
cache.delete("key3", "value33")
assert cache.get("key3") == "value3"
assert len(cache) == 4
assert cache.to_add == {"key3"}
assert cache.to_remove == set()
# I can remove an element while nothing remains
cache.reset_events()
cache.delete("key4", "value4")
assert cache.get("key4") is None
assert len(cache) == 3
assert cache.to_remove == {"key4"}
assert cache.to_add == set()
# I do not remove when the value is not the same
cache.reset_events()
cache.delete("key3", "value33") # value33 was already remove
assert cache.get("key3") == "value3"
assert len(cache) == 3
assert cache.to_add == set()
assert cache.to_remove == set()
def test_deleting_a_list_if_need_entry_that_does_not_exist_is_not_an_error(self):
cache = ListIfNeededCache()
cache.put("key", "value1")
cache.reset_events()
cache.delete("key3")
assert len(cache) == 1
assert cache.to_add == set()
assert cache.to_remove == set()
cache.delete("key3", "value")
assert len(cache) == 1
assert cache.to_add == set()
assert cache.to_remove == set()
cache.delete("key", "value2")
assert len(cache) == 1
assert cache.to_add == set()
assert cache.to_remove == set()
def test_initialized_key_is_removed_when_the_entry_is_found(self):
caches = [Cache(), ListCache(), ListIfNeededCache(), SetCache()]
+47 -1
View File
@@ -1,7 +1,10 @@
import pytest
from cache.Cache import Cache
from cache.CacheManager import CacheManager
from cache.CacheManager import CacheManager, ConceptNotFound
from cache.DictionaryCache import DictionaryCache
from cache.ListCache import ListCache
from cache.ListIfNeededCache import ListIfNeededCache
from core.concept import Concept
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -109,3 +112,46 @@ class TestCacheManager(TestUsingMemoryBasedSheerka):
cache.put(False, {"key": "value", "key2": "value2", "key3": "value3"})
cache_manager.commit(context)
assert sheerka.sdp.get("test") == {"key": "value", "key2": "value2", "key3": "value3"}
def test_i_can_remove_a_concept_from_concepts_caches(self):
cache_manager = CacheManager(True)
cache_manager.register_concept_cache("id", Cache(), lambda c: c.id, True)
cache_manager.register_concept_cache("key", ListIfNeededCache(), lambda c: c.key, True)
sheerka, context, one, two, three, two_bis = self.init_concepts("one", "two", "three", Concept("two", body="2"))
for concept in [one, two, three, two_bis]:
cache_manager.add_concept(concept)
# sanity check
cache_def = cache_manager.caches["id"]
assert cache_def.cache.copy() == {one.id: one, two.id: two, three.id: three, two_bis.id: two_bis}
cache_def = cache_manager.caches["key"]
assert cache_def.cache.copy() == {one.key: one, two.key: [two, two_bis], three.key: three}
for cache_name in cache_manager.concept_caches:
cache_manager.caches[cache_name].cache.reset_events()
cache_manager.remove_concept(sheerka.new(("two", two_bis.id)))
cache_def = cache_manager.caches["id"]
assert cache_def.cache.copy() == {one.id: one, two.id: two, three.id: three}
assert cache_def.cache.to_remove == {two_bis.id}
assert cache_def.cache.to_add == set()
assert len(cache_def.cache) == 3
cache_def = cache_manager.caches["key"]
assert cache_def.cache.copy() == {one.key: one, two.key: two, three.key: three}
assert cache_def.cache.to_remove == set()
assert cache_def.cache.to_add == {"two"}
assert len(cache_def.cache) == 3
def test_i_cannot_remove_a_concept_that_does_not_exists(self):
cache_manager = CacheManager(True)
cache_manager.register_concept_cache("id", Cache(), lambda c: c.id, True)
cache_manager.register_concept_cache("key", ListIfNeededCache(), lambda c: c.key, True)
with pytest.raises(ConceptNotFound) as ex:
cache_manager.remove_concept(Concept("foo", id="1001"))
assert ex.value.concept == Concept("foo", id="1001")