53 lines
2.3 KiB
Python
53 lines
2.3 KiB
Python
from core.builtin_concepts import BuiltinConcepts
|
|
from core.concept import Concept
|
|
from core.sheerka.ExecutionContext import ExecutionContext
|
|
from core.sheerka.services.SheerkaShortTermMemory import SheerkaShortTermMemory
|
|
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
class TestSheerkaShortTermMemory(TestUsingMemoryBasedSheerka):
|
|
def test_i_can_add_to_global_short_term_memory(self):
|
|
sheerka = self.get_sheerka()
|
|
service = sheerka.services[SheerkaShortTermMemory.NAME]
|
|
|
|
foo = Concept("foo")
|
|
sheerka.add_to_short_term_memory(None, "a", foo)
|
|
|
|
assert service.objects.copy() == {":a": foo}
|
|
assert id(sheerka.get_from_short_term_memory(None, "a")) == id(foo)
|
|
|
|
def test_i_can_add_context_short_term_memory(self):
|
|
sheerka, context = self.init_concepts()
|
|
service = sheerka.services[SheerkaShortTermMemory.NAME]
|
|
|
|
foo = Concept("foo")
|
|
sheerka.add_to_short_term_memory(context, "a", foo)
|
|
|
|
context_id = ExecutionContext.ids[context.event.get_digest()]
|
|
assert service.objects.copy() == {f"{context_id}:a": foo}
|
|
assert id(sheerka.get_from_short_term_memory(context, "a")) == id(foo)
|
|
assert sheerka.get_from_short_term_memory(None, "a") is None
|
|
|
|
def test_i_can_get_obj_from_parents(self):
|
|
sheerka, context = self.init_concepts()
|
|
service = sheerka.services[SheerkaShortTermMemory.NAME]
|
|
foo = Concept("foo")
|
|
sheerka.add_to_short_term_memory(None, "a", foo)
|
|
|
|
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
|
|
assert service.objects.copy() == {":a": foo}
|
|
assert id(sheerka.get_from_short_term_memory(sub_context, "a")) == id(foo)
|
|
assert id(sheerka.get_from_short_term_memory(context, "a")) == id(foo)
|
|
assert id(sheerka.get_from_short_term_memory(None, "a")) == id(foo)
|
|
|
|
def test_entry_are_removed_on_context_exit(self):
|
|
sheerka, context = self.init_concepts()
|
|
|
|
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
|
|
foo = Concept("foo")
|
|
sheerka.add_to_short_term_memory(sub_context, "a", foo)
|
|
assert id(sheerka.get_from_short_term_memory(sub_context, "a")) == id(foo)
|
|
|
|
assert sheerka.get_from_short_term_memory(sub_context, "a") is None
|