1import unittest
2
3from . import test_helpers as th
4
5from .subcomponent import (
6 subcomponent_out,
7 subcomponent_in
8)
9
10from .lib import (
11 pick_keys,
12 directed_graph,
13 empty_directed_graph
14)
15
16
17if __name__ == "__main__":
18 unittest.main()
19
20
21# Making sure vertex attrs are preserved.
22vertex_props_dict = {
23 "Root1": {"a": 1, "b": 1},
24 "B": {"b": 2},
25 "X": {"x": 3}
26}
27
28
29def make_test_graph():
30 edges = [
31 ("Root1", "A"),
32 ("A", "B"),
33 ("A", "C"),
34 ("B", "D"),
35 ("B", "E"),
36 ("Root2", "B"),
37 ("Root3", "C"),
38 ]
39
40 detached_vertices = ["X"]
41
42 vertex_props = vertex_props_dict.items()
43
44 return directed_graph(edges, detached_vertices, vertex_props)
45
46
47class CustomAssertions:
48 def assertResultKeys(self, result):
49 self.assertListEqual(
50 list(result.keys()),
51 ["main", "rest"]
52 )
53
54 return result
55
56
57class Test(
58 unittest.TestCase,
59 CustomAssertions,
60 th.CustomAssertions
61):
62
63 def test_empty_paths(self):
64 def test(func):
65 input_graph = make_test_graph()
66
67 result = self.assertResultKeys(
68 func([], input_graph)
69 )
70
71 self.assertGraphEqual(
72 result["main"],
73 empty_directed_graph()
74 )
75
76 self.assertGraphEqual(
77 result["rest"],
78 input_graph
79 )
80
81 test(subcomponent_out)
82 test(subcomponent_in)
83
84 def test_empty_graph(self):
85 def test(func):
86 empty_graph = empty_directed_graph()
87
88 def test_empty(paths):
89 result = self.assertResultKeys(
90 func(paths, empty_graph)
91 )
92
93 self.assertGraphEqual(
94 result["main"],
95 empty_graph
96 )
97
98 self.assertGraphEqual(
99 result["rest"],
100 empty_graph
101 )
102
103 test_empty([])
104 test_empty(["B"])
105
106 test(subcomponent_out)
107 test(subcomponent_in)
108
109 def test_subcomponent_out(self):
110 result = self.assertResultKeys(
111 subcomponent_out(["B"], make_test_graph())
112 )
113
114 self.assertGraphEqual(
115 result["main"],
116 directed_graph(
117 [
118 ("B", "D"),
119 ("B", "E")
120 ],
121 None,
122 pick_keys(["B"], vertex_props_dict).items()
123 )
124 )
125
126 self.assertGraphEqual(
127 result["rest"],
128 directed_graph(
129 [
130 ("Root1", "A"),
131 ("A", "C"),
132 ("Root3", "C")
133 ],
134 ["Root2", "X"],
135 pick_keys(["Root1", "X"], vertex_props_dict).items()
136 )
137 )
138
139 def test_subcomponent_out_multi(self):
140 result = self.assertResultKeys(
141 subcomponent_out(["B", "Root3"], make_test_graph())
142 )
143
144 self.assertGraphEqual(
145 result["main"],
146 directed_graph(
147 [
148 ("B", "D"),
149 ("B", "E"),
150 ("Root3", "C")
151 ],
152 None,
153 pick_keys(["B"], vertex_props_dict).items()
154 )
155 )
156
157 self.assertGraphEqual(
158 result["rest"],
159 directed_graph(
160 [("Root1", "A")],
161 ["Root2", "X"],
162 pick_keys(["Root1", "X"], vertex_props_dict).items()
163 )
164 )
165
166 def test_subcomponent_in(self):
167 result = self.assertResultKeys(
168 subcomponent_in(["B"], make_test_graph())
169 )
170
171 self.assertGraphEqual(
172 result["main"],
173 directed_graph(
174 [
175 ("Root1", "A"),
176 ("A", "B"),
177 ("Root2", "B")
178 ],
179 None,
180 pick_keys(["Root1", "B"], vertex_props_dict).items()
181 )
182 )
183
184 self.assertGraphEqual(
185 result["rest"],
186 directed_graph(
187 [("Root3", "C")],
188 ["D", "E", "X"],
189 pick_keys(["X"], vertex_props_dict).items()
190 )
191 )
192
193 def test_subcomponent_in_multi(self):
194 result = self.assertResultKeys(
195 subcomponent_in(["B", "Root3"], make_test_graph())
196 )
197
198 self.assertGraphEqual(
199 result["main"],
200 directed_graph(
201 [
202 ("Root1", "A"),
203 ("A", "B"),
204 ("Root2", "B"),
205 ],
206 ["Root3"],
207 pick_keys(["Root1", "B"], vertex_props_dict).items()
208
209 )
210 )
211
212 self.assertGraphEqual(
213 result["rest"],
214 directed_graph(
215 [],
216 ["C", "D", "E", "X"],
217 pick_keys(["X"], vertex_props_dict).items()
218 )
219 )