Total Complexity | 52 |
Total Lines | 356 |
Duplicated Lines | 10.39 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like build.tests.integration.test_paths_metadata often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Module to test the KytosGraph in graph.py.""" |
||
2 | |||
3 | # pylint: disable=too-many-public-methods, import-error |
||
4 | from tests.integration.metadata_settings import MetadataSettings |
||
5 | |||
6 | |||
7 | class TestPathsMetadata(MetadataSettings): |
||
8 | """Tests for the graph class. |
||
9 | |||
10 | Tests if the metadata in search paths edges have passing values. |
||
11 | """ |
||
12 | |||
13 | def test_path_constrained_user_user_k1(self): |
||
14 | """Test if there is a constrained path between User - User.""" |
||
15 | self.initializer() |
||
16 | |||
17 | source = "User1" |
||
18 | destination = "User2" |
||
19 | paths = self.graph.constrained_k_shortest_paths( |
||
20 | source, destination, k=1 |
||
21 | ) |
||
22 | assert len(paths) == 1 |
||
23 | |||
24 | for path in paths: |
||
25 | assert path["hops"][0] == source |
||
26 | assert path["hops"][-1] == destination |
||
27 | |||
28 | def test_path_constrained_user_user_k2(self): |
||
29 | """Test if there are two constrained path between User - User.""" |
||
30 | self.initializer() |
||
31 | |||
32 | source = "User1" |
||
33 | destination = "User2" |
||
34 | paths = self.graph.constrained_k_shortest_paths( |
||
35 | source, destination, k=2 |
||
36 | ) |
||
37 | assert len(paths) == 2 |
||
38 | |||
39 | for path in paths: |
||
40 | assert path["hops"][0] == source |
||
41 | assert path["hops"][-1] == destination |
||
42 | |||
43 | def test_path_constrained_user_user_k4(self): |
||
44 | """Test if there are four constrained path between User - User.""" |
||
45 | self.initializer() |
||
46 | |||
47 | source = "User1" |
||
48 | destination = "User2" |
||
49 | paths = self.graph.constrained_k_shortest_paths( |
||
50 | source, destination, k=4 |
||
51 | ) |
||
52 | assert len(paths) == 4 |
||
53 | |||
54 | for path in paths: |
||
55 | assert path["hops"][0] == source |
||
56 | assert path["hops"][-1] == destination |
||
57 | |||
58 | def test_path_constrained_user_switch(self): |
||
59 | """Test if there is a constrained |
||
60 | path between User - Switch.""" |
||
61 | self.initializer() |
||
62 | |||
63 | source = "User1" |
||
64 | destination = "S4" |
||
65 | paths = self.graph.constrained_k_shortest_paths(source, destination) |
||
66 | assert paths |
||
67 | |||
68 | for path in paths: |
||
69 | assert path["hops"][0] == source |
||
70 | assert path["hops"][-1] == destination |
||
71 | |||
72 | def test_path_constrained_switch_switch(self): |
||
73 | """Test if there is a constrained |
||
74 | path between Switch - Switch.""" |
||
75 | self.initializer() |
||
76 | |||
77 | source = "S2" |
||
78 | destination = "S4" |
||
79 | paths = self.graph.constrained_k_shortest_paths(source, destination) |
||
80 | assert paths |
||
81 | |||
82 | for path in paths: |
||
83 | assert path["hops"][0] == source |
||
84 | assert path["hops"][-1] == destination |
||
85 | |||
86 | def test_no_path_constrained_user_user(self): |
||
87 | """Test if there is NOT a constrained |
||
88 | path between User - User.""" |
||
89 | self.initializer() |
||
90 | paths = self.graph.constrained_k_shortest_paths("User1", "User3") |
||
91 | assert not paths |
||
92 | |||
93 | def test_path_constrained_user_user_t1(self): |
||
94 | """Test if there is a constrained path between |
||
95 | User - User using the 2nd topology variant.""" |
||
96 | self.initializer(val=1) |
||
97 | |||
98 | source = "User1" |
||
99 | destination = "User3" |
||
100 | paths = self.graph.constrained_k_shortest_paths(source, destination) |
||
101 | assert paths |
||
102 | |||
103 | for path in paths: |
||
104 | assert path["hops"][0] == source |
||
105 | assert path["hops"][-1] == destination |
||
106 | |||
107 | def test_no_path_constrained_user_user_t1(self): |
||
108 | """Test if there is NOT a constrained path between |
||
109 | User - User using the 2nd topology variant.""" |
||
110 | self.initializer(val=1) |
||
111 | paths = self.graph.constrained_k_shortest_paths("User1", "User2") |
||
112 | assert not paths |
||
113 | |||
114 | def test_no_path_constrained_switch_switch_t1(self): |
||
115 | """Test if there is NOT a constrained path between |
||
116 | Switch - Switch using the 2nd topology variant.""" |
||
117 | self.initializer(val=1) |
||
118 | paths = self.graph.constrained_k_shortest_paths("S1", "S2") |
||
119 | assert not paths |
||
120 | |||
121 | def test_path_constrained_user_user_t2(self): |
||
122 | """Test if there is a constrained path between |
||
123 | User - User using the 3rd topology variant.""" |
||
124 | self.initializer(val=2) |
||
125 | |||
126 | source = "User1" |
||
127 | destination = "User2" |
||
128 | paths = self.graph.constrained_k_shortest_paths(source, destination) |
||
129 | assert paths |
||
130 | |||
131 | for path in paths: |
||
132 | assert path["hops"][0] == source |
||
133 | assert path["hops"][-1] == destination |
||
134 | |||
135 | def test_path_constrained_user_switch_t2(self): |
||
136 | """Test if there is a constrained path between |
||
137 | User - Switch using the 3rd topology variant.""" |
||
138 | self.initializer(val=2) |
||
139 | |||
140 | source = "User1" |
||
141 | destination = "S4" |
||
142 | paths = self.graph.constrained_k_shortest_paths(source, destination) |
||
143 | assert paths |
||
144 | |||
145 | for path in paths: |
||
146 | assert path["hops"][0] == source |
||
147 | assert path["hops"][-1] == destination |
||
148 | paths = self.graph.constrained_k_shortest_paths("User1", "S4") |
||
149 | |||
150 | def test_path_constrained_switch_switch_t2(self): |
||
151 | """Test if there is a constrained path between |
||
152 | two switches using the 3rd topology variant.""" |
||
153 | self.initializer(val=2) |
||
154 | |||
155 | source = "S2" |
||
156 | destination = "S4" |
||
157 | paths = self.graph.constrained_k_shortest_paths(source, destination) |
||
158 | assert paths |
||
159 | |||
160 | for path in paths: |
||
161 | assert path["hops"][0] == source |
||
162 | assert path["hops"][-1] == destination |
||
163 | |||
164 | def test_path_constrained_reliability(self): |
||
165 | """Tests if the edges used in the paths |
||
166 | of the paths set do not have poor reliability |
||
167 | """ |
||
168 | requirements = {"reliability": 3} |
||
169 | |||
170 | self.initializer() |
||
171 | |||
172 | source = "User1" |
||
173 | destination = "User2" |
||
174 | paths = self.graph.constrained_k_shortest_paths( |
||
175 | source, destination, mandatory_metrics=requirements |
||
176 | ) |
||
177 | assert paths |
||
178 | |||
179 | for path in paths: |
||
180 | assert path["hops"][0] == source |
||
181 | assert path["hops"][-1] == destination |
||
182 | |||
183 | def test_cspf_with_multiple_owners(self): |
||
184 | """Tests if the edges with multiple owners""" |
||
185 | |||
186 | owners = ("B", "C") |
||
187 | owners_paths = [] |
||
188 | for owner in owners: |
||
189 | requirements = {"ownership": owner} |
||
190 | |||
191 | self.initializer() |
||
192 | |||
193 | source = "User1" |
||
194 | destination = "User2" |
||
195 | paths = self.graph.constrained_k_shortest_paths( |
||
196 | source, destination, mandatory_metrics=requirements, k=1 |
||
197 | ) |
||
198 | assert paths |
||
199 | assert paths[0]["hops"][0] == source |
||
200 | assert paths[0]["hops"][-1] == destination |
||
201 | assert paths[0]["metrics"] == requirements |
||
202 | owners_paths.append(paths[0]["hops"]) |
||
203 | assert owners_paths[0] == owners_paths[1] |
||
204 | |||
205 | def test_no_path_constrained_reliability(self): |
||
206 | """Tests if the edges used in the paths |
||
207 | of the paths set do not have poor reliability |
||
208 | """ |
||
209 | requirements = {"reliability": 1} |
||
210 | |||
211 | self.initializer() |
||
212 | |||
213 | paths = self.graph.constrained_k_shortest_paths( |
||
214 | "User1", "User3", mandatory_metrics=requirements |
||
215 | ) |
||
216 | assert not paths |
||
217 | |||
218 | def test_path_constrained_reliability_detailed(self): |
||
219 | """Tests if the edges used in the paths |
||
220 | of the paths set do not have poor reliability |
||
221 | """ |
||
222 | reliabilities = [] |
||
223 | requirements = {"reliability": 3} |
||
224 | poor_reliability = 1 |
||
225 | |||
226 | self.initializer() |
||
227 | |||
228 | paths = self.graph.constrained_k_shortest_paths( |
||
229 | "User1", "User2", mandatory_metrics=requirements |
||
230 | ) |
||
231 | |||
232 | if paths: |
||
233 | for path in paths[0]["hops"]: |
||
234 | for i in range(1, len(path)): |
||
235 | endpoint_a = path[i - 1] |
||
236 | endpoint_b = path[i] |
||
237 | meta_data = self.graph.get_link_metadata( |
||
238 | endpoint_a, endpoint_b |
||
239 | ) |
||
240 | if meta_data and "reliability" in meta_data.keys(): |
||
241 | reliabilities.append(meta_data["reliability"]) |
||
242 | |||
243 | self.assertNotIn(poor_reliability, reliabilities) |
||
244 | |||
245 | else: |
||
246 | self.assertNotEqual(paths, []) |
||
247 | |||
248 | def test_path_constrained_delay(self): |
||
249 | """Tests if the edges used in the paths |
||
250 | from User 1 to User 2 have less than 30 delay. |
||
251 | """ |
||
252 | delays = [] |
||
253 | requirements = {"delay": 29} |
||
254 | |||
255 | self.initializer() |
||
256 | |||
257 | paths = self.graph.constrained_k_shortest_paths( |
||
258 | "User1", "User2", mandatory_metrics=requirements |
||
259 | ) |
||
260 | assert paths |
||
261 | |||
262 | for path in paths: |
||
263 | for i, j in zip( |
||
264 | range(0, len(path["hops"])), range(1, len(path["hops"])) |
||
265 | ): |
||
266 | endpoint_a = path["hops"][i] |
||
267 | endpoint_b = path["hops"][j] |
||
268 | meta_data = self.graph.get_link_metadata( |
||
269 | endpoint_a, endpoint_b |
||
270 | ) |
||
271 | if meta_data and "delay" in meta_data.keys(): |
||
272 | delays.append(meta_data["delay"]) |
||
273 | |||
274 | assert delays |
||
275 | for delay in delays: |
||
276 | assert delay <= requirements["delay"] |
||
277 | |||
278 | def links_metadata_values(self, path, attr): |
||
279 | """Method to build a list of metadata values of the links of a path""" |
||
280 | values = [] |
||
281 | for i, j in zip( |
||
282 | range(0, len(path["hops"])), range(1, len(path["hops"])) |
||
283 | ): |
||
284 | endpoint_a = path["hops"][i] |
||
285 | endpoint_b = path["hops"][j] |
||
286 | meta_data = self.graph.get_link_metadata(endpoint_a, endpoint_b) |
||
287 | if meta_data and attr in meta_data.keys(): |
||
288 | values.append(meta_data[attr]) |
||
289 | return values |
||
290 | |||
291 | View Code Duplication | def test_path_constrained_bandwidth_detailed(self): |
|
|
|||
292 | """Tests if the edges used in the paths |
||
293 | from User 1 to User 2 have at least 20 bandwidth. |
||
294 | """ |
||
295 | requirements = {"bandwidth": 20} |
||
296 | |||
297 | self.initializer() |
||
298 | |||
299 | paths = self.graph.constrained_k_shortest_paths( |
||
300 | "User1", "User2", mandatory_metrics=requirements |
||
301 | ) |
||
302 | assert paths |
||
303 | |||
304 | for path in paths: |
||
305 | bandwidths = self.links_metadata_values(path, "bandwidth") |
||
306 | assert bandwidths |
||
307 | |||
308 | for bandwidth in bandwidths: |
||
309 | assert bandwidth >= requirements["bandwidth"] |
||
310 | |||
311 | View Code Duplication | def test_path_constrained_bandwidth_detailed_t2(self): |
|
312 | """Tests if the edges used in the paths |
||
313 | from User 1 to User 2 have at least 20 bandwidth. |
||
314 | """ |
||
315 | requirements = {"bandwidth": 20} |
||
316 | |||
317 | self.initializer(val=2) |
||
318 | |||
319 | paths = self.graph.constrained_k_shortest_paths( |
||
320 | "User1", "User2", mandatory_metrics=requirements |
||
321 | ) |
||
322 | assert paths |
||
323 | |||
324 | for path in paths: |
||
325 | bandwidths = self.links_metadata_values(path, "bandwidth") |
||
326 | assert bandwidths |
||
327 | for bandwidth in bandwidths: |
||
328 | assert bandwidth >= requirements["bandwidth"] |
||
329 | |||
330 | def test_path_constrained_bandwidth_delay(self): |
||
331 | """Tests if the edges used in the paths from User 1 |
||
332 | to User 2 have at least 20 bandwidth and under 30 delay. |
||
333 | """ |
||
334 | requirements = {"bandwidth": 20, "delay": 29} |
||
335 | |||
336 | self.initializer() |
||
337 | |||
338 | paths = self.graph.constrained_k_shortest_paths( |
||
339 | "User1", "User2", mandatory_metrics=requirements |
||
340 | ) |
||
341 | assert paths |
||
342 | |||
343 | for path in paths: |
||
344 | |||
345 | bandwidths = self.links_metadata_values(path, "bandwidth") |
||
346 | assert bandwidths |
||
347 | for bandwidth in bandwidths: |
||
348 | assert bandwidth >= requirements["bandwidth"] |
||
349 | |||
350 | delays = self.links_metadata_values(path, "delay") |
||
351 | assert delays |
||
352 | for delay in delays: |
||
353 | assert delay <= requirements["delay"] |
||
354 | |||
355 | assert len(bandwidths) == len(delays) |
||
356 |