Skip to content

Commit 59d12bb

Browse files
author
Alan Christie
committed
feat: Add simple test DB adapter (reads test JD file)
1 parent c72aba6 commit 59d12bb

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

tests/database_adapter.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from typing import Any, Dict, Optional
3+
4+
import yaml
5+
6+
from workflow.workflow_abc import DatabaseAdapter
7+
8+
# Load the Unit test Job Definitions file now.
9+
_JOB_DEFINITION_FILE: str = os.path.join(
10+
os.path.dirname(__file__), "job-definitions", "job-definitions.yaml"
11+
)
12+
with open(_JOB_DEFINITION_FILE, "r", encoding="utf8") as jd_file:
13+
_JOB_DEFINITIONS: Dict[str, Any] = yaml.load(jd_file, Loader=yaml.FullLoader)
14+
assert _JOB_DEFINITIONS
15+
16+
17+
class UnitTestDatabaseAdapter(DatabaseAdapter):
18+
"""A minimal Database adapter simply to serve-up Job Definitions."""
19+
20+
def get_workflow(self, *, workflow_definition_id: str) -> Optional[Dict[str, Any]]:
21+
return {}
22+
23+
def get_workflow_by_name(
24+
self, *, name: str, version: str
25+
) -> Optional[Dict[str, Any]]:
26+
return {}
27+
28+
def get_job(
29+
self, *, collection: str, job: str, version: str
30+
) -> Optional[Dict[str, Any]]:
31+
if collection != _JOB_DEFINITIONS["collection"]:
32+
return 1
33+
if job not in _JOB_DEFINITIONS["jobs"]:
34+
return 2
35+
if version != _JOB_DEFINITIONS["jobs"][job]["version"]:
36+
return 3
37+
jd = _JOB_DEFINITIONS["jobs"][job]
38+
response = {"command": jd["command"]}
39+
if "variables" in jd:
40+
response["variables"] = jd["variables"]
41+
return response
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
# Job definitions used exclusively for unit testing.
3+
# They provide sufficient information to 'masquerade' as real jobs
4+
# and are served up by the UnitTestDatabaseAdapter (found in the tests directory).
5+
#
6+
# This offers bare-essential job definitions that are used to test
7+
# the workflow engine. It does not fully comply with the Job definition schema -
8+
# only those that we need for testing the Workflow Engine.
9+
#
10+
# All jobs used for unit testing must be defined in this file.
11+
#
12+
# For each job you must provide: -
13+
# - Job version (and only one version for each Job)
14+
# - Job command
15+
16+
collection: workflow-engine-unit-test-jobs
17+
18+
jobs:
19+
20+
nop:
21+
version: "1.0.0"
22+
command: >-
23+
python --version

tests/test_database_adapter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Tests for the decoder package.
2+
import os
3+
from typing import Any, Dict
4+
5+
import pytest
6+
import yaml
7+
8+
pytestmark = pytest.mark.unit
9+
10+
from tests.database_adapter import UnitTestDatabaseAdapter
11+
12+
13+
def test_get_nop_job():
14+
# Arrange
15+
utda = UnitTestDatabaseAdapter()
16+
17+
# Act
18+
jd = utda.get_job(
19+
collection="workflow-engine-unit-test-jobs", job="nop", version="1.0.0"
20+
)
21+
22+
# Assert
23+
assert jd["command"] == "python --version"

tests/test_worflow_validator.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
1-
# Tests for the decoder package.
2-
from typing import Any, Dict
1+
# Tests for the WorkflowValidator
32

43
import pytest
54

65
pytestmark = pytest.mark.unit
76

7+
from tests.database_adapter import UnitTestDatabaseAdapter
88
from tests.test_decoder_minimal import _MINIMAL_WORKFLOW
9-
from workflow.worklfow_validator import (
10-
DatabaseAdapter,
11-
ValidationLevel,
12-
WorkflowValidator,
13-
)
14-
15-
16-
class DummyDatabaseAdapter(DatabaseAdapter):
17-
"""A minimal Database adapter simply to satisfy the tests in this file."""
18-
19-
def get_workflow(self, *, workflow_definition_id: str) -> Dict[str, Any]:
20-
return _MINIMAL_WORKFLOW
21-
22-
def get_workflow_by_name(self, *, name: str, version: str) -> Dict[str, Any]:
23-
return _MINIMAL_WORKFLOW
24-
25-
def get_job(self, *, collection: str, job: str, version: str) -> Dict[str, Any]:
26-
return {}
9+
from workflow.worklfow_validator import ValidationLevel, WorkflowValidator
2710

2811

2912
def test_validate_minimal_for_create():
3013
# Arrange
31-
db_adapter = DummyDatabaseAdapter()
14+
db_adapter = UnitTestDatabaseAdapter()
3215
validator = WorkflowValidator(db_adapter=db_adapter)
3316

3417
# Act

0 commit comments

Comments
 (0)