|
| 1 | +"""工作流画布拆分/源目标派生测试(纯函数)。""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from app.core.exceptions import CustomException |
| 10 | +from app.modules.task.storage.workflow.service import WorkflowService |
| 11 | + |
| 12 | + |
| 13 | +def _graph() -> dict: |
| 14 | + return { |
| 15 | + "nodes": [ |
| 16 | + {"id": "n1", "type": "storage", "position": {"x": 1, "y": 2}, "data": {"source_id": 1, "source_path": "a"}}, |
| 17 | + {"id": "n2", "position": {"x": 3, "y": 4}, "data": {"source_id": 2, "source_path": "b"}}, |
| 18 | + {"id": "n3", "data": {}}, |
| 19 | + ], |
| 20 | + "edges": [ |
| 21 | + {"id": "e1", "source": "n1", "target": "n2", "data": {"enabled": True, "transfer_mode": "stream"}}, |
| 22 | + {"id": "e2", "source": "n1", "target": "n2", "data": {}}, |
| 23 | + {"id": "e3", "source": "n1", "target": "missing"}, |
| 24 | + ], |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +def test_split_graph_valid_and_dedup() -> None: |
| 29 | + result = WorkflowService._split_graph(_graph()) |
| 30 | + assert [n.node_key for n in result.nodes] == ["n1", "n2"] |
| 31 | + assert result.sources[0].source_id == 1 |
| 32 | + assert result.targets[0].target_id == 2 and result.targets[0].target_path == "b" |
| 33 | + assert len(result.edges) == 1 and result.edges[0].enabled is True |
| 34 | + assert len(result.layout["nodes"]) == 3 and len(result.layout["edges"]) == 1 |
| 35 | + |
| 36 | + |
| 37 | +def test_split_graph_invalid_edges() -> None: |
| 38 | + with pytest.raises(CustomException): |
| 39 | + WorkflowService._split_graph({"nodes": [], "edges": []}) |
| 40 | + with pytest.raises(CustomException): |
| 41 | + WorkflowService._split_graph({"nodes": [], "edges": [{"id": "e", "source": "a", "target": "b"}]}) |
| 42 | + |
| 43 | + |
| 44 | +def test_split_graph_same_source_target() -> None: |
| 45 | + graph = { |
| 46 | + "nodes": [ |
| 47 | + {"id": "a", "data": {"source_id": 1}}, |
| 48 | + {"id": "b", "data": {"source_id": 1}}, |
| 49 | + ], |
| 50 | + "edges": [{"id": "e", "source": "a", "target": "b"}], |
| 51 | + } |
| 52 | + with pytest.raises(CustomException, match="源与目标不能相同"): |
| 53 | + WorkflowService._split_graph(graph) |
| 54 | + |
| 55 | + |
| 56 | +def test_sources_and_targets_from_edges() -> None: |
| 57 | + rows = [ |
| 58 | + SimpleNamespace(source_node_key="a", target_node_key="b"), |
| 59 | + SimpleNamespace(source_node_key="missing", target_node_key="c"), |
| 60 | + ] |
| 61 | + node_map = { |
| 62 | + "a": SimpleNamespace(source_id=1, source_path="a"), |
| 63 | + "b": SimpleNamespace(source_id=2, source_path="b"), |
| 64 | + "c": SimpleNamespace(source_id=3, source_path=None), |
| 65 | + } |
| 66 | + sources = WorkflowService._sources_from(rows, node_map) |
| 67 | + targets = WorkflowService._targets_from(rows, node_map) |
| 68 | + assert [s.source_id for s in sources] == [1] |
| 69 | + assert [(t.target_id, t.target_path) for t in targets] == [(2, "b"), (3, "")] |
0 commit comments