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

56 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.texttree import ( 

9 TextTreeStylingConfig, 

10 create_topology_tree_txt, 

11 export_topology_tree_txt, 

12) 

13 

14 

15class TestTextTree: 

16 @fixture 

17 def graph_fixture(self): 

18 a = Graphable("A") 

19 b = Graphable("B") 

20 c = Graphable("C") 

21 g = Graph() 

22 # C depends on B, B depends on A 

23 # A -> B -> C 

24 g.add_edge(a, b) 

25 g.add_edge(b, c) 

26 return g, a, b, c 

27 

28 def test_create_topology_tree_txt_simple(self, graph_fixture): 

29 g, a, b, c = graph_fixture 

30 # C is the sink 

31 tree = create_topology_tree_txt(g) 

32 

33 # Expected: 

34 # C 

35 # └─ B 

36 # └─ A 

37 

38 assert "C" in tree 

39 assert "└─ B" in tree 

40 assert " └─ A" in tree 

41 

42 def test_create_topology_tree_txt_custom_config(self, graph_fixture): 

43 g, a, b, c = graph_fixture 

44 config = TextTreeStylingConfig( 

45 initial_indent=" ", node_text_fnc=lambda n: f"[{n.reference}]" 

46 ) 

47 tree = create_topology_tree_txt(g, config) 

48 

49 assert " [C]" in tree 

50 assert " └─ [B]" in tree 

51 assert " └─ [A]" in tree 

52 

53 def test_create_topology_tree_txt_circular_or_revisited(self): 

54 # E -> A, A -> B, A -> C, B -> D, C -> D 

55 a = Graphable("A") 

56 b = Graphable("B") 

57 c = Graphable("C") 

58 d = Graphable("D") 

59 e = Graphable("E") 

60 

61 g = Graph() 

62 g.add_edge(e, a) 

63 g.add_edge(a, b) 

64 g.add_edge(a, c) 

65 g.add_edge(b, d) 

66 g.add_edge(c, d) 

67 

68 tree = create_topology_tree_txt(g) 

69 

70 assert "D" in tree 

71 assert "A" in tree 

72 assert "B" in tree 

73 assert "C" in tree 

74 assert "E" in tree 

75 assert "└─ E" in tree 

76 assert "(see above)" in tree 

77 

78 def test_export_topology_tree_txt(self, graph_fixture): 

79 g, _, _, _ = graph_fixture 

80 output_path = Path("output.txt") 

81 

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

83 export_topology_tree_txt(g, output_path) 

84 

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

86 mock_file().write.assert_called()