Skip to content

Commit ea33e58

Browse files
author
Alan Christie
committed
refactor: Further API refinements
1 parent aeaae42 commit ea33e58

File tree

3 files changed

+50
-110
lines changed

3 files changed

+50
-110
lines changed

tests/test_test_api_adapter.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,3 @@ def test_create_task():
276276

277277
# Assert
278278
assert "id" in response
279-
280-
281-
def test_create_and_get_task():
282-
# Arrange
283-
utaa = UnitTestWorkflowAPIAdapter()
284-
response = utaa.create_task()
285-
task_id = response["id"]
286-
287-
# Act
288-
response = utaa.get_task(task_id=task_id)
289-
290-
# Assert
291-
assert not response["done"]
292-
assert response["exit_code"] == 0

tests/wapi_adapter.py

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,6 @@ def __init__(self):
8080
Pickler(pickle_file).dump({})
8181
UnitTestWorkflowAPIAdapter.lock.release()
8282

83-
def create_workflow(self, *, workflow_definition: dict[str, Any]) -> dict[str, Any]:
84-
UnitTestWorkflowAPIAdapter.lock.acquire()
85-
with open(_WORKFLOW_PICKLE_FILE, "rb") as pickle_file:
86-
workflow = Unpickler(pickle_file).load()
87-
88-
next_id: int = len(workflow) + 1
89-
workflow_definition_id: str = _WORKFLOW_DEFINITION_ID_FORMAT.format(id=next_id)
90-
workflow[workflow_definition_id] = workflow_definition
91-
92-
with open(_WORKFLOW_PICKLE_FILE, "wb") as pickle_file:
93-
Pickler(pickle_file).dump(workflow)
94-
UnitTestWorkflowAPIAdapter.lock.release()
95-
96-
return {"id": workflow_definition_id}
97-
9883
def get_workflow(self, *, workflow_id: str) -> dict[str, Any]:
9984
UnitTestWorkflowAPIAdapter.lock.acquire()
10085
with open(_WORKFLOW_PICKLE_FILE, "rb") as pickle_file:
@@ -103,40 +88,6 @@ def get_workflow(self, *, workflow_id: str) -> dict[str, Any]:
10388

10489
return {"workflow": workflow[workflow_id]} if workflow_id in workflow else {}
10590

106-
def create_running_workflow(
107-
self,
108-
*,
109-
user_id: str,
110-
workflow_id: str,
111-
project_id: str,
112-
variables: dict[str, Any],
113-
) -> dict[str, Any]:
114-
assert user_id
115-
assert isinstance(variables, dict)
116-
117-
UnitTestWorkflowAPIAdapter.lock.acquire()
118-
with open(_RUNNING_WORKFLOW_PICKLE_FILE, "rb") as pickle_file:
119-
running_workflow = Unpickler(pickle_file).load()
120-
121-
next_id: int = len(running_workflow) + 1
122-
running_workflow_id: str = _RUNNING_WORKFLOW_ID_FORMAT.format(id=next_id)
123-
record = {
124-
"user_id": user_id,
125-
"user_api_token": "123456789",
126-
"done": False,
127-
"success": False,
128-
"workflow": workflow_id,
129-
"project_id": project_id,
130-
"variables": variables,
131-
}
132-
running_workflow[running_workflow_id] = record
133-
134-
with open(_RUNNING_WORKFLOW_PICKLE_FILE, "wb") as pickle_file:
135-
Pickler(pickle_file).dump(running_workflow)
136-
UnitTestWorkflowAPIAdapter.lock.release()
137-
138-
return {"id": running_workflow_id}
139-
14091
def get_running_workflow(self, *, running_workflow_id: str) -> dict[str, Any]:
14192
UnitTestWorkflowAPIAdapter.lock.acquire()
14293
with open(_RUNNING_WORKFLOW_PICKLE_FILE, "rb") as pickle_file:
@@ -251,14 +202,6 @@ def get_instance(self, *, instance_id: str) -> dict[str, Any]:
251202

252203
return {} if instance_id not in instances else instances[instance_id]
253204

254-
def get_task(self, *, task_id: str) -> dict[str, Any]:
255-
UnitTestWorkflowAPIAdapter.lock.acquire()
256-
with open(_TASK_PICKLE_FILE, "rb") as pickle_file:
257-
tasks = Unpickler(pickle_file).load()
258-
UnitTestWorkflowAPIAdapter.lock.release()
259-
260-
return {} if task_id not in tasks else tasks[task_id]
261-
262205
def get_job(self, *, collection: str, job: str, version: str) -> dict[str, Any]:
263206
assert collection == _JOB_DEFINITIONS["collection"]
264207
assert job in _JOB_DEFINITIONS["jobs"]
@@ -270,9 +213,58 @@ def get_job(self, *, collection: str, job: str, version: str) -> dict[str, Any]:
270213
response["variables"] = jd["variables"]
271214
return response
272215

273-
# Methods required for the UnitTestInstanceLauncher
216+
# Methods required for the UnitTestInstanceLauncher and other (internal) logic
274217
# but not exposed to (or required by) the Workflow Engine...
275218

219+
def create_workflow(self, *, workflow_definition: dict[str, Any]) -> dict[str, Any]:
220+
UnitTestWorkflowAPIAdapter.lock.acquire()
221+
with open(_WORKFLOW_PICKLE_FILE, "rb") as pickle_file:
222+
workflow = Unpickler(pickle_file).load()
223+
224+
next_id: int = len(workflow) + 1
225+
workflow_definition_id: str = _WORKFLOW_DEFINITION_ID_FORMAT.format(id=next_id)
226+
workflow[workflow_definition_id] = workflow_definition
227+
228+
with open(_WORKFLOW_PICKLE_FILE, "wb") as pickle_file:
229+
Pickler(pickle_file).dump(workflow)
230+
UnitTestWorkflowAPIAdapter.lock.release()
231+
232+
return {"id": workflow_definition_id}
233+
234+
def create_running_workflow(
235+
self,
236+
*,
237+
user_id: str,
238+
workflow_id: str,
239+
project_id: str,
240+
variables: dict[str, Any],
241+
) -> dict[str, Any]:
242+
assert user_id
243+
assert isinstance(variables, dict)
244+
245+
UnitTestWorkflowAPIAdapter.lock.acquire()
246+
with open(_RUNNING_WORKFLOW_PICKLE_FILE, "rb") as pickle_file:
247+
running_workflow = Unpickler(pickle_file).load()
248+
249+
next_id: int = len(running_workflow) + 1
250+
running_workflow_id: str = _RUNNING_WORKFLOW_ID_FORMAT.format(id=next_id)
251+
record = {
252+
"user_id": user_id,
253+
"user_api_token": "123456789",
254+
"done": False,
255+
"success": False,
256+
"workflow": workflow_id,
257+
"project_id": project_id,
258+
"variables": variables,
259+
}
260+
running_workflow[running_workflow_id] = record
261+
262+
with open(_RUNNING_WORKFLOW_PICKLE_FILE, "wb") as pickle_file:
263+
Pickler(pickle_file).dump(running_workflow)
264+
UnitTestWorkflowAPIAdapter.lock.release()
265+
266+
return {"id": running_workflow_id}
267+
276268
def create_instance(self, *, running_workflow_step_id: str) -> dict[str, Any]:
277269
UnitTestWorkflowAPIAdapter.lock.acquire()
278270
with open(_INSTANCE_PICKLE_FILE, "rb") as pickle_file:

workflow/workflow_abc.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,6 @@ class WorkflowAPIAdapter(ABC):
7474
Workflow, RunningWorkflow and RunningWorkflowStep records returning dictionary
7575
(API-like) responses."""
7676

77-
@abstractmethod
78-
def create_workflow(
79-
self,
80-
*,
81-
workflow_definition: dict[str, Any],
82-
) -> dict[str, Any]:
83-
"""Create a Workflow, getting an ID in return"""
84-
# Should return:
85-
# {
86-
# "id": "workflow-00000000-0000-0000-0000-000000000001",
87-
# }
88-
8977
@abstractmethod
9078
def get_workflow(
9179
self,
@@ -99,21 +87,6 @@ def get_workflow(
9987
# }
10088
# If not present an empty dictionary should be returned.
10189

102-
@abstractmethod
103-
def create_running_workflow(
104-
self,
105-
*,
106-
workflow_id: str,
107-
project_id: str,
108-
variables: dict[str, Any],
109-
user_id: str,
110-
) -> dict[str, Any]:
111-
"""Create a RunningWorkflow Record (from a Workflow)"""
112-
# Should return:
113-
# {
114-
# "id": "r-workflow-00000000-0000-0000-0000-000000000001",
115-
# }
116-
11790
@abstractmethod
11891
def get_running_workflow(self, *, running_workflow_id: str) -> dict[str, Any]:
11992
"""Get a RunningWorkflow Record"""
@@ -220,17 +193,6 @@ def get_instance(self, *, instance_id: str) -> dict[str, Any]:
220193
# }
221194
# If not present an empty dictionary should be returned.
222195

223-
@abstractmethod
224-
def get_task(self, *, task_id: str) -> dict[str, Any]:
225-
"""Get a Task Record"""
226-
# Should return:
227-
# {
228-
# "done": True,
229-
# "exit_code": 0,
230-
# [...],
231-
# }
232-
# If not present an empty dictionary should be returned.
233-
234196
@abstractmethod
235197
def get_job(
236198
self,

0 commit comments

Comments
 (0)