Skip to content

Commit 77b06d5

Browse files
author
Alan Christie
committed
refactor: Further API refactoring (DM-friendly responses)
1 parent da7f8fe commit 77b06d5

File tree

7 files changed

+78
-77
lines changed

7 files changed

+78
-77
lines changed

tests/instance_launcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def launch(self, launch_parameters: LaunchParameters) -> LaunchResult:
7171

7272
# We're passed a RunningWorkflowStep ID but a record is expected to have been
7373
# created bt the caller, we simply create instance records.
74-
response = self._api_adapter.get_running_workflow_step(
74+
response, _ = self._api_adapter.get_running_workflow_step(
7575
running_workflow_step_id=launch_parameters.running_workflow_step_id
7676
)
7777
assert "running_workflow_step" in response
@@ -83,7 +83,7 @@ def launch(self, launch_parameters: LaunchParameters) -> LaunchResult:
8383
task_id = "task-00000000-0000-0000-0000-000000000001"
8484

8585
# Apply variables to the step's Job command.
86-
job = self._api_adapter.get_job(
86+
job, _ = self._api_adapter.get_job(
8787
collection=launch_parameters.specification["collection"],
8888
job=launch_parameters.specification["job"],
8989
version="do-not-care",

tests/test_test_api_adapter.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_get_nop_job():
1111
utaa = UnitTestWorkflowAPIAdapter()
1212

1313
# Act
14-
jd = utaa.get_job(
14+
jd, _ = utaa.get_job(
1515
collection="workflow-engine-unit-test-jobs", job="nop", version="1.0.0"
1616
)
1717

@@ -24,7 +24,9 @@ def test_get_unknown_workflow():
2424
utaa = UnitTestWorkflowAPIAdapter()
2525

2626
# Act
27-
wfd = utaa.get_workflow(workflow_id="workflow-00000000-0000-0000-0000-000000000001")
27+
wfd, _ = utaa.get_workflow(
28+
workflow_id="workflow-00000000-0000-0000-0000-000000000001"
29+
)
2830

2931
# Assert
3032
assert wfd == {}
@@ -48,7 +50,7 @@ def test_get_workflow():
4850
wfid = response["id"]
4951

5052
# Act
51-
wf = utaa.get_workflow(workflow_id=wfid)
53+
wf, _ = utaa.get_workflow(workflow_id=wfid)
5254

5355
# Assert
5456
assert wf["workflow"]["name"] == "blah"
@@ -85,7 +87,7 @@ def test_get_running_workflow():
8587
rwfid = response["id"]
8688

8789
# Act
88-
response = utaa.get_running_workflow(running_workflow_id=rwfid)
90+
response, _ = utaa.get_running_workflow(running_workflow_id=rwfid)
8991

9092
# Assert
9193
rwf = response["running_workflow"]
@@ -110,7 +112,7 @@ def test_set_running_workflow_done_when_success():
110112
utaa.set_running_workflow_done(running_workflow_id=rwfid, success=True)
111113

112114
# Assert
113-
response = utaa.get_running_workflow(running_workflow_id=rwfid)
115+
response, _ = utaa.get_running_workflow(running_workflow_id=rwfid)
114116
assert response["running_workflow"]["done"]
115117
assert response["running_workflow"]["success"]
116118
assert response["running_workflow"]["error"] is None
@@ -135,7 +137,7 @@ def test_set_running_workflow_done_when_failed():
135137
)
136138

137139
# Assert
138-
response = utaa.get_running_workflow(running_workflow_id=rwfid)
140+
response, _ = utaa.get_running_workflow(running_workflow_id=rwfid)
139141
assert response["running_workflow"]["done"]
140142
assert not response["running_workflow"]["success"]
141143
assert response["running_workflow"]["error"] == 1
@@ -154,7 +156,7 @@ def test_create_running_workflow_step():
154156
)
155157

156158
# Act
157-
response = utaa.create_running_workflow_step(
159+
response, _ = utaa.create_running_workflow_step(
158160
running_workflow_id=response["id"], step="step-1"
159161
)
160162

@@ -172,7 +174,7 @@ def test_set_running_workflow_step_done_when_success():
172174
project_id=TEST_PROJECT_ID,
173175
variables={},
174176
)
175-
response = utaa.create_running_workflow_step(
177+
response, _ = utaa.create_running_workflow_step(
176178
running_workflow_id=response["id"], step="step-1"
177179
)
178180
rwfsid = response["id"]
@@ -181,7 +183,7 @@ def test_set_running_workflow_step_done_when_success():
181183
utaa.set_running_workflow_step_done(running_workflow_step_id=rwfsid, success=True)
182184

183185
# Assert
184-
response = utaa.get_running_workflow_step(running_workflow_step_id=rwfsid)
186+
response, _ = utaa.get_running_workflow_step(running_workflow_step_id=rwfsid)
185187
assert response["running_workflow_step"]["done"]
186188
assert response["running_workflow_step"]["success"]
187189
assert response["running_workflow_step"]["error"] is None
@@ -198,7 +200,7 @@ def test_set_running_workflow_step_done_when_failed():
198200
project_id=TEST_PROJECT_ID,
199201
variables={},
200202
)
201-
response = utaa.create_running_workflow_step(
203+
response, _ = utaa.create_running_workflow_step(
202204
running_workflow_id=response["id"], step="step-1"
203205
)
204206
rwfsid = response["id"]
@@ -209,7 +211,7 @@ def test_set_running_workflow_step_done_when_failed():
209211
)
210212

211213
# Assert
212-
response = utaa.get_running_workflow_step(running_workflow_step_id=rwfsid)
214+
response, _ = utaa.get_running_workflow_step(running_workflow_step_id=rwfsid)
213215
assert response["running_workflow_step"]["done"]
214216
assert not response["running_workflow_step"]["success"]
215217
assert response["running_workflow_step"]["error"] == 1
@@ -228,13 +230,13 @@ def test_get_running_workflow_step():
228230
variables={},
229231
)
230232
rwfid = response["id"]
231-
response = utaa.create_running_workflow_step(
233+
response, _ = utaa.create_running_workflow_step(
232234
running_workflow_id=rwfid, step="step-1"
233235
)
234236
rwfsid = response["id"]
235237

236238
# Act
237-
response = utaa.get_running_workflow_step(running_workflow_step_id=rwfsid)
239+
response, _ = utaa.get_running_workflow_step(running_workflow_step_id=rwfsid)
238240

239241
# Assert
240242
rwfs = response["running_workflow_step"]
@@ -261,7 +263,7 @@ def test_create_and_get_instance():
261263
instance_id = response["id"]
262264

263265
# Act
264-
response = utaa.get_instance(instance_id=instance_id)
266+
response, _ = utaa.get_instance(instance_id=instance_id)
265267

266268
# Assert
267269
assert response["running_workflow_step"] == "r-workflow-step-000"

tests/test_test_instance_launcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_launch_nop(basic_launcher):
3636
project_id=TEST_PROJECT_ID,
3737
variables={},
3838
)
39-
response = utaa.create_running_workflow_step(
39+
response, _ = utaa.create_running_workflow_step(
4040
running_workflow_id=response["id"], step="step-1"
4141
)
4242
rwfsid = response["id"]
@@ -73,7 +73,7 @@ def test_launch_nop_fail(basic_launcher):
7373
variables={},
7474
)
7575
rwfid = response["id"]
76-
response = utaa.create_running_workflow_step(
76+
response, _ = utaa.create_running_workflow_step(
7777
running_workflow_id=response["id"], step="step-1"
7878
)
7979
rwfsid = response["id"]
@@ -113,7 +113,7 @@ def test_launch_smiles_to_file(basic_launcher):
113113
variables={},
114114
)
115115
rwfid = response["id"]
116-
response = utaa.create_running_workflow_step(
116+
response, _ = utaa.create_running_workflow_step(
117117
running_workflow_id=response["id"], step="step-1"
118118
)
119119
rwfsid = response["id"]

tests/test_workflow_engine_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def wait_for_workflow(
110110
attempts = 0
111111
done = False
112112
while not done:
113-
response = da.get_running_workflow(running_workflow_id=r_wfid)
113+
response, _ = da.get_running_workflow(running_workflow_id=r_wfid)
114114
assert "running_workflow" in response
115115
r_wf = response["running_workflow"]
116116
if r_wf["done"]:

tests/wapi_adapter.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ def get_workflow(self, *, workflow_id: str) -> dict[str, Any]:
8383
workflow = Unpickler(pickle_file).load()
8484
UnitTestWorkflowAPIAdapter.lock.release()
8585

86-
return {"workflow": workflow[workflow_id]} if workflow_id in workflow else {}
86+
response = (
87+
{"workflow": workflow[workflow_id]} if workflow_id in workflow else {}
88+
)
89+
return response, 0
8790

8891
def get_running_workflow(self, *, running_workflow_id: str) -> dict[str, Any]:
8992
UnitTestWorkflowAPIAdapter.lock.acquire()
@@ -93,7 +96,7 @@ def get_running_workflow(self, *, running_workflow_id: str) -> dict[str, Any]:
9396

9497
if running_workflow_id not in running_workflow:
9598
return {}
96-
return {"running_workflow": running_workflow[running_workflow_id]}
99+
return {"running_workflow": running_workflow[running_workflow_id]}, 0
97100

98101
def set_running_workflow_done(
99102
self,
@@ -140,7 +143,7 @@ def create_running_workflow_step(
140143
Pickler(pickle_file).dump(running_workflow_step)
141144
UnitTestWorkflowAPIAdapter.lock.release()
142145

143-
return {"id": running_workflow_step_id}
146+
return {"id": running_workflow_step_id}, 0
144147

145148
def get_running_workflow_step(
146149
self, *, running_workflow_step_id: str
@@ -151,10 +154,10 @@ def get_running_workflow_step(
151154
UnitTestWorkflowAPIAdapter.lock.release()
152155

153156
if running_workflow_step_id not in running_workflow_step:
154-
return {}
157+
return {}, 0
155158
return {
156159
"running_workflow_step": running_workflow_step[running_workflow_step_id]
157-
}
160+
}, 0
158161

159162
def set_running_workflow_step_done(
160163
self,
@@ -178,26 +181,14 @@ def set_running_workflow_step_done(
178181
Pickler(pickle_file).dump(running_workflow_step)
179182
UnitTestWorkflowAPIAdapter.lock.release()
180183

181-
def get_running_workflow_steps(self, *, running_workflow_id: str) -> dict[str, Any]:
182-
UnitTestWorkflowAPIAdapter.lock.acquire()
183-
with open(_RUNNING_WORKFLOW_STEP_PICKLE_FILE, "rb") as pickle_file:
184-
running_workflow_step = Unpickler(pickle_file).load()
185-
UnitTestWorkflowAPIAdapter.lock.release()
186-
187-
steps = []
188-
for key, value in running_workflow_step.items():
189-
if value["running_workflow"] == running_workflow_id:
190-
item = {"running_workflow_step": value, "id": key}
191-
steps.append(item)
192-
return {"count": len(steps), "running_workflow_steps": steps}
193-
194184
def get_instance(self, *, instance_id: str) -> dict[str, Any]:
195185
UnitTestWorkflowAPIAdapter.lock.acquire()
196186
with open(_INSTANCE_PICKLE_FILE, "rb") as pickle_file:
197187
instances = Unpickler(pickle_file).load()
198188
UnitTestWorkflowAPIAdapter.lock.release()
199189

200-
return {} if instance_id not in instances else instances[instance_id]
190+
response = {} if instance_id not in instances else instances[instance_id]
191+
return response, 0
201192

202193
def get_job(self, *, collection: str, job: str, version: str) -> dict[str, Any]:
203194
assert collection == _JOB_DEFINITIONS["collection"]
@@ -208,7 +199,7 @@ def get_job(self, *, collection: str, job: str, version: str) -> dict[str, Any]:
208199
response = {"command": jd["command"]}
209200
if "variables" in jd:
210201
response["variables"] = jd["variables"]
211-
return response
202+
return response, 0
212203

213204
# Methods required for the UnitTestInstanceLauncher and other (internal) logic
214205
# but not exposed to (or required by) the Workflow Engine...
@@ -279,3 +270,16 @@ def create_instance(self, *, running_workflow_step_id: str) -> dict[str, Any]:
279270
UnitTestWorkflowAPIAdapter.lock.release()
280271

281272
return {"id": instance_id}
273+
274+
def get_running_workflow_steps(self, *, running_workflow_id: str) -> dict[str, Any]:
275+
UnitTestWorkflowAPIAdapter.lock.acquire()
276+
with open(_RUNNING_WORKFLOW_STEP_PICKLE_FILE, "rb") as pickle_file:
277+
running_workflow_step = Unpickler(pickle_file).load()
278+
UnitTestWorkflowAPIAdapter.lock.release()
279+
280+
steps = []
281+
for key, value in running_workflow_step.items():
282+
if value["running_workflow"] == running_workflow_id:
283+
item = {"running_workflow_step": value, "id": key}
284+
steps.append(item)
285+
return {"count": len(steps), "running_workflow_steps": steps}

workflow/workflow_abc.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,29 @@ def get_workflow(
7979
self,
8080
*,
8181
workflow_id: str,
82-
) -> dict[str, Any]:
82+
) -> tuple[dict[str, Any], int]:
8383
"""Get a Workflow Record by ID."""
8484
# If present this should return:
8585
# {
86-
# "workflow": <workflow>,
86+
# "workflow": {
87+
# "steps": [
88+
# {
89+
# "name": "step-name"
90+
# }
91+
# ]
92+
# }
8793
# }
8894
# If not present an empty dictionary should be returned.
95+
#
96+
# The 'int' in the return tuple here (and elsewhere in this ABC)
97+
# is an HTTP status code to simplify the DM implementation,
98+
# and allow it to re-use any 'views.py' function that may be defined.
99+
# This value is ignored by the Engine.
89100

90101
@abstractmethod
91-
def get_running_workflow(self, *, running_workflow_id: str) -> dict[str, Any]:
102+
def get_running_workflow(
103+
self, *, running_workflow_id: str
104+
) -> tuple[dict[str, Any], int]:
92105
"""Get a RunningWorkflow Record"""
93106
# Should return:
94107
# {
@@ -124,7 +137,7 @@ def create_running_workflow_step(
124137
*,
125138
running_workflow_id: str,
126139
step: str,
127-
) -> dict[str, Any]:
140+
) -> tuple[dict[str, Any], int]:
128141
"""Create a RunningWorkflowStep Record (from a RunningWorkflow)"""
129142
# Should return:
130143
# {
@@ -134,7 +147,7 @@ def create_running_workflow_step(
134147
@abstractmethod
135148
def get_running_workflow_step(
136149
self, *, running_workflow_step_id: str
137-
) -> dict[str, Any]:
150+
) -> tuple[dict[str, Any], int]:
138151
"""Get a RunningWorkflowStep Record"""
139152
# Should return:
140153
# {
@@ -162,29 +175,7 @@ def set_running_workflow_step_done(
162175
If not successful an error code and message should be provided."""
163176

164177
@abstractmethod
165-
def get_running_workflow_steps(self, *, running_workflow_id: str) -> dict[str, Any]:
166-
"""Gets all the RunningWorkflowStep Records (for a RunningWorkflow)"""
167-
# Should return:
168-
# {
169-
# "count": 1,
170-
# "running_workflow_steps": [
171-
# {
172-
# "id": "r-workflow-step-00000000-0000-0000-0000-000000000001",
173-
# "running_workflow_step": {
174-
# "step:": "step-1234",
175-
# "done": False,
176-
# "success": false,
177-
# "error": None,
178-
# "error_msg": None,
179-
# "workflow": "workflow-00000000-0000-0000-0000-000000000001",
180-
# }
181-
# ...
182-
# ]
183-
# }
184-
# If there are not steps an empty dictionary should be returned and a count of 0
185-
186-
@abstractmethod
187-
def get_instance(self, *, instance_id: str) -> dict[str, Any]:
178+
def get_instance(self, *, instance_id: str) -> tuple[dict[str, Any], int]:
188179
"""Get an Instance Record"""
189180
# Should return:
190181
# {
@@ -200,7 +191,7 @@ def get_job(
200191
collection: str,
201192
job: str,
202193
version: str,
203-
) -> dict[str, Any]:
194+
) -> tuple[dict[str, Any], int]:
204195
"""Get a Job"""
205196
# If not present an empty dictionary should be returned.
206197

0 commit comments

Comments
 (0)