Coverage for tests / unit / views / test_tikz.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.tikz import ( 

9 TikzStylingConfig, 

10 create_topology_tikz, 

11 export_topology_tikz, 

12) 

13 

14 

15class TestTikZ: 

16 @fixture 

17 def graph_fixture(self): 

18 a = Graphable("A") 

19 b = Graphable("B") 

20 g = Graph() 

21 g.add_edge(a, b) 

22 return g, a, b 

23 

24 def test_create_topology_tikz_graphs_lib(self, graph_fixture): 

25 g, a, b = graph_fixture 

26 tikz = create_topology_tikz(g) 

27 

28 assert r"\begin{tikzpicture}" in tikz 

29 assert r"\usetikzlibrary{graphs}" in tikz 

30 assert f"node_{id(a)} -> node_{id(b)}" in tikz 

31 assert r"\end{tikzpicture}" in tikz 

32 

33 def test_create_topology_tikz_standard(self, graph_fixture): 

34 g, a, b = graph_fixture 

35 config = TikzStylingConfig(use_graphs_lib=False) 

36 tikz = create_topology_tikz(g, config) 

37 

38 assert r"\node" in tikz 

39 assert r"\draw" in tikz 

40 assert "node_" in tikz 

41 assert r"\end{tikzpicture}" in tikz 

42 

43 def test_export_topology_tikz(self, graph_fixture): 

44 g, _, _ = graph_fixture 

45 output_path = Path("output.tex") 

46 

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

48 export_topology_tikz(g, output_path) 

49 

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

51 mock_file().write.assert_called()