Fixed #12
Fixed #13
Fixed #14
This commit is contained in:
2023-05-08 17:50:28 +02:00
parent 21a397861a
commit e41094f908
95 changed files with 12168 additions and 260 deletions
View File
+103
View File
@@ -0,0 +1,103 @@
from common.global_symbols import NotFound, NotInit
from core.concept import ConceptDefaultProps
from helpers import GetNextId, get_concept
def test_i_can_retrieve_concept_properties():
foo = get_concept("a plus b", "a + b", variables=("a", "b"), id="1001")
assert foo.name == "a plus b"
assert foo.id == "1001"
assert foo.str_id == "c:#1001:"
assert foo.all_attrs() == ('#where#', '#pre#', '#post#', '#body#', '#ret#', 'a', 'b')
assert foo.get_definition_digest() == "3a2cfcda8ffd0d99a7f8c7d2f1ffc4a99fc96162f3be7b9875f30751d3691af6"
# sanity check to make sure that 'get_concept' works as expected
assert foo.get_metadata().variables == (("a", NotInit), ("b", NotInit))
def test_i_can_set_and_get_value():
foo = get_concept("foo", variables=["a"])
foo.set_value("a", "some value")
assert foo.get_value("a") == "some value"
def test_i_can_set_and_get_value_from_bound_attr():
foo = get_concept("foo", variables=["a"], bound_body="a")
foo.set_value("a", "some value")
assert foo.get_value(ConceptDefaultProps.BODY) == "some value"
foo.set_value(ConceptDefaultProps.BODY, "another value")
assert foo.get_value("a") == "another value"
def test_i_can_test_concept_equality():
foo1 = get_concept("foo", "a + b", variables=["a", "b"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a", "b"], id=6)
foo1.set_value("a", 10).set_value("b", 20)
foo2.set_value("a", 10).set_value("b", 20)
assert foo1 == foo2
def test_i_can_detect_when_concepts_are_not_equal():
foo1 = get_concept("foo", "a + b", variables=["a", "b"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a", "b"], id=6)
foo1.set_value("a", 10).set_value("b", 20)
foo2.set_value("a", 10).set_value("b", 25)
assert foo1 != foo2
def test_i_can_test_concept_equality_in_case_of_infinite_recursion():
foo1 = get_concept("foo", "a + b", variables=["a"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a"], id=6)
# foo1 and foo2 are equals
assert foo1 == foo2
foo1.set_value("a", foo1)
foo2.set_value("a", foo2)
assert foo1 == foo2
foo1.set_value("a", foo2)
foo2.set_value("a", foo1)
assert foo1 == foo2
def test_i_can_test_concept_equality_in_case_of_infinite_recursion_with_more_than_two_concepts():
foo1 = get_concept("foo", "a + b", variables=["a"], id=5)
foo2 = get_concept("foo", "a + b", variables=["a"], id=6)
foo3 = get_concept("foo", "a + b", variables=["a"], id=7)
foo1.set_value("a", foo2)
foo2.set_value("a", foo3)
foo3.set_value("a", foo1)
assert foo1 == foo2
foo1.set_value("a", foo2)
foo2.set_value("a", foo3)
foo3.set_value("a", foo3)
assert foo1 == foo2
def test_i_cannot_get_an_attribute_which_is_not_defined():
next_id = GetNextId()
foo = get_concept("add a b", definition="add", variables=["a", "b"], sequence=next_id)
assert foo.get_value("a") is NotInit
assert foo.get_value("b") is NotInit
assert foo.get_value("c") is NotFound
def test_i_can_repr_a_concept():
next_id = GetNextId()
foo = get_concept("foo", sequence=next_id)
assert repr(foo) == "(1001)foo"
bar = get_concept("bar", pre="is an int", sequence=next_id)
assert repr(bar) == "(1002)bar, #pre=is an int"
baz = get_concept("baz", definition="add a b", variables=["a", "b"], sequence=next_id)
assert repr(baz) == "(1003)baz, a=**NotInit**, b=**NotInit**"
+151
View File
@@ -0,0 +1,151 @@
from core.Event import Event
from core.ExecutionContext import ExecutionContext, ExecutionContextActions
def test_i_can_create_execution_context(sheerka):
event = Event("myEvent", "fake_userid")
context1 = ExecutionContext("who", event, sheerka, ExecutionContextActions.TESTING, "value1", "my desc")
assert context1.who == "who"
assert context1.event == event
assert context1.sheerka == sheerka
assert context1.action == ExecutionContextActions.TESTING
assert context1.action_context == "value1"
assert context1.desc == "my desc"
assert context1.id == 0
assert context1.long_id == f"{event.get_digest()}:{context1.id}"
def test_i_can_push(sheerka):
event = Event("test")
context = ExecutionContext("who", event, sheerka, ExecutionContextActions.TESTING, "value")
with context.push("pusher", ExecutionContextActions.PARSING, "action_context", "my desc") as sub_context:
assert sub_context.who == "pusher"
assert sub_context.event == event
assert sub_context.sheerka == sheerka
assert sub_context.action == ExecutionContextActions.PARSING
assert sub_context.action_context == "action_context"
assert sub_context.desc == "my desc"
assert sub_context.id == context.id + 1
def test_i_can_increment_ids(sheerka):
# The id of an execution context is linked to the event
# If the event is the same, the id is incremented
event = Event("TEST::myEvent", "fake_userid")
context1 = ExecutionContext("who", event, sheerka, ExecutionContextActions.TESTING, "value")
context2 = context1.push("who1", ExecutionContextActions.TESTING, "value1")
context3 = context2.push("who2", ExecutionContextActions.TESTING, "value2")
context4 = context1.push("who1", ExecutionContextActions.TESTING, "value3")
context5 = ExecutionContext("who", event, sheerka, ExecutionContextActions.TESTING, "value4")
assert context1.id == 0
assert context2.id == 1
assert context3.id == 2
assert context4.id == 3
assert context5.id == 4
event2 = Event("TEST::myEvent2", "fake_userid")
context6 = ExecutionContext("who", event2, sheerka, ExecutionContextActions.TESTING, "value")
assert context6.id == 0
def test_i_can_manage_global_hints(context):
context2 = context.push("pusher", ExecutionContextActions.TESTING, None)
context3 = context2.push("pusher", ExecutionContextActions.TESTING, None)
context4 = context3.push("pusher", ExecutionContextActions.TESTING, None)
context5 = context.push("pusher", ExecutionContextActions.TESTING, None)
context.global_hints.add("new_hint")
assert context.global_hints == {"new_hint"}
assert context2.global_hints == {"new_hint"}
assert context3.global_hints == {"new_hint"}
assert context4.global_hints == {"new_hint"}
assert context5.global_hints == {"new_hint"}
context4.global_hints.add("another_hint")
assert context.global_hints == {"new_hint", "another_hint"}
assert context2.global_hints == {"new_hint", "another_hint"}
assert context3.global_hints == {"new_hint", "another_hint"}
assert context4.global_hints == {"new_hint", "another_hint"}
assert context5.global_hints == {"new_hint", "another_hint"}
def test_i_can_manage_protected_hint(context):
# Note that protected hint only works if the hint is added BEFORE the creation of the child
context.protected_hints.add("new_hint")
context2 = context.push("pusher", ExecutionContextActions.TESTING, None)
context3 = context2.push("pusher", ExecutionContextActions.TESTING, None)
context3.protected_hints.add("another_hint")
context4 = context3.push("pusher", ExecutionContextActions.TESTING, None)
context5 = context.push("pusher", ExecutionContextActions.TESTING, None)
assert context.protected_hints == {"new_hint"}
assert context2.protected_hints == {"new_hint"}
assert context3.protected_hints == {"new_hint", "another_hint"}
assert context4.protected_hints == {"new_hint", "another_hint"}
assert context5.protected_hints == {"new_hint"}
def test_i_can_manage_private_hints(context):
context.private_hints.add("new_hint")
context2 = context.push("pusher", ExecutionContextActions.TESTING, None)
context3 = context2.push("pusher", ExecutionContextActions.TESTING, None)
context3.private_hints.add("another_hint")
context4 = context3.push("pusher", ExecutionContextActions.TESTING, None)
context5 = context.push("pusher", ExecutionContextActions.TESTING, None)
assert context.private_hints == {"new_hint"}
assert context2.private_hints == set()
assert context3.private_hints == {"another_hint"}
assert context4.private_hints == set()
assert context5.private_hints == set()
def test_i_can_keep_track_of_children(context):
context2 = context.push("pusher", ExecutionContextActions.TESTING, None)
context3 = context.push("pusher", ExecutionContextActions.TESTING, None)
context4 = context2.push("pusher2", ExecutionContextActions.TESTING, None)
assert len(context._children) == 2
assert len(context2._children) == 1
assert len(context3._children) == 0
assert len(context4._children) == 0
def test_i_can_get_children(context):
context1 = context.push("child 1", ExecutionContextActions.TESTING, None)
context2 = context.push("child 2", ExecutionContextActions.TESTING, None)
context3 = context.push("child 3", ExecutionContextActions.TESTING, None)
context21 = context2.push("child 21", ExecutionContextActions.TESTING, None)
context22 = context2.push("child 22", ExecutionContextActions.TESTING, None)
context211 = context21.push("child 211", ExecutionContextActions.TESTING, None)
context31 = context3.push("child 31", ExecutionContextActions.TESTING, None)
assert list(context1.get_children()) == []
assert list(context.get_children()) == [
context1,
context2,
context21,
context211,
context22,
context3,
context31
]
assert list(context.get_children(level=1)) == [
context1,
context2,
context3
]
assert list(context.get_children(level=2)) == [
context1,
context2,
context21,
context22,
context3,
context31,
]
+36
View File
@@ -0,0 +1,36 @@
from os import path
from base import UsingFileBasedSheerka
from helpers import get_concept, get_concepts, get_file_content
class TestSheerka(UsingFileBasedSheerka):
def test_i_can_initialize_sheerka(self, sheerka_fb):
sheerka = sheerka_fb
assert path.exists(self.SHEERKA_ROOT_DIR)
last_event_path = path.join(self.SHEERKA_ROOT_DIR, "LAST_EVENT")
assert path.exists(last_event_path)
last_event_digest = get_file_content(last_event_path)
last_event_folder = path.join(self.SHEERKA_ROOT_DIR, "events", last_event_digest[:24], last_event_digest)
assert path.exists(last_event_folder)
assert path.exists(last_event_folder + "_admin_context")
assert len(sheerka.services) > 0
assert len(sheerka.evaluators) > 0
# add test to validate that we can access bind methods
def test_i_can_use_isinstance(self, sheerka, context):
foo, bar = get_concepts(context, "foo", "bar", use_sheerka=True)
assert sheerka.isinstance(foo, foo.key)
assert sheerka.isinstance(foo, foo.str_id)
assert sheerka.isinstance(foo, foo)
assert sheerka.isinstance(foo, foo.get_metadata())
assert not sheerka.isinstance(foo, bar.key)
assert not sheerka.isinstance(foo, bar.str_id)
assert not sheerka.isinstance(foo, bar)
assert not sheerka.isinstance(foo, bar.get_metadata())