Skip to content

Commit a607716

Browse files
author
Alan Christie
committed
test: Unit Test DB adapter now able to save and get workflows
1 parent b91c289 commit a607716

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

tests/database_adapter.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,35 @@
1313
_JOB_DEFINITIONS: Dict[str, Any] = yaml.load(jd_file, Loader=yaml.FullLoader)
1414
assert _JOB_DEFINITIONS
1515

16+
_WORKFLOW_DEFINITION_ID_FORMAT: str = "workflow-00000000-0000-0000-0000-{id:012d}"
17+
1618

1719
class UnitTestDatabaseAdapter(DatabaseAdapter):
18-
"""A minimal Database adapter simply to serve-up Job Definitions
19-
from the job-definitions/job-definitions.yaml file."""
20+
"""A minimal Database adapter. It serves-up Job Definitions
21+
from the job-definitions/job-definitions.yaml file and provides basic
22+
(in-memory) storage for Workflow Definitions and related tables."""
23+
24+
def __init__(self):
25+
super().__init__()
26+
# A map of workflow definitions, keyed by workflow definition ID.
27+
self._workflow_definitions: Dict[str, Dict[str, Any]] = {}
28+
29+
def save_workflow(self, *, workflow_definition: Dict[str, Any]) -> str:
30+
next_id: int = len(self._workflow_definitions) + 1
31+
workflow_definition_id: str = _WORKFLOW_DEFINITION_ID_FORMAT.format(id=next_id)
32+
self._workflow_definitions[workflow_definition_id] = workflow_definition
33+
return workflow_definition_id
2034

2135
def get_workflow(self, *, workflow_definition_id: str) -> Optional[Dict[str, Any]]:
22-
return {}
36+
return self._workflow_definitions.get(workflow_definition_id, None)
2337

2438
def get_workflow_by_name(
2539
self, *, name: str, version: str
2640
) -> Optional[Dict[str, Any]]:
27-
return {}
41+
return next(
42+
(wd for wd in self._workflow_definitions.values() if wd["name"] == name),
43+
None,
44+
)
2845

2946
def get_job(
3047
self, *, collection: str, job: str, version: str

tests/test_test_database_adapter.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,51 @@ def test_get_nop_job():
1818

1919
# Assert
2020
assert jd["command"] == "python --version"
21+
22+
23+
def test_get_unknown_workflow():
24+
# Arrange
25+
utda = UnitTestDatabaseAdapter()
26+
27+
# Act
28+
wfid = utda.get_workflow(
29+
workflow_definition_id="workflow-00000000-0000-0000-0000-000000000001"
30+
)
31+
32+
# Assert
33+
assert wfid is None
34+
35+
36+
def test_save_workflow():
37+
# Arrange
38+
utda = UnitTestDatabaseAdapter()
39+
40+
# Act
41+
wfid = utda.save_workflow(workflow_definition={"name": "blah"})
42+
43+
# Assert
44+
assert wfid == "workflow-00000000-0000-0000-0000-000000000001"
45+
46+
47+
def test_get_workflow():
48+
# Arrange
49+
utda = UnitTestDatabaseAdapter()
50+
wfid = utda.save_workflow(workflow_definition={"name": "blah"})
51+
52+
# Act
53+
wf = utda.get_workflow(workflow_definition_id=wfid)
54+
55+
# Assert
56+
assert wf == {"name": "blah"}
57+
58+
59+
def test_get_workflow_by_name():
60+
# Arrange
61+
utda = UnitTestDatabaseAdapter()
62+
wfid = utda.save_workflow(workflow_definition={"name": "blah"})
63+
64+
# Act
65+
wf = utda.get_workflow_by_name(name="blah", version="1.0.0")
66+
67+
# Assert
68+
assert wf == {"name": "blah"}

workflow/workflow_abc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ class DatabaseAdapter(ABC):
6363
is responsible for creating RunningWorkflow and RunningWorkflowStep
6464
records for example."""
6565

66+
@abstractmethod
67+
def save_workflow(
68+
self,
69+
*,
70+
workflow_definition: Dict[str, Any],
71+
) -> str:
72+
"""Save a Workflow, getting an ID in return"""
73+
6674
@abstractmethod
6775
def get_workflow(
6876
self,

0 commit comments

Comments
 (0)