Skip to content

Commit c765ea0

Browse files
authored
Merge pull request #591
Add unit tests for runtime exception handling in flows
2 parents 7a09f9c + 5fa30ee commit c765ea0

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

nemoguardrails/colang/v2_x/runtime/statemachine.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,9 @@ def _advance_head_front(state: State, heads: List[FlowHead]) -> List[FlowHead]:
951951

952952
if flow_finished:
953953
_finish_flow(state, flow_state, head.matching_scores)
954-
flow_finished = True
955954
log.debug("Flow finished: %s with last element", head.flow_state_uid)
956955
elif flow_aborted:
957956
_abort_flow(state, flow_state, head.matching_scores)
958-
flow_aborted = True
959957
log.debug("Flow aborted: %s by 'abort' statement", head.flow_state_uid)
960958

961959
# Make sure that all actionable heads still exist in flows, otherwise remove them

tests/v2_x/test_event_mechanics.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,5 +1452,86 @@ def test_event_number_parameter_comparison():
14521452
)
14531453

14541454

1455+
def test_runtime_exception_handling_1():
1456+
"""Test to match events based on numeric parameters."""
1457+
1458+
content = """
1459+
flow a
1460+
$var = "test" + 3
1461+
match WaitEvent()
1462+
1463+
flow main
1464+
match StartEvent()
1465+
when a
1466+
pass
1467+
else
1468+
start UtteranceBotAction(script="success")
1469+
match Event()
1470+
"""
1471+
state = run_to_completion(_init_state(content), start_main_flow_event)
1472+
assert is_data_in_events(
1473+
state.outgoing_events,
1474+
[],
1475+
)
1476+
state = run_to_completion(
1477+
state,
1478+
{
1479+
"type": "StartEvent",
1480+
},
1481+
)
1482+
assert is_data_in_events(
1483+
state.outgoing_events,
1484+
[
1485+
{
1486+
"type": "StartUtteranceBotAction",
1487+
"script": "success",
1488+
},
1489+
],
1490+
)
1491+
1492+
1493+
def test_runtime_exception_handling_2():
1494+
"""Test to match events based on numeric parameters."""
1495+
1496+
content = """
1497+
flow a
1498+
start UtteranceBotAction(script="test")
1499+
match StartEvent()
1500+
$var = "test" + 3
1501+
1502+
flow main
1503+
activate a
1504+
match Event()
1505+
"""
1506+
state = run_to_completion(_init_state(content), start_main_flow_event)
1507+
assert is_data_in_events(
1508+
state.outgoing_events,
1509+
[
1510+
{
1511+
"type": "StartUtteranceBotAction",
1512+
"script": "test",
1513+
},
1514+
],
1515+
)
1516+
state = run_to_completion(
1517+
state,
1518+
{
1519+
"type": "StartEvent",
1520+
},
1521+
)
1522+
assert is_data_in_events(
1523+
state.outgoing_events,
1524+
[
1525+
{
1526+
"type": "StopUtteranceBotAction",
1527+
},
1528+
{
1529+
"type": "StartUtteranceBotAction",
1530+
"script": "test",
1531+
},
1532+
],
1533+
)
1534+
1535+
14551536
if __name__ == "__main__":
1456-
test_custom_regex_event_parameter_match()
1537+
test_runtime_exception_handling_2()

0 commit comments

Comments
 (0)