|
| 1 | +import json |
| 2 | +import os |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | +import networkx as nx |
| 6 | +from pydantic import ValidationError |
| 7 | +from python_workflow_definition.plot import plot |
| 8 | +from python_workflow_definition.shared import ( |
| 9 | + NODES_LABEL, |
| 10 | + EDGES_LABEL, |
| 11 | + SOURCE_LABEL, |
| 12 | + TARGET_LABEL, |
| 13 | + SOURCE_PORT_LABEL, |
| 14 | + TARGET_PORT_LABEL, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TestPlot(unittest.TestCase): |
| 19 | + def setUp(self): |
| 20 | + self.test_file = "test_workflow.json" |
| 21 | + self.workflow_data = { |
| 22 | + "version": "0.0.1", |
| 23 | + NODES_LABEL: [ |
| 24 | + {"id": 1, "name": "Node 1", "type": "function", "value": "a.b"}, |
| 25 | + {"id": 2, "name": "Node 2", "type": "function", "value": "c.d"}, |
| 26 | + {"id": 3, "name": "Node 3", "type": "function", "value": "e.f"}, |
| 27 | + ], |
| 28 | + EDGES_LABEL: [ |
| 29 | + { |
| 30 | + SOURCE_LABEL: 1, |
| 31 | + TARGET_LABEL: 2, |
| 32 | + SOURCE_PORT_LABEL: "out1", |
| 33 | + TARGET_PORT_LABEL: "in1", |
| 34 | + }, |
| 35 | + { |
| 36 | + SOURCE_LABEL: 2, |
| 37 | + TARGET_LABEL: 3, |
| 38 | + SOURCE_PORT_LABEL: "out2", |
| 39 | + TARGET_PORT_LABEL: "in2", |
| 40 | + }, |
| 41 | + { |
| 42 | + SOURCE_LABEL: 1, |
| 43 | + TARGET_LABEL: 3, |
| 44 | + SOURCE_PORT_LABEL: None, |
| 45 | + TARGET_PORT_LABEL: "in3", |
| 46 | + }, |
| 47 | + ], |
| 48 | + } |
| 49 | + with open(self.test_file, "w") as f: |
| 50 | + json.dump(self.workflow_data, f) |
| 51 | + |
| 52 | + def tearDown(self): |
| 53 | + if os.path.exists(self.test_file): |
| 54 | + os.remove(self.test_file) |
| 55 | + |
| 56 | + @patch("python_workflow_definition.plot.display") |
| 57 | + @patch("python_workflow_definition.plot.SVG") |
| 58 | + @patch("networkx.nx_agraph.to_agraph") |
| 59 | + def test_plot(self, mock_to_agraph, mock_svg, mock_display): |
| 60 | + mock_agraph = MagicMock() |
| 61 | + mock_to_agraph.return_value = mock_agraph |
| 62 | + mock_agraph.draw.return_value = "<svg></svg>" |
| 63 | + |
| 64 | + plot(self.test_file) |
| 65 | + |
| 66 | + self.assertEqual(1, mock_to_agraph.call_count) |
| 67 | + graph = mock_to_agraph.call_args[0][0] |
| 68 | + self.assertIsInstance(graph, nx.DiGraph) |
| 69 | + |
| 70 | + self.assertCountEqual(["1", "2", "3"], graph.nodes) |
| 71 | + self.assertEqual("a.b", graph.nodes["1"]["name"]) |
| 72 | + self.assertEqual("c.d", graph.nodes["2"]["name"]) |
| 73 | + self.assertEqual("e.f", graph.nodes["3"]["name"]) |
| 74 | + |
| 75 | + self.assertCountEqual([("1", "2"), ("2", "3"), ("1", "3")], graph.edges) |
| 76 | + |
| 77 | + edge_n1_n2_data = graph.get_edge_data("1", "2") |
| 78 | + self.assertIn("label", edge_n1_n2_data) |
| 79 | + self.assertEqual("in1=result[out1]", edge_n1_n2_data["label"]) |
| 80 | + |
| 81 | + edge_n1_n3_data = graph.get_edge_data("1", "3") |
| 82 | + self.assertIn("label", edge_n1_n3_data) |
| 83 | + self.assertEqual("in3", edge_n1_n3_data["label"]) |
| 84 | + |
| 85 | + mock_svg.assert_called_once_with("<svg></svg>") |
| 86 | + mock_display.assert_called_once() |
| 87 | + |
| 88 | + @patch("python_workflow_definition.plot.display") |
| 89 | + @patch("python_workflow_definition.plot.SVG") |
| 90 | + @patch("networkx.nx_agraph.to_agraph") |
| 91 | + def test_plot_multiple_edges_same_source(self, mock_to_agraph, mock_svg, mock_display): |
| 92 | + self.workflow_data[EDGES_LABEL].append( |
| 93 | + { |
| 94 | + SOURCE_LABEL: 1, |
| 95 | + TARGET_LABEL: 2, |
| 96 | + SOURCE_PORT_LABEL: "out2", |
| 97 | + TARGET_PORT_LABEL: "in2", |
| 98 | + } |
| 99 | + ) |
| 100 | + with open(self.test_file, "w") as f: |
| 101 | + json.dump(self.workflow_data, f) |
| 102 | + |
| 103 | + mock_agraph = MagicMock() |
| 104 | + mock_to_agraph.return_value = mock_agraph |
| 105 | + mock_agraph.draw.return_value = "<svg></svg>" |
| 106 | + |
| 107 | + plot(self.test_file) |
| 108 | + |
| 109 | + self.assertEqual(1, mock_to_agraph.call_count) |
| 110 | + graph = mock_to_agraph.call_args[0][0] |
| 111 | + self.assertIsInstance(graph, nx.DiGraph) |
| 112 | + |
| 113 | + # This assertion is correct due to the logic in `plot.py`. The function |
| 114 | + # groups all connections between a single source node and a single target |
| 115 | + # node. If it finds more than one connection (e.g., from different |
| 116 | + # source ports to different target ports), it creates a single, |
| 117 | + # unlabeled edge in the graph to represent the multiple connections. |
| 118 | + edge_n1_n2_data = graph.get_edge_data("1", "2") |
| 119 | + self.assertNotIn("label", edge_n1_n2_data) |
| 120 | + |
| 121 | + def test_plot_file_not_found(self): |
| 122 | + with self.assertRaises(FileNotFoundError): |
| 123 | + plot("non_existent_file.json") |
| 124 | + |
| 125 | + def test_plot_invalid_json(self): |
| 126 | + with open(self.test_file, "w") as f: |
| 127 | + f.write("{'invalid': 'json'") |
| 128 | + with self.assertRaises(ValidationError): |
| 129 | + plot(self.test_file) |
| 130 | + |
| 131 | + def test_plot_missing_keys(self): |
| 132 | + invalid_data = {"version": "0.0.1", "edges": []} |
| 133 | + with open(self.test_file, "w") as f: |
| 134 | + json.dump(invalid_data, f) |
| 135 | + with self.assertRaises(ValidationError): |
| 136 | + plot(self.test_file) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + unittest.main() |
0 commit comments