| 1 |
import logging |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from errbot.backends.test import TestPerson |
| 6 |
from errbot.flow import Flow, FlowRoot, InvalidState |
| 7 |
|
| 8 |
log = logging.getLogger(__name__) |
| 9 |
|
| 10 |
|
| 11 |
def test_node(): |
| 12 |
root = FlowRoot("test", "This is my flowroot") |
| 13 |
node = root.connect("a", lambda ctx: ctx["toto"] == "titui") |
| 14 |
|
| 15 |
assert root.predicate_for_node(node)({"toto": "titui"}) |
| 16 |
assert not root.predicate_for_node(node)({"toto": "blah"}) |
| 17 |
|
| 18 |
|
| 19 |
def test_flow_predicate(): |
| 20 |
root = FlowRoot("test", "This is my flowroot") |
| 21 |
node = root.connect("a", lambda ctx: "toto" in ctx and ctx["toto"] == "titui") |
| 22 |
somebody = TestPerson("me") |
| 23 |
|
| 24 |
# Non-matching predicate |
| 25 |
flow = Flow(root, somebody, {}) |
| 26 |
assert node in flow.next_steps() |
| 27 |
assert node not in flow.next_autosteps() |
| 28 |
with pytest.raises(InvalidState): |
| 29 |
flow.advance(node) |
| 30 |
|
| 31 |
flow.advance(node, enforce_predicate=False) # This will bypass the restriction |
| 32 |
assert flow._current_step == node |
| 33 |
|
| 34 |
# Matching predicate |
| 35 |
flow = Flow(root, somebody, {"toto": "titui"}) |
| 36 |
assert node in flow.next_steps() |
| 37 |
assert node in flow.next_autosteps() |
| 38 |
flow.advance(node) |
| 39 |
assert flow._current_step == node |
| 40 |
|
| 41 |
|
| 42 |
def test_autotrigger(): |
| 43 |
root = FlowRoot("test", "This is my flowroot") |
| 44 |
node = root.connect( |
| 45 |
"a", lambda ctx: "toto" in ctx and ctx["toto"] == "titui", auto_trigger=True |
| 46 |
) |
| 47 |
assert node.command in root.auto_triggers |