178 lines
7.0 KiB
Python
178 lines
7.0 KiB
Python
from core.Event import Event
|
|
from core.ExecutionContext import ExecutionContext, ContextActions
|
|
|
|
|
|
def test_i_can_create_execution_context(sheerka):
|
|
event = Event("myEvent", "fake_userid")
|
|
context1 = ExecutionContext("who", event, sheerka, ContextActions.TESTING, "value1", "my desc")
|
|
|
|
assert context1.who == "who"
|
|
assert context1.event == event
|
|
assert context1.sheerka == sheerka
|
|
assert context1.action == ContextActions.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, ContextActions.TESTING, "value")
|
|
with context.push("pusher", ContextActions.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 == ContextActions.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, ContextActions.TESTING, "value")
|
|
context2 = context1.push("who1", ContextActions.TESTING, "value1")
|
|
context3 = context2.push("who2", ContextActions.TESTING, "value2")
|
|
context4 = context1.push("who1", ContextActions.TESTING, "value3")
|
|
context5 = ExecutionContext("who", event, sheerka, ContextActions.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, ContextActions.TESTING, "value")
|
|
assert context6.id == 0
|
|
|
|
|
|
def test_i_can_manage_global_hints(context):
|
|
context2 = context.push("pusher", ContextActions.TESTING, None)
|
|
context3 = context2.push("pusher", ContextActions.TESTING, None)
|
|
context4 = context3.push("pusher", ContextActions.TESTING, None)
|
|
context5 = context.push("pusher", ContextActions.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", ContextActions.TESTING, None)
|
|
context3 = context2.push("pusher", ContextActions.TESTING, None)
|
|
context3.protected_hints.add("another_hint")
|
|
context4 = context3.push("pusher", ContextActions.TESTING, None)
|
|
context5 = context.push("pusher", ContextActions.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", ContextActions.TESTING, None)
|
|
context3 = context2.push("pusher", ContextActions.TESTING, None)
|
|
context3.private_hints.add("another_hint")
|
|
context4 = context3.push("pusher", ContextActions.TESTING, None)
|
|
context5 = context.push("pusher", ContextActions.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_check_if_hints_are_in_context(context):
|
|
context.private_hints.add("private_hint")
|
|
context.protected_hints.add("protected_hint")
|
|
context.global_hints.add("global_hint")
|
|
assert context.in_context("private_hint")
|
|
assert context.in_context("protected_hint")
|
|
assert context.in_context("global_hint")
|
|
|
|
context2 = context.push("pusher", ContextActions.TESTING, None)
|
|
assert not context2.in_context("private_hint")
|
|
assert context2.in_context("protected_hint")
|
|
assert context2.in_context("global_hint")
|
|
|
|
|
|
def test_i_can_keep_track_of_children(context):
|
|
context2 = context.push("pusher", ContextActions.TESTING, None)
|
|
context3 = context.push("pusher", ContextActions.TESTING, None)
|
|
context4 = context2.push("pusher2", ContextActions.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", ContextActions.TESTING, None)
|
|
context2 = context.push("child 2", ContextActions.TESTING, None)
|
|
context3 = context.push("child 3", ContextActions.TESTING, None)
|
|
context21 = context2.push("child 21", ContextActions.TESTING, None)
|
|
context22 = context2.push("child 22", ContextActions.TESTING, None)
|
|
context211 = context21.push("child 211", ContextActions.TESTING, None)
|
|
context31 = context3.push("child 31", ContextActions.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,
|
|
]
|
|
|
|
|
|
def test_i_can_get_parents(context):
|
|
context1 = context.push("child 1", ContextActions.TESTING, None)
|
|
context2 = context1.push("child 2", ContextActions.TESTING, None)
|
|
context3 = context2.push("child 3", ContextActions.TESTING, None)
|
|
|
|
assert list(context3.get_parents()) == [context2, context1, context]
|
|
assert list(context3.get_parents(level=1)) == [context2]
|
|
assert list(context3.get_parents(level=2)) == [context2, context1]
|
|
assert list(context3.get_parents(level=3)) == [context2, context1, context]
|
|
assert list(context3.get_parents(level=4)) == [context2, context1, context]
|