|
| 1 | +import pytest |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from ..runner.pipeline import Pipeline |
| 5 | +from ..dataclasses.process_step import ProcessStep |
| 6 | +from ..dataclasses.process_step_describer import ProcessStepDescriber |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def linear_pipeline(): |
| 11 | + return {3: {2, 1}, 2: {1}} |
| 12 | + |
| 13 | + |
| 14 | +class DummyIoSources: |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +class DummyProcessStepDescriber: |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +class DummyProcessStep: |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +def test_linear_pipeline(linear_pipeline): |
| 27 | + "tests the sequence is expected for a linear graph" |
| 28 | + pipeline = Pipeline(graph=linear_pipeline) |
| 29 | + pipeline.prepare() |
| 30 | + sequence = [] |
| 31 | + while pipeline.is_active(): |
| 32 | + for node in pipeline.get_ready(): |
| 33 | + sequence.append(node) |
| 34 | + pipeline.done(node) |
| 35 | + assert sequence == [1, 2, 3] |
| 36 | + |
| 37 | + |
| 38 | +def test_node_addition(linear_pipeline): |
| 39 | + pipeline = Pipeline.from_dict(linear_pipeline) |
| 40 | + ps = DummyProcessStep() |
| 41 | + pipeline.add(ps, *[1, 2, 3]) |
| 42 | + pipeline.prepare() |
| 43 | + sequence = [] |
| 44 | + while pipeline.is_active(): |
| 45 | + for node in pipeline.get_ready(): |
| 46 | + sequence.append(node) |
| 47 | + pipeline.done(node) |
| 48 | + assert sequence == [1, 2, 3, ps] |
| 49 | + |
| 50 | + |
| 51 | +def test_branch_addition(linear_pipeline, pipeline_to_add={5: {6}}, at_node=2): |
| 52 | + """ |
| 53 | + add a pipeline as a branch on an existing pipeline, using the inherited add method |
| 54 | +
|
| 55 | + """ |
| 56 | + pipeline_1 = Pipeline(graph=linear_pipeline) |
| 57 | + pipeline_2 = Pipeline(graph=pipeline_to_add) |
| 58 | + pipeline_1.add(at_node, *pipeline_2.static_order()) |
| 59 | + assert [*pipeline_1.static_order()] == [1, 6, 5, 2, 3] |
| 60 | + |
| 61 | + |
| 62 | +def test_branch_addition_method(linear_pipeline, branch_graph={5: {6}}, branching_node=2): |
| 63 | + pipeline = Pipeline(graph=linear_pipeline) |
| 64 | + branch = Pipeline(graph=branch_graph) |
| 65 | + pipeline.add_incoming_branch(branch, branching_node=2) |
| 66 | + assert [*pipeline.static_order()] == [1, 6, 5, 2, 3] |
| 67 | + assert pipeline.graph == {3: {2, 1}, 2: {1, 5}, 5: {6}} |
| 68 | + |
| 69 | + |
| 70 | +def test_diverging_branch_addition( |
| 71 | + linear_pipeline, branch_graph={5: {6}, 6: set()}, branching_node=2 |
| 72 | +): |
| 73 | + pipeline = Pipeline(graph=linear_pipeline) |
| 74 | + branch = Pipeline(graph=branch_graph) |
| 75 | + pipeline.add_outgoing_branch(branch, branching_node) |
| 76 | + assert [*pipeline.static_order()] == [1, 2, 3, 6, 5] |
| 77 | + assert pipeline.graph == {3: {2, 1}, 2: {1}, 5: {6}, 6: {2}} |
0 commit comments