"""A Workflow is checked when it is built, and holds its names as plain strings.""" import dataclasses from enum import StrEnum import pytest from execution_engine.workflows.deliverables import DeliverableSpec from execution_engine.workflows.workflow import ( ValidationPolicy, Workflow, WorkflowError, ) from tests.toy.workflow import CHECK, MENU, PLAN, SHOPPING_LIST, TOY def spec(kind: str, phase: str, role: str, *depends_on: str) -> DeliverableSpec: return DeliverableSpec(kind, phase, role, depends_on=depends_on) def build( *specs: DeliverableSpec, phases=("one", "two"), final="b", **parts ) -> Workflow: return Workflow( name="t", about="", phases=phases, deliverables={each.kind: each for each in specs}, final=final, **parts, ) A, B = spec("a", "one", "first"), spec("b", "two", "second", "a") def test_a_workflow_that_holds_together_is_accepted(): workflow = build(A, B) assert workflow.specialists() == {"first": "a", "second": "b"} assert workflow.inputs_of("b") == ("a",) assert workflow.dependents_of("a") == ("b",) @pytest.mark.parametrize( ("specs", "parts", "problem"), [ ((A, spec("b", "two", "second", "c")), {}, "b: names unknown Deliverable c"), ((A, spec("b", "three", "second")), {}, "b: Phase three is not listed"), ((A, spec("b", "one", "second")), {}, "Phase two has no Deliverable"), ((A, spec("b", "two", "first")), {}, "b: first already produces a"), ( (spec("a", "one", "first", "b"), spec("b", "two", "second")), {}, "a: depends on b, in a later Phase", ), ((A, B), {"final": "z"}, "final Deliverable z is not declared"), ( (A, B), {"data_checks": {"z": ("z.json", lambda *_: [])}}, "unknown Deliverable z", ), ((A, B), {"spec_deliverable": "z"}, "unknown Deliverable z"), ( (A, B), {"validation": ValidationPolicy("b", "v.json", "a", "z")}, "unknown Deliverable z", ), ((A, B), {"phases": ("one", "two", "one")}, "a Phase is listed twice"), ], ) def test_a_workflow_that_does_not_hold_together_is_refused(specs, parts, problem): with pytest.raises(WorkflowError, match=problem): build(*specs, **parts) def test_a_spec_filed_under_another_kind_is_refused(): with pytest.raises(WorkflowError, match="a: its spec is for x"): Workflow( name="t", about="", phases=("one",), deliverables={"a": spec("x", "one", "first")}, final="a", ) def test_names_written_as_an_enum_are_held_as_plain_strings(): class Kind(StrEnum): A = "a" B = "b" class Phase(StrEnum): ONE = "one" TWO = "two" workflow = Workflow( name="t", about="", phases=tuple(Phase), deliverables={ Kind.A: DeliverableSpec(Kind.A, Phase.ONE, "first"), Kind.B: DeliverableSpec(Kind.B, Phase.TWO, "second", depends_on=(Kind.A,)), }, final=Kind.B, data_checks={Kind.A: ("a.json", lambda *_: [])}, ) # They go into checkpointed state, which is plain data. names = [ *workflow.phases, *workflow.deliverables, workflow.final, *workflow.data_checks, *workflow.deliverables["b"].depends_on, workflow.deliverables["b"].phase, ] assert all(type(name) is str for name in names) assert workflow.kind(Kind.A) == "a" assert type(workflow.kind(Kind.A)) is str def test_a_name_that_is_not_the_workflows_is_refused(): with pytest.raises( ValueError, match="'requirements' is not a Deliverable of picnic" ): TOY.kind("requirements") with pytest.raises(ValueError, match="'scoping' is not a Phase of picnic"): TOY.phase("scoping") def test_the_final_deliverable_reads_everything_and_is_shown_what_readers_are(): assert TOY.inputs_of(PLAN) == (MENU, SHOPPING_LIST, CHECK) given = { MENU: {"content": "# Menu\n", "attachments": {"menu.json": "[]"}}, SHOPPING_LIST: { "content": "# List\n\n## Totals\n\n3 items.\n", "attachments": {"shopping.json": "[]"}, }, } for_checker = TOY.shown(CHECK, TOY.dependencies_for(CHECK, given)) assert for_checker[SHOPPING_LIST]["content"] == "# List\n" # Not read in full: the data file alone. assert for_checker[MENU] == {"attachments": {"menu.json": "[]"}} def test_a_workflow_with_no_validation_never_reads_one(): plain = dataclasses.replace(TOY, validation=None) assert plain.validation is None assert plain.check_submission(MENU, "# Menu\n## Dishes\n", {"menu.json": "[]"}) == ( "menu.json: lists no dishes", )