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) b = ExecutionContext("foo", Event("event_1"), None) c = ExecutionContext("foo", Event("event_2"), None) d = b.push() e = c.push() 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") as e: pass assert e.elapsed > 0 def test_i_can_push(): a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", "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() assert b._parent == a assert b.who == a.who assert b.event == a.event assert b.sheerka == a.sheerka assert b.desc is None 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") e.push("a", desc="I do something") e.push("b", desc="oups! I did a again") e.push("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") sub_e = e.push("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") a.local_hints.add("local hint 1") a.global_hints.add("global hint 1") b = a.push() 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") b = a.push() b.global_hints.add("global hint 2") assert a.global_hints == {"global hint 2"} assert b.global_hints == {"global hint 2"}