|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright © 2025 IBM |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os, yaml, unittest |
| 18 | + |
| 19 | +from unittest import TestCase |
| 20 | + |
| 21 | +from src.mermaid import Mermaid |
| 22 | +from src.workflow import Workflow |
| 23 | + |
| 24 | +def parse_yaml(file_path): |
| 25 | + with open(file_path, "r") as file: |
| 26 | + yaml_data = list(yaml.safe_load_all(file)) |
| 27 | + return yaml_data |
| 28 | + |
| 29 | +class TestMermaid(TestCase): |
| 30 | + def setUp(self): |
| 31 | + self.workflow_yaml = parse_yaml(os.path.join(os.path.dirname(__file__),"../yamls/workflows/simple_workflow.yaml")) |
| 32 | + |
| 33 | + def tearDown(self): |
| 34 | + self.workflow_yaml = None |
| 35 | + |
| 36 | + def test_markdown_sequenceDiagram(self): |
| 37 | + mermaid = Mermaid(self.workflow_yaml, "sequenceDiagram") |
| 38 | + self.assertTrue(mermaid.to_markdown().startswith("sequenceDiagram")) |
| 39 | + |
| 40 | + for agent in ['test1', 'test2', 'test3']: |
| 41 | + self.assertTrue(f"participant {agent}" in mermaid.to_markdown()) |
| 42 | + |
| 43 | + self.assertTrue(f"test1->>test2: step1" in mermaid.to_markdown()) |
| 44 | + self.assertTrue(f"test2->>test3: step2" in mermaid.to_markdown()) |
| 45 | + self.assertTrue(f"test3->>test3: step3" in mermaid.to_markdown()) |
| 46 | + |
| 47 | + def test_markdown_flowchartTD(self): |
| 48 | + mermaid = Mermaid(self.workflow_yaml, "flowchart", "TD") |
| 49 | + self.assertTrue(mermaid.to_markdown().startswith("flowchart TD")) |
| 50 | + |
| 51 | + self.assertTrue(f"test1-- step1 -->test2" in mermaid.to_markdown()) |
| 52 | + self.assertTrue(f"test2-- step2 -->test3" in mermaid.to_markdown()) |
| 53 | + self.assertTrue(f"test3-- step3 -->test3" in mermaid.to_markdown()) |
| 54 | + |
| 55 | + def test_markdown_flowchartRL(self): |
| 56 | + mermaid = Mermaid(self.workflow_yaml, "flowchart", "RL") |
| 57 | + self.assertTrue(mermaid.to_markdown().startswith("flowchart RL")) |
| 58 | + |
| 59 | + self.assertTrue(f"test1-- step1 -->test2" in mermaid.to_markdown()) |
| 60 | + self.assertTrue(f"test2-- step2 -->test3" in mermaid.to_markdown()) |
| 61 | + self.assertTrue(f"test3-- step3 -->test3" in mermaid.to_markdown()) |
| 62 | + |
| 63 | +class TestWorkdlow_to_mermaid(TestCase): |
| 64 | + def setUp(self): |
| 65 | + self.workflow_yaml = parse_yaml(os.path.join(os.path.dirname(__file__),"../yamls/workflows/simple_workflow.yaml")) |
| 66 | + self.workflow = Workflow({}, self.workflow_yaml) |
| 67 | + |
| 68 | + def tearDown(self): |
| 69 | + self.workflow_yaml = None |
| 70 | + self.workflow = None |
| 71 | + |
| 72 | + def test_markdown_sequenceDiagram(self): |
| 73 | + markdown = self.workflow.to_mermaid("sequenceDiagram") |
| 74 | + self.assertTrue(markdown.startswith("sequenceDiagram")) |
| 75 | + |
| 76 | + self.assertTrue(f"test1->>test2: step1" in markdown) |
| 77 | + self.assertTrue(f"test2->>test3: step2" in markdown) |
| 78 | + self.assertTrue(f"test3->>test3: step3" in markdown) |
| 79 | + |
| 80 | + def test_markdown_flowchartTD(self): |
| 81 | + markdown = self.workflow.to_mermaid("flowchart", "TD") |
| 82 | + self.assertTrue(markdown.startswith("flowchart TD")) |
| 83 | + |
| 84 | + self.assertTrue(f"test1-- step1 -->test2" in markdown) |
| 85 | + self.assertTrue(f"test2-- step2 -->test3" in markdown) |
| 86 | + self.assertTrue(f"test3-- step3 -->test3" in markdown) |
| 87 | + |
| 88 | + def test_markdown_flowchartRL(self): |
| 89 | + markdown = self.workflow.to_mermaid("flowchart", "RL") |
| 90 | + self.assertTrue(markdown.startswith("flowchart RL")) |
| 91 | + |
| 92 | + self.assertTrue(f"test1-- step1 -->test2" in markdown) |
| 93 | + self.assertTrue(f"test2-- step2 -->test3" in markdown) |
| 94 | + self.assertTrue(f"test3-- step3 -->test3" in markdown) |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + unittest.main() |
0 commit comments