Coverage for tests / unit / views / test_yaml.py: 100%
43 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-16 21:32 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-16 21:32 +0000
1from pathlib import Path
2from unittest.mock import mock_open, patch
4from pytest import fixture
5from yaml import safe_load
7from graphable.graph import Graph
8from graphable.graphable import Graphable
9from graphable.views.yaml import (
10 YamlStylingConfig,
11 create_topology_yaml,
12 export_topology_yaml,
13)
16class TestYAML:
17 @fixture
18 def graph_fixture(self):
19 a = Graphable("A")
20 b = Graphable("B")
21 a.add_tag("tag1")
22 g = Graph()
23 g.add_edge(a, b)
24 return g, a, b
26 def test_create_topology_yaml_default(self, graph_fixture):
27 g, a, b = graph_fixture
28 yaml_str = create_topology_yaml(g)
29 data = safe_load(yaml_str)
31 assert "nodes" in data
32 assert "edges" in data
33 assert len(data["nodes"]) == 2
34 assert len(data["edges"]) == 1
36 node_a = next(n for n in data["nodes"] if n["id"] == "A")
37 assert node_a["tags"] == ["tag1"]
39 edge = data["edges"][0]
40 assert edge["source"] == "A"
41 assert edge["target"] == "B"
43 def test_create_topology_yaml_custom_data(self, graph_fixture):
44 g, a, b = graph_fixture
45 config = YamlStylingConfig(node_data_fnc=lambda n: {"extra": True})
46 yaml_str = create_topology_yaml(g, config=config)
47 data = safe_load(yaml_str)
49 for node in data["nodes"]:
50 assert node["extra"] is True
52 def test_export_topology_yaml(self, graph_fixture):
53 g, _, _ = graph_fixture
54 output_path = Path("output.yaml")
56 with patch("builtins.open", mock_open()) as mock_file:
57 export_topology_yaml(g, output_path)
59 mock_file.assert_called_with(output_path, "w+")
60 mock_file().write.assert_called()