|
| 1 | +# A unit (functional) test for the WorkflowEngine's handling of 'Example 1'. |
| 2 | +import time |
| 3 | +from datetime import datetime, timezone |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +pytestmark = pytest.mark.unit |
| 8 | + |
| 9 | +from informaticsmatters.protobuf.datamanager.workflow_message_pb2 import WorkflowMessage |
| 10 | + |
| 11 | +from tests.database_adapter import UnitTestDatabaseAdapter |
| 12 | +from tests.instance_launcher import UnitTestInstanceLauncher |
| 13 | +from tests.message_dispatcher import UnitTestMessageDispatcher |
| 14 | +from tests.message_queue import UnitTestMessageQueue |
| 15 | +from workflow.workflow_engine import WorkflowEngine |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def basic_engine(): |
| 20 | + utda = UnitTestDatabaseAdapter() |
| 21 | + utmq = UnitTestMessageQueue() |
| 22 | + utmd = UnitTestMessageDispatcher(msg_queue=utmq) |
| 23 | + util = UnitTestInstanceLauncher(msg_dispatcher=utmd) |
| 24 | + return [utda, utmd, WorkflowEngine(db_adapter=utda, instance_launcher=util)] |
| 25 | + |
| 26 | + |
| 27 | +def test_workflow_engine_with_example_1(basic_engine): |
| 28 | + # Arrange |
| 29 | + da, md, engine = basic_engine |
| 30 | + # LOAD THE EXAMPLE-1 WORKFLOW DEFINITION INTO THE DATABASE |
| 31 | + # TODO |
| 32 | + # SIMULATE THE API CREATION OF A RUNNING WORKFLOW FROM THE WORKFLOW |
| 33 | + wfid = da.save_workflow(workflow_definition={"name": "example-1"}) |
| 34 | + assert wfid |
| 35 | + response = da.create_running_workflow(workflow_definition_id=wfid) |
| 36 | + r_wfid = response["id"] |
| 37 | + assert r_wfid |
| 38 | + |
| 39 | + # Act |
| 40 | + # SEND A MESSAGE TO THE ENGINE (VIA THE MESSAGE DISPATCHER) TO START THE WORKFLOW |
| 41 | + # THE RUNNING WORKFLOW WILL HAVE THE ID "1" |
| 42 | + msg = WorkflowMessage() |
| 43 | + msg.timestamp = f"{datetime.now(timezone.utc).isoformat()}Z" |
| 44 | + msg.action = "START" |
| 45 | + msg.running_workflow = r_wfid |
| 46 | + md.send(msg) |
| 47 | + |
| 48 | + # Assert |
| 49 | + # Wait until the workflow is done (successfully) |
| 50 | + # But don't wait for ever! |
| 51 | + attempts = 0 |
| 52 | + done = False |
| 53 | + r_wf = None |
| 54 | + while not done: |
| 55 | + response = da.get_running_workflow(running_workflow_id=r_wfid) |
| 56 | + r_wf = response["running-workflow"] |
| 57 | + if r_wf["done"]: |
| 58 | + done = True |
| 59 | + else: |
| 60 | + attempts += 1 |
| 61 | + if attempts > 10: |
| 62 | + break |
| 63 | + time.sleep(0.5) |
| 64 | + assert r_wf |
| 65 | + # TODO - The following should be 'success' but the implementation does not set it yet |
| 66 | + assert not r_wf["success"] |
| 67 | + # Now check there are the right number of RunningWorkflowStep Records |
| 68 | + # (and they're all set to success/done) |
| 69 | + response = da.get_running_workflow_steps(running_workflow_id=r_wfid) |
| 70 | + # TODO - The following should not be zero but the implementation does not set it yet |
| 71 | + assert response["count"] == 0 |
| 72 | + for step in response["running-workflow-steps"]: |
| 73 | + assert step["running-workflow-step"]["done"] |
| 74 | + assert step["running-workflow-step"]["success"] |
0 commit comments