Skip to content

Commit aa392f9

Browse files
author
Alan Christie
committed
fix: Better logic
1 parent f465398 commit aa392f9

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

tests/api_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ def get_workflow_by_name(self, *, name: str, version: str) -> Dict[str, Any]:
9494
item = {"id": wfid, "workflow": value}
9595
return item
9696

97-
def create_running_workflow(self, *, workflow_definition_id: str) -> str:
97+
def create_running_workflow(self, *, workflow_id: str) -> str:
9898
UnitTestAPIAdapter.mp_lock.acquire()
9999
with open(f"tests/{_RUNNING_WORKFLOW_PICKLE_FILE}", "rb") as pickle_file:
100100
running_workflow = Unpickler(pickle_file).load()
101101

102102
next_id: int = len(running_workflow) + 1
103103
running_workflow_id: str = _RUNNING_WORKFLOW_ID_FORMAT.format(id=next_id)
104-
record = {"done": False, "success": False, "workflow": workflow_definition_id}
104+
record = {"done": False, "success": False, "workflow": workflow_id}
105105
running_workflow[running_workflow_id] = record
106106

107107
with open(f"tests/{_RUNNING_WORKFLOW_PICKLE_FILE}", "wb") as pickle_file:

tests/test_test_api_adapter.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_create_running_workflow():
7474
response = utaa.create_workflow(workflow_definition={"name": "blah"})
7575

7676
# Act
77-
response = utaa.create_running_workflow(workflow_definition_id=response["id"])
77+
response = utaa.create_running_workflow(workflow_id=response["id"])
7878

7979
# Assert
8080
assert response["id"] == "r-workflow-00000000-0000-0000-0000-000000000001"
@@ -85,7 +85,7 @@ def test_get_running_workflow():
8585
utaa = UnitTestAPIAdapter()
8686
response = utaa.create_workflow(workflow_definition={"name": "blah"})
8787
wfid = response["id"]
88-
response = utaa.create_running_workflow(workflow_definition_id=wfid)
88+
response = utaa.create_running_workflow(workflow_id=wfid)
8989
rwfid = response["id"]
9090

9191
# Act
@@ -97,11 +97,43 @@ def test_get_running_workflow():
9797
assert rwf["workflow"] == wfid
9898

9999

100+
def test_set_running_workflow_done_when_success():
101+
# Arrange
102+
utaa = UnitTestAPIAdapter()
103+
response = utaa.create_workflow(workflow_definition={"name": "blah"})
104+
response = utaa.create_running_workflow(workflow_id=response["id"])
105+
rwfid = response["id"]
106+
107+
# Act
108+
utaa.set_running_workflow_done(running_workflow_id=rwfid, success=True)
109+
110+
# Assert
111+
response = utaa.get_running_workflow(running_workflow_id=rwfid)
112+
assert response["running_workflow"]["done"]
113+
assert response["running_workflow"]["success"]
114+
115+
116+
def test_set_running_workflow_done_when_failed():
117+
# Arrange
118+
utaa = UnitTestAPIAdapter()
119+
response = utaa.create_workflow(workflow_definition={"name": "blah"})
120+
response = utaa.create_running_workflow(workflow_id=response["id"])
121+
rwfid = response["id"]
122+
123+
# Act
124+
utaa.set_running_workflow_done(running_workflow_id=rwfid, success=False)
125+
126+
# Assert
127+
response = utaa.get_running_workflow(running_workflow_id=rwfid)
128+
assert response["running_workflow"]["done"]
129+
assert not response["running_workflow"]["success"]
130+
131+
100132
def test_create_running_workflow_step():
101133
# Arrange
102134
utaa = UnitTestAPIAdapter()
103135
response = utaa.create_workflow(workflow_definition={"name": "blah"})
104-
response = utaa.create_running_workflow(workflow_definition_id=response["id"])
136+
response = utaa.create_running_workflow(workflow_id=response["id"])
105137

106138
# Act
107139
response = utaa.create_running_workflow_step(
@@ -117,7 +149,7 @@ def test_get_running_workflow_step():
117149
utaa = UnitTestAPIAdapter()
118150
response = utaa.create_workflow(workflow_definition={"name": "blah"})
119151
wfid = response["id"]
120-
response = utaa.create_running_workflow(workflow_definition_id=wfid)
152+
response = utaa.create_running_workflow(workflow_id=wfid)
121153
rwfid = response["id"]
122154
response = utaa.create_running_workflow_step(
123155
running_workflow_id=rwfid, step="step-1"
@@ -139,7 +171,7 @@ def test_get_running_workflow_steps():
139171
utaa = UnitTestAPIAdapter()
140172
response = utaa.create_workflow(workflow_definition={"name": "blah"})
141173
wfid = response["id"]
142-
response = utaa.create_running_workflow(workflow_definition_id=wfid)
174+
response = utaa.create_running_workflow(workflow_id=wfid)
143175
rwfid = response["id"]
144176
response = utaa.create_running_workflow_step(
145177
running_workflow_id=rwfid, step="step-1"

tests/test_test_instance_launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_get_nop_job(basic_launcher):
2626
utaa = basic_launcher[0]
2727
launcher = basic_launcher[1]
2828
response = utaa.create_workflow(workflow_definition={"name": "blah"})
29-
response = utaa.create_running_workflow(workflow_definition_id=response["id"])
29+
response = utaa.create_running_workflow(workflow_id=response["id"])
3030
response = utaa.create_running_workflow_step(
3131
running_workflow_id=response["id"], step="step-1"
3232
)

tests/test_workflow_engine_examples.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ def basic_engine():
4646

4747

4848
def start_workflow(md, da, workflow_file_name) -> str:
49-
"""A convenience function to handle all the 'START' logic for a workflow."""
49+
"""A convenience function to handle all the 'START' logic for a workflow.
50+
It is given the message dispatcher, data adapter, and the base-name of the
51+
workflow definition - i.e. the filename without the '.yaml' extension
52+
(expected to be in the workflow-definitions directory).
53+
54+
It loads the workflow definition into the API adapter, creates a running workflow
55+
from it and then sends a 'START' message which should cause the workflow engine to
56+
start the workflow."""
5057

5158
# To start a workflow we need to:
5259
# 1. Load and create a Workflow Definition
@@ -64,7 +71,7 @@ def start_workflow(md, da, workflow_file_name) -> str:
6471
assert wfid
6572
print(f"Created workflow definition {wfid}")
6673
# 2.
67-
response = da.create_running_workflow(workflow_definition_id=wfid)
74+
response = da.create_running_workflow(workflow_id=wfid)
6875
r_wfid = response["id"]
6976
assert r_wfid
7077
print(f"Created running workflow {r_wfid}")
@@ -81,7 +88,10 @@ def start_workflow(md, da, workflow_file_name) -> str:
8188

8289
def wait_for_workflow(da, mq, r_wfid, expect_success=True):
8390
"""A convenience function to wait for and check a workflow execution
84-
(by inspecting the anticipated DB/API records)."""
91+
(by inspecting the anticipated DB/API records). The workflow is expected
92+
to start (because start_workflow() has been called), this function
93+
waits for the running workflow to complete (by polling the API).
94+
"""
8595

8696
# We wait for the workflow to complete by polling the API and checking
8797
# the running workflow's 'done' status. The user can specify whether
@@ -106,7 +116,7 @@ def wait_for_workflow(da, mq, r_wfid, expect_success=True):
106116
assert r_wf["success"] == expect_success
107117

108118

109-
def test_workflow_engine_with_two_step_nop(basic_engine):
119+
def test_workflow_engine_example_two_step_nop(basic_engine):
110120
# Arrange
111121
da, mq, md, _ = basic_engine
112122

@@ -125,7 +135,7 @@ def test_workflow_engine_with_two_step_nop(basic_engine):
125135
assert step["running_workflow_step"]["success"]
126136

127137

128-
def test_workflow_engine_with_nop_fail(basic_engine):
138+
def test_workflow_engine_example_nop_fail(basic_engine):
129139
# Arrange
130140
da, mq, md, _ = basic_engine
131141

workflow/workflow_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def get_workflow_by_name(
9595
def create_running_workflow(
9696
self,
9797
*,
98-
workflow_definition_id: str,
98+
workflow_id: str,
9999
) -> Dict[str, Any]:
100100
"""Create a RunningWorkflow Record (from a Workflow)"""
101101
# Should return:

0 commit comments

Comments
 (0)