Coverage for tests / unit / views / test_graphml.py: 100%

36 statements  

« 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 

3 

4from pytest import fixture 

5 

6from graphable.graph import Graph 

7from graphable.graphable import Graphable 

8from graphable.views.graphml import ( 

9 GraphmlStylingConfig, 

10 create_topology_graphml, 

11 export_topology_graphml, 

12) 

13 

14 

15class TestGraphML: 

16 @fixture 

17 def graph_fixture(self): 

18 a = Graphable("A") 

19 b = Graphable("B") 

20 a.add_tag("source") 

21 g = Graph() 

22 g.add_edge(a, b) 

23 return g, a, b 

24 

25 def test_create_topology_graphml_default(self, graph_fixture): 

26 g, a, b = graph_fixture 

27 graphml = create_topology_graphml(g) 

28 

29 assert 'xmlns="http://graphml.graphdrawing.org/xmlns"' in graphml 

30 assert '<node id="A">' in graphml 

31 assert '<node id="B"/>' in graphml 

32 assert '<edge id="e_A_B" source="A" target="B"/>' in graphml 

33 assert '<data key="tags">source</data>' in graphml 

34 

35 def test_create_topology_graphml_custom_config(self, graph_fixture): 

36 g, a, b = graph_fixture 

37 config = GraphmlStylingConfig(node_ref_fnc=lambda n: f"id_{n.reference}") 

38 graphml = create_topology_graphml(g, config) 

39 

40 assert '<node id="id_A">' in graphml 

41 assert '<edge id="e_id_A_id_B" source="id_A" target="id_B"/>' in graphml 

42 

43 def test_export_topology_graphml(self, graph_fixture): 

44 g, _, _ = graph_fixture 

45 output_path = Path("output.graphml") 

46 

47 with patch("builtins.open", mock_open()) as mock_file: 

48 export_topology_graphml(g, output_path) 

49 

50 mock_file.assert_called_with(output_path, "w+") 

51 mock_file().write.assert_called()