141 lines
5.3 KiB
Python
141 lines
5.3 KiB
Python
import pytest
|
|
|
|
from core.builtin_concepts import BuiltinConcepts
|
|
from core.concept import Concept
|
|
from core.sheerka.ExecutionContext import ExecutionContext
|
|
from sdp.sheerkaDataProvider import Event
|
|
|
|
|
|
def test_id_is_incremented_by_event_digest():
|
|
a = ExecutionContext("foo", Event("event_1"), None, BuiltinConcepts.NOP, None)
|
|
b = ExecutionContext("foo", Event("event_1"), None, BuiltinConcepts.NOP, None)
|
|
c = ExecutionContext("foo", Event("event_2"), None, BuiltinConcepts.NOP, None)
|
|
d = b.push(BuiltinConcepts.NOP, None)
|
|
e = c.push(BuiltinConcepts.NOP, None)
|
|
|
|
assert a.id == 0
|
|
assert b.id == 1
|
|
assert c.id == 0
|
|
assert d.id == 2
|
|
assert e.id == 1
|
|
|
|
|
|
def test_i_can_use_with_statement():
|
|
with ExecutionContext("who_", Event("event"), "fake_sheerka", BuiltinConcepts.NOP, None) as e:
|
|
pass
|
|
assert e.elapsed > 0
|
|
|
|
|
|
def test_i_can_push():
|
|
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None,
|
|
desc="some description",
|
|
obj=Concept("foo"),
|
|
step=BuiltinConcepts.EVALUATION,
|
|
iteration=15,
|
|
concepts={"bar": Concept("bar")})
|
|
a.preprocess = set()
|
|
a.preprocess.add("preprocess")
|
|
a.local_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
|
|
a.global_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
|
|
|
|
b = a.push(BuiltinConcepts.EVALUATION, "sub action context", desc="sub description")
|
|
|
|
assert b._parent == a
|
|
assert b.who == a.who
|
|
assert b.event == a.event
|
|
assert b.sheerka == a.sheerka
|
|
assert b.action == BuiltinConcepts.EVALUATION
|
|
assert b.action_context == "sub action context"
|
|
assert b.desc == "sub description"
|
|
assert b.obj == a.obj
|
|
assert b.step == a.step
|
|
assert b.iteration == a.iteration
|
|
assert b.concepts == a.concepts
|
|
assert b.id == a.id + 1
|
|
assert b._tab == a._tab + " "
|
|
assert b.preprocess == a.preprocess
|
|
assert b.local_hints == {BuiltinConcepts.EVAL_BODY_REQUESTED}
|
|
assert b.global_hints == a.global_hints
|
|
|
|
|
|
def test_children_i_created_when_i_push():
|
|
e = ExecutionContext("who_", Event("event"), "fake_sheerka", BuiltinConcepts.NOP, None)
|
|
e.push(BuiltinConcepts.NOP, None, who="a", desc="I do something")
|
|
e.push(BuiltinConcepts.NOP, None, who="b", desc="oups! I did a again")
|
|
e.push(BuiltinConcepts.NOP, None, who="c", desc="I do something else")
|
|
|
|
assert len(e.children) == 3
|
|
assert e.children[0].who, e.children[0].who == ("a", "I do something")
|
|
assert e.children[1].who, e.children[1].who == ("b", "oups! I did a again")
|
|
assert e.children[2].who, e.children[2].who == ("c", "I do something else")
|
|
|
|
|
|
def test_i_can_add_variable_when_i_push():
|
|
e = ExecutionContext("who_", Event("event"), "fake_sheerka", BuiltinConcepts.NOP, None)
|
|
sub_e = e.push(BuiltinConcepts.NOP, None, who="a", my_new_var="new var value")
|
|
|
|
assert sub_e.my_new_var == "new var value"
|
|
with pytest.raises(AttributeError):
|
|
assert e.my_new_var == "" # my_new_var does not exist in parent
|
|
|
|
|
|
def test_local_hints_are_local_and_global_hints_are_global():
|
|
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None)
|
|
a.local_hints.add("local hint 1")
|
|
a.global_hints.add("global hint 1")
|
|
|
|
b = a.push(BuiltinConcepts.NOP, None)
|
|
b.local_hints.add("local hint 2")
|
|
b.global_hints.add("global hint 2")
|
|
|
|
assert a.local_hints == {"local hint 1"}
|
|
assert a.global_hints == {"global hint 1", "global hint 2"}
|
|
|
|
assert b.local_hints == {"local hint 1", "local hint 2"}
|
|
assert b.global_hints == {"global hint 1", "global hint 2"}
|
|
|
|
|
|
def test_global_hits_are_global_even_when_empty():
|
|
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None)
|
|
|
|
b = a.push(BuiltinConcepts.NOP, None)
|
|
b.global_hints.add("global hint 2")
|
|
|
|
assert a.global_hints == {"global hint 2"}
|
|
assert b.global_hints == {"global hint 2"}
|
|
|
|
|
|
def test_i_can_search():
|
|
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.TESTING, "a")
|
|
ab = a.push(BuiltinConcepts.TESTING, "ab", obj="obj_ab")
|
|
ac = a.push(BuiltinConcepts.TESTING, "ac", obj="obj_ac")
|
|
abb = ab.push(BuiltinConcepts.TESTING, "abb", obj="skip")
|
|
abbb = abb.push(BuiltinConcepts.TESTING, "abbb", obj="obj_abbb")
|
|
|
|
assert list(abbb.search()) == [abb, ab, a]
|
|
assert list(abbb.search(start_with_self=True)) == [abbb, abb, ab, a]
|
|
assert list(abbb.search(lambda ec: ec.obj != "skip")) == [ab, a]
|
|
assert list(abbb.search(lambda ec: ec.obj != "skip", lambda ec: ec.action_context)) == ["ab", "a"]
|
|
assert list(abbb.search(stop=lambda ec: ec.obj == "skip")) == [abb]
|
|
assert list(abbb.search(
|
|
stop=lambda ec: ec.obj == "skip",
|
|
start_with_self=True,
|
|
get_obj=lambda ec: ec.obj)) == ["obj_abbb", "skip"]
|
|
|
|
|
|
def test_variables_are_passed_to_children_but_not_to_parents():
|
|
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None)
|
|
assert not hasattr(a, "var")
|
|
|
|
b = a.push(BuiltinConcepts.NOP, None, var="foo")
|
|
assert b.var == "foo"
|
|
assert not hasattr(a, "var")
|
|
|
|
c = b.push(BuiltinConcepts.NOP, None)
|
|
assert c.var == "foo"
|
|
|
|
c.var = "bar"
|
|
assert c.var == "bar"
|
|
assert b.var != "bar"
|
|
assert not hasattr(a, "var")
|