Coverage for tests / unit / parsers / test_generic.py: 100%

90 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-16 21:32 +0000

1from graphable.graph import Graph, Graphable 

2from graphable.parsers.json import load_graph_json 

3from graphable.views.csv import create_topology_csv 

4from graphable.views.graphml import create_topology_graphml 

5from graphable.views.json import create_topology_json 

6from graphable.views.toml import create_topology_toml 

7from graphable.views.yaml import create_topology_yaml 

8 

9 

10def test_graph_roundtrip_json(): 

11 a = Graphable("A") 

12 a.add_tag("t1") 

13 b = Graphable("B") 

14 g = Graph() 

15 g.add_edge(a, b) 

16 

17 json_data = create_topology_json(g) 

18 g_parsed = Graph.from_json(json_data) 

19 

20 assert g == g_parsed 

21 

22 

23def test_graph_roundtrip_yaml(): 

24 a = Graphable("A") 

25 a.add_tag("t1") 

26 b = Graphable("B") 

27 g = Graph() 

28 g.add_edge(a, b) 

29 

30 yaml_data = create_topology_yaml(g) 

31 g_parsed = Graph.from_yaml(yaml_data) 

32 

33 assert g == g_parsed 

34 

35 

36def test_graph_roundtrip_toml(): 

37 a = Graphable("A") 

38 a.add_tag("t1") 

39 b = Graphable("B") 

40 g = Graph() 

41 g.add_edge(a, b) 

42 

43 toml_data = create_topology_toml(g) 

44 g_parsed = Graph.from_toml(toml_data) 

45 

46 assert g == g_parsed 

47 

48 

49def test_graph_roundtrip_csv(): 

50 # CSV only preserves structure, not tags 

51 a = Graphable("A") 

52 b = Graphable("B") 

53 g = Graph() 

54 g.add_edge(a, b) 

55 

56 csv_data = create_topology_csv(g) 

57 g_parsed = Graph.from_csv(csv_data) 

58 

59 # We can't use g == g_parsed because g has tags (empty) and parsed might differ 

60 # if implementation details change, but actually CSV graph is simpler. 

61 # Let's ensure they are equal in structure. 

62 assert len(g) == len(g_parsed) 

63 assert "A" in g_parsed 

64 assert "B" in g_parsed 

65 assert g_parsed["B"] in g_parsed["A"].dependents 

66 

67 

68def test_graph_roundtrip_graphml(): 

69 a = Graphable("A") 

70 a.add_tag("t1") 

71 b = Graphable("B") 

72 g = Graph() 

73 g.add_edge(a, b) 

74 

75 graphml_data = create_topology_graphml(g) 

76 g_parsed = Graph.from_graphml(graphml_data) 

77 

78 assert g == g_parsed 

79 

80 

81def test_graph_parse_generic(): 

82 data = '{"nodes": [{"id": "A"}], "edges": []}' 

83 g = Graph.parse(load_graph_json, data) 

84 assert len(g) == 1 

85 assert "A" in g 

86 

87 

88def test_graph_from_json(): 

89 data = '{"nodes": [{"id": "A"}], "edges": []}' 

90 g = Graph.from_json(data) 

91 assert len(g) == 1 

92 assert "A" in g 

93 

94 

95def test_graph_from_yaml(): 

96 data = "nodes: [{id: A}]" 

97 g = Graph.from_yaml(data) 

98 assert len(g) == 1 

99 assert "A" in g 

100 

101 

102def test_graph_from_toml(): 

103 data = """ 

104[[nodes]] 

105id = "A" 

106""" 

107 g = Graph.from_toml(data) 

108 assert len(g) == 1 

109 assert "A" in g 

110 

111 

112def test_graph_from_csv(): 

113 data = "A,B" 

114 g = Graph.from_csv(data) 

115 assert len(g) == 2 

116 assert "A" in g 

117 assert "B" in g 

118 

119 

120def test_graph_from_graphml(): 

121 data = '<graphml xmlns="http://graphml.graphdrawing.org/xmlns"><graph id="G"><node id="A"/></graph></graphml>' 

122 g = Graph.from_graphml(data) 

123 assert len(g) == 1 

124 assert "A" in g 

125 

126 

127def test_graph_from_json_invalid_ref_type(): 

128 data = '{"nodes": [{"id": "A"}], "edges": []}' 

129 

130 # Passing int as reference_type for "A" should fail and fallback to str 

131 g = Graph.from_json(data, reference_type=int) 

132 assert len(g) == 1 

133 assert g["A"].reference == "A"