| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from types import SimpleNamespace |
| 18 |
import typing as t |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
from cylc.flow.scripts.graph import ( |
| 23 |
Edge, |
| 24 |
Node, |
| 25 |
format_cylc_reference, |
| 26 |
format_graphviz, |
| 27 |
get_nodes_and_edges, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def example_graph(): |
| 33 |
"""Example workflow graph with inter-cycle dependencies.""" |
| 34 |
nodes: t.List[Node] = [ |
| 35 |
'1/a', |
| 36 |
'1/b', |
| 37 |
'1/c', |
| 38 |
'2/a', |
| 39 |
'2/b', |
| 40 |
'2/c', |
| 41 |
] |
| 42 |
edges: t.List[Edge] = [ |
| 43 |
('1/a', '1/b'), |
| 44 |
('1/b', '1/c'), |
| 45 |
('2/a', '2/b'), |
| 46 |
('2/b', '2/c'), |
| 47 |
('1/b', '2/b'), |
| 48 |
] |
| 49 |
return nodes, edges |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture |
| 53 |
def example_namespace_graph(): |
| 54 |
"""Example namespace graph with inheritance.""" |
| 55 |
nodes: t.List[Node] = [ |
| 56 |
'A', |
| 57 |
'a1', |
| 58 |
'a2', |
| 59 |
'B', |
| 60 |
'B1', |
| 61 |
'b11', |
| 62 |
'B2', |
| 63 |
'b22', |
| 64 |
] |
| 65 |
edges: t.List[Edge] = [ |
| 66 |
('A', 'a1'), |
| 67 |
('A', 'a2'), |
| 68 |
('B', 'B1'), |
| 69 |
('B', 'B2'), |
| 70 |
('B1', 'b11'), |
| 71 |
('B2', 'b22'), |
| 72 |
] |
| 73 |
return nodes, edges |
| 74 |
|
| 75 |
|
| 76 |
@pytest.fixture |
| 77 |
def null_config(monkeypatch): |
| 78 |
"""Patch the config loader to return a workflow with no nodes or edges.""" |
| 79 |
def _get_graph_raw(*args, **kwargs): |
| 80 |
return None |
| 81 |
|
| 82 |
def _get_parents_lists(*args, **kwargs): |
| 83 |
return {} |
| 84 |
|
| 85 |
config = SimpleNamespace( |
| 86 |
get_graph_raw=_get_graph_raw, |
| 87 |
get_parent_lists=_get_parents_lists, |
| 88 |
) |
| 89 |
|
| 90 |
monkeypatch.setattr( |
| 91 |
'cylc.flow.scripts.graph.get_config', |
| 92 |
lambda x, y, z: config |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def test_format_graphviz_normal(example_graph): |
| 97 |
"""Test graphviz output for default options. |
| 98 |
|
| 99 |
Tests both orientations (--transpose). |
| 100 |
""" |
| 101 |
nodes, edges = example_graph |
| 102 |
|
| 103 |
# format the graph in regular orientation |
| 104 |
opts = SimpleNamespace(transpose=False, namespaces=False, cycles=False) |
| 105 |
lines = format_graphviz(opts, nodes, edges) |
| 106 |
assert lines == [ |
| 107 |
'digraph {', |
| 108 |
' graph [fontname="sans" fontsize="25"]', |
| 109 |
' node [fontname="sans"]', |
| 110 |
'', |
| 111 |
' "1/a" [label="a\\n1"]', |
| 112 |
' "1/b" [label="b\\n1"]', |
| 113 |
' "1/c" [label="c\\n1"]', |
| 114 |
'', |
| 115 |
' "2/a" [label="a\\n2"]', |
| 116 |
' "2/b" [label="b\\n2"]', |
| 117 |
' "2/c" [label="c\\n2"]', |
| 118 |
'', |
| 119 |
' "1/a" -> "1/b"', |
| 120 |
' "1/b" -> "1/c"', |
| 121 |
' "2/a" -> "2/b"', |
| 122 |
' "2/b" -> "2/c"', |
| 123 |
' "1/b" -> "2/b"', |
| 124 |
'}', |
| 125 |
] |
| 126 |
|
| 127 |
# format the graph in transposed orientation |
| 128 |
opts = SimpleNamespace(transpose=True, namespaces=False, cycles=False) |
| 129 |
transposed_lines = format_graphviz(opts, nodes, edges) |
| 130 |
|
| 131 |
# the transposed graph should be the same except for one line... |
| 132 |
assert [ |
| 133 |
line |
| 134 |
for line in transposed_lines |
| 135 |
if line not in lines |
| 136 |
] == [ |
| 137 |
# ...the one which sets the orientation |
| 138 |
' rankdir="LR"', |
| 139 |
] |
| 140 |
|
| 141 |
|
| 142 |
def test_format_graphviz_namespace(example_namespace_graph): |
| 143 |
"""Test graphviz output for a namespace graph. |
| 144 |
|
| 145 |
Tests both orientations (--transpose). |
| 146 |
""" |
| 147 |
nodes, edges = example_namespace_graph |
| 148 |
|
| 149 |
# format the graph in regular orientation |
| 150 |
opts = SimpleNamespace(transpose=False, namespaces=True, cycles=False) |
| 151 |
lines = format_graphviz(opts, nodes, edges) |
| 152 |
assert lines == [ |
| 153 |
'digraph {', |
| 154 |
' graph [fontname="sans" fontsize="25"]', |
| 155 |
' node [fontname="sans"]', |
| 156 |
' node [shape="rect"]', |
| 157 |
'', |
| 158 |
' "A"', |
| 159 |
' "a1"', |
| 160 |
' "a2"', |
| 161 |
' "B"', |
| 162 |
' "B1"', |
| 163 |
' "b11"', |
| 164 |
' "B2"', |
| 165 |
' "b22"', |
| 166 |
'', |
| 167 |
' "A" -> "a1"', |
| 168 |
' "A" -> "a2"', |
| 169 |
' "B" -> "B1"', |
| 170 |
' "B" -> "B2"', |
| 171 |
' "B1" -> "b11"', |
| 172 |
' "B2" -> "b22"', |
| 173 |
'}', |
| 174 |
] |
| 175 |
|
| 176 |
# format the graph in transposed orientation |
| 177 |
opts = SimpleNamespace(transpose=True, namespaces=True, cycles=False) |
| 178 |
transposed_lines = format_graphviz(opts, nodes, edges) |
| 179 |
|
| 180 |
# the transposed graph should be the same except for one line... |
| 181 |
assert [ |
| 182 |
line |
| 183 |
for line in transposed_lines |
| 184 |
if line not in lines |
| 185 |
] == [ |
| 186 |
# ...the one which sets the orientation |
| 187 |
' rankdir="LR"', |
| 188 |
] |
| 189 |
|
| 190 |
|
| 191 |
def test_format_graphviz_cycles(example_graph): |
| 192 |
"""Test graphviz format for the --cycles option (group by cycle). |
| 193 |
|
| 194 |
Note: There is no difference between iso8601 and integer cycle points here, |
| 195 |
the graph logic is cycle point format agnostic. Sorting is not performed |
| 196 |
in this funtion. |
| 197 |
""" |
| 198 |
nodes, edges = example_graph |
| 199 |
|
| 200 |
opts = SimpleNamespace(transpose=False, namespaces=False, cycles=True) |
| 201 |
lines = format_graphviz(opts, nodes, edges) |
| 202 |
assert lines == [ |
| 203 |
'digraph {', |
| 204 |
' graph [fontname="sans" fontsize="25"]', |
| 205 |
' node [fontname="sans"]', |
| 206 |
'', |
| 207 |
' subgraph "cluster_1" { ', |
| 208 |
' label="1"', |
| 209 |
' style="dashed"', |
| 210 |
' "1/a" [label="a\\n1"]', |
| 211 |
' "1/b" [label="b\\n1"]', |
| 212 |
' "1/c" [label="c\\n1"]', |
| 213 |
' }', |
| 214 |
'', |
| 215 |
' subgraph "cluster_2" { ', |
| 216 |
' label="2"', |
| 217 |
' style="dashed"', |
| 218 |
' "2/a" [label="a\\n2"]', |
| 219 |
' "2/b" [label="b\\n2"]', |
| 220 |
' "2/c" [label="c\\n2"]', |
| 221 |
' }', |
| 222 |
'', |
| 223 |
' "1/a" -> "1/b"', |
| 224 |
' "1/b" -> "1/c"', |
| 225 |
' "2/a" -> "2/b"', |
| 226 |
' "2/b" -> "2/c"', |
| 227 |
' "1/b" -> "2/b"', |
| 228 |
'}', |
| 229 |
] |
| 230 |
|
| 231 |
|
| 232 |
def test_format_cylc_reference_normal(example_graph): |
| 233 |
"""Test Cylc "reference" format (used by the test battery). |
| 234 |
|
| 235 |
Note: There is no difference between iso8601 and integer cycle points here, |
| 236 |
the graph logic is cycle point format agnostic. Sorting is not performed |
| 237 |
in this funtion. |
| 238 |
|
| 239 |
Note: There is no transpose mode for reference graphs. |
| 240 |
""" |
| 241 |
nodes, edges = example_graph |
| 242 |
|
| 243 |
opts = SimpleNamespace(namespaces=False) |
| 244 |
lines = format_cylc_reference(opts, nodes, edges) |
| 245 |
assert lines == [ |
| 246 |
'edge "1/a" "1/b"', |
| 247 |
'edge "1/b" "1/c"', |
| 248 |
'edge "2/a" "2/b"', |
| 249 |
'edge "2/b" "2/c"', |
| 250 |
'edge "1/b" "2/b"', |
| 251 |
'graph', |
| 252 |
'node "1/a" "a\\n1"', |
| 253 |
'node "1/b" "b\\n1"', |
| 254 |
'node "1/c" "c\\n1"', |
| 255 |
'node "2/a" "a\\n2"', |
| 256 |
'node "2/b" "b\\n2"', |
| 257 |
'node "2/c" "c\\n2"', |
| 258 |
'stop', |
| 259 |
] |
| 260 |
|
| 261 |
|
| 262 |
def test_format_cylc_reference_namespace(example_namespace_graph): |
| 263 |
"""Test Cylc "reference" format for namespace graphs. |
| 264 |
|
| 265 |
Note: There is no transpose mode for reference graphs. |
| 266 |
""" |
| 267 |
nodes, edges = example_namespace_graph |
| 268 |
|
| 269 |
opts = SimpleNamespace(namespaces=True) |
| 270 |
lines = format_cylc_reference(opts, nodes, edges) |
| 271 |
assert lines == [ |
| 272 |
'edge "A" "a1"', |
| 273 |
'edge "A" "a2"', |
| 274 |
'edge "B" "B1"', |
| 275 |
'edge "B" "B2"', |
| 276 |
'edge "B1" "b11"', |
| 277 |
'edge "B2" "b22"', |
| 278 |
'graph', |
| 279 |
'node "A" "A"', |
| 280 |
'node "a1" "a1"', |
| 281 |
'node "a2" "a2"', |
| 282 |
'node "B" "B"', |
| 283 |
'node "B1" "B1"', |
| 284 |
'node "b11" "b11"', |
| 285 |
'node "B2" "B2"', |
| 286 |
'node "b22" "b22"', |
| 287 |
'stop', |
| 288 |
] |
| 289 |
|
| 290 |
|
| 291 |
def test_null(null_config): |
| 292 |
"""Ensure that an empty graph is handled elegantly.""" |
| 293 |
opts = SimpleNamespace( |
| 294 |
namespaces=False, |
| 295 |
grouping=False, |
| 296 |
show_suicide=False |
| 297 |
) |
| 298 |
assert get_nodes_and_edges(opts, None, 1, 2, '') == ([], []) |
| 299 |
|
| 300 |
opts = SimpleNamespace( |
| 301 |
namespaces=True, |
| 302 |
grouping=False, |
| 303 |
show_suicide=False |
| 304 |
) |
| 305 |
assert get_nodes_and_edges(opts, None, 1, 2, '') == ([], []) |