Total Complexity | 62 |
Total Lines | 466 |
Duplicated Lines | 11.37 % |
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_results_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 | # module under test |
||
4 | from tests.integration.test_results import TestResults |
||
5 | |||
6 | |||
7 | class TestResultsMetadata(TestResults): |
||
8 | """Tests for the graph class. |
||
9 | |||
10 | Tests if the metadata in search result edges have passing values. |
||
11 | """ |
||
12 | |||
13 | def test_path_constrained_user_user(self): |
||
14 | """Test to see if there is a constrained |
||
15 | path between User - User.""" |
||
16 | self.initializer() |
||
17 | result = self.get_path_constrained("User1", "User2") |
||
18 | self.assertNotEqual(result, [], True) |
||
19 | |||
20 | def test_path_constrained_user_switch(self): |
||
21 | """Test to see if there is a constrained |
||
22 | path between User - Switch.""" |
||
23 | self.initializer() |
||
24 | result = self.get_path_constrained("User1", "S4") |
||
25 | self.assertNotEqual(result, [], True) |
||
26 | |||
27 | def test_path_constrained_switch_switch(self): |
||
28 | """Test to see if there is a constrained |
||
29 | path between Switch - Switch.""" |
||
30 | self.initializer() |
||
31 | result = self.get_path_constrained("S2", "S4") |
||
32 | self.assertNotEqual(result, [], True) |
||
33 | |||
34 | def test_no_path_constrained_user_user(self): |
||
35 | """Test to see if there is NOT a constrained |
||
36 | path between User - User.""" |
||
37 | self.initializer() |
||
38 | result = self.get_path_constrained("User1", "User3") |
||
39 | self.assertEqual(result, [], True) |
||
40 | |||
41 | def test_path_constrained_user_user_t1(self): |
||
42 | """Test to see if there is a constrained path between |
||
43 | User - User using the 2nd topology variant.""" |
||
44 | self.initializer(val=1) |
||
45 | result = self.get_path_constrained("User1", "User3") |
||
46 | self.assertNotEqual(result, [], True) |
||
47 | |||
48 | def test_no_path_constrained_user_user_t1(self): |
||
49 | """Test to see if there is NOT a constrained path between |
||
50 | User - User using the 2nd topology variant.""" |
||
51 | self.initializer(val=1) |
||
52 | result = self.get_path_constrained("User1", "User2") |
||
53 | self.assertEqual(result, [], True) |
||
54 | |||
55 | def test_no_path_constrained_switch_switch_t1(self): |
||
56 | """Test to see if there is NOT a constrained path between |
||
57 | Switch - Switch using the 2nd topology variant.""" |
||
58 | self.initializer(val=1) |
||
59 | result = self.get_path_constrained("S1", "S2") |
||
60 | self.assertEqual(result, [], True) |
||
61 | |||
62 | def test_path_constrained_user_user_t2(self): |
||
63 | """Test to see if there is a constrained path between |
||
64 | User - User using the 3rd topology variant.""" |
||
65 | self.initializer(val=2) |
||
66 | result = self.get_path_constrained("User1", "User2") |
||
67 | self.assertNotEqual(result, [], True) |
||
68 | |||
69 | def test_path_constrained_user_switch_t2(self): |
||
70 | """Test to see if there is a constrained path between |
||
71 | User - Switch using the 3rd topology variant.""" |
||
72 | self.initializer(val=2) |
||
73 | result = self.get_path_constrained("User1", "S4") |
||
74 | self.assertNotEqual(result, [], True) |
||
75 | |||
76 | def test_path_constrained_switch_switch_t2(self): |
||
77 | """Test to see if there is a constrained path between |
||
78 | two switches using the 3rd topology variant.""" |
||
79 | self.initializer(val=2) |
||
80 | result = self.get_path_constrained("S2", "S4") |
||
81 | self.assertNotEqual(result, [], True) |
||
82 | |||
83 | def test_path_constrained_reliability(self): |
||
84 | """Tests to see if the edges used in the paths |
||
85 | of the result set do not have poor reliability |
||
86 | """ |
||
87 | requirements = {"reliability": 3} |
||
88 | |||
89 | self.initializer() |
||
90 | |||
91 | result = self.get_path_constrained("User1", "User2", base=requirements) |
||
92 | |||
93 | self.assertNotEqual(result, []) |
||
94 | |||
95 | def test_no_path_constrained_reliability(self): |
||
96 | """Tests to see if the edges used in the paths |
||
97 | of the result set do not have poor reliability |
||
98 | """ |
||
99 | requirements = {"reliability": 3} |
||
100 | |||
101 | self.initializer() |
||
102 | |||
103 | result = self.get_path_constrained("User1", "User3", base=requirements) |
||
104 | |||
105 | self.assertEqual(result, []) |
||
106 | |||
107 | def test_path_constrained_reliability_detailed(self): |
||
108 | """Tests to see if the edges used in the paths |
||
109 | of the result set do not have poor reliability |
||
110 | """ |
||
111 | reliabilities = [] |
||
112 | requirements = {"reliability": 3} |
||
113 | poor_reliability = 1 |
||
114 | |||
115 | self.initializer() |
||
116 | |||
117 | result = self.get_path_constrained("User1", "User2", base=requirements) |
||
118 | |||
119 | if result: |
||
120 | for path in result[0]["paths"]: |
||
121 | for i in range(1, len(path)): |
||
122 | endpoint_a = path[i - 1] |
||
123 | endpoint_b = path[i] |
||
124 | meta_data = self.graph.get_link_metadata( |
||
125 | endpoint_a, endpoint_b) |
||
126 | if meta_data and "reliability" in meta_data.keys(): |
||
127 | reliabilities.append(meta_data["reliability"]) |
||
128 | |||
129 | self.assertNotIn(poor_reliability, reliabilities) |
||
130 | |||
131 | else: |
||
132 | self.assertNotEqual(result, []) |
||
133 | |||
134 | def test_path_constrained_delay(self): |
||
135 | """Tests to see if the edges used in the paths |
||
136 | from User 1 to User 2 have less than 30 delay. |
||
137 | """ |
||
138 | delays = [] |
||
139 | requirements = {"delay": 29} |
||
140 | |||
141 | self.initializer() |
||
142 | |||
143 | result = self.get_path_constrained("User1", "User2", base=requirements) |
||
144 | |||
145 | if result: |
||
146 | for path in result[0]["paths"]: |
||
147 | for i in range(1, len(path)): |
||
148 | endpoint_a = path[i - 1] |
||
149 | endpoint_b = path[i] |
||
150 | meta_data = self.graph.get_link_metadata( |
||
151 | endpoint_a, endpoint_b) |
||
152 | if meta_data and "delay" in meta_data.keys(): |
||
153 | delays.append(meta_data["delay"]) |
||
154 | |||
155 | if not delays: |
||
156 | self.assertNotEqual(delays, []) |
||
157 | |||
158 | valid = True |
||
159 | for delay in delays: |
||
160 | if delay > requirements["delay"]: |
||
161 | valid = False |
||
162 | break |
||
163 | |||
164 | self.assertEqual(valid, True) |
||
165 | |||
166 | View Code Duplication | def test_path_constrained_bandwidth_detailed(self): |
|
|
|||
167 | """Tests to see if the edges used in the paths |
||
168 | from User 1 to User 2 have at least 20 bandwidth. |
||
169 | """ |
||
170 | bandwidths = [] |
||
171 | requirements = {"bandwidth": 20} |
||
172 | |||
173 | self.initializer() |
||
174 | |||
175 | result = self.get_path_constrained("User1", "User2", base=requirements) |
||
176 | |||
177 | if result: |
||
178 | for path in result[0]["paths"]: |
||
179 | for i in range(1, len(path)): |
||
180 | endpoint_a = path[i - 1] |
||
181 | endpoint_b = path[i] |
||
182 | meta_data = self.graph.get_link_metadata( |
||
183 | endpoint_a, endpoint_b) |
||
184 | if meta_data and "bandwidth" in meta_data.keys(): |
||
185 | bandwidths.append(meta_data["bandwidth"]) |
||
186 | |||
187 | # for bandwidth in bandwidths: |
||
188 | # self.assertEqual(bandwidth < requirements["bandwidth"], False) |
||
189 | valid = True |
||
190 | for bandwidth in bandwidths: |
||
191 | if bandwidth < requirements["bandwidth"]: |
||
192 | valid = False |
||
193 | break |
||
194 | |||
195 | self.assertEqual(valid, True) |
||
196 | |||
197 | View Code Duplication | def test_path_constrained_bandwidth_detailed_t2(self): |
|
198 | """Tests to see if the edges used in the paths |
||
199 | from User 1 to User 2 have at least 20 bandwidth. |
||
200 | """ |
||
201 | bandwidths = [] |
||
202 | requirements = {"bandwidth": 20} |
||
203 | |||
204 | self.initializer(val=2) |
||
205 | |||
206 | result = self.get_path_constrained("User1", "User2", base=requirements) |
||
207 | |||
208 | if result: |
||
209 | for path in result[0]["paths"]: |
||
210 | for i in range(1, len(path)): |
||
211 | endpoint_a = path[i - 1] |
||
212 | endpoint_b = path[i] |
||
213 | meta_data = self.graph.get_link_metadata( |
||
214 | endpoint_a, endpoint_b) |
||
215 | if meta_data and "bandwidth" in meta_data.keys(): |
||
216 | bandwidths.append(meta_data["bandwidth"]) |
||
217 | |||
218 | for bandwidth in bandwidths: |
||
219 | self.assertEqual(bandwidth < requirements["bandwidth"], False) |
||
220 | |||
221 | def test_path_constrained_bandwidth_delay(self): |
||
222 | """Tests to see if the edges used in the paths from User 1 |
||
223 | to User 2 have at least 20 bandwidth and under 30 delay. |
||
224 | """ |
||
225 | bandwidths = [] |
||
226 | delays = [] |
||
227 | requirements = {"bandwidth": 20, "delay": 29} |
||
228 | |||
229 | self.initializer() |
||
230 | |||
231 | result = self.get_path_constrained("User1", "User2", base=requirements) |
||
232 | |||
233 | if result: |
||
234 | for path in result[0]["paths"]: |
||
235 | for i in range(1, len(path)): |
||
236 | endpoint_a = path[i - 1] |
||
237 | endpoint_b = path[i] |
||
238 | meta_data = self.graph.get_link_metadata( |
||
239 | endpoint_a, endpoint_b) |
||
240 | if meta_data and "bandwidth" in meta_data.keys(): |
||
241 | bandwidths.append(meta_data["bandwidth"]) |
||
242 | elif meta_data and "delay" in meta_data.keys(): |
||
243 | delays.append(meta_data["delay"]) |
||
244 | |||
245 | for bandwidth in bandwidths: |
||
246 | self.assertEqual(bandwidth < requirements["bandwidth"], False) |
||
247 | |||
248 | for delay in delays: |
||
249 | self.assertEqual(delay > requirements["delay"], False) |
||
250 | |||
251 | @staticmethod |
||
252 | def generate_topology(): |
||
253 | """Generates a predetermined topology.""" |
||
254 | switches = {} |
||
255 | interfaces = {} |
||
256 | links = {} |
||
257 | |||
258 | TestResultsMetadata.setting_switches_interfaces(interfaces, switches) |
||
259 | |||
260 | TestResultsMetadata.setting_links(interfaces, links) |
||
261 | |||
262 | TestResultsMetadata.adding_metadata(links) |
||
263 | |||
264 | return switches, links |
||
265 | |||
266 | @staticmethod |
||
267 | def setting_switches_interfaces(interfaces, switches): |
||
268 | """Generates the switches in a a predetermined topology.""" |
||
269 | TestResults.create_switch("User1", switches) |
||
270 | TestResults.add_interfaces(3, switches["User1"], interfaces) |
||
271 | |||
272 | TestResults.create_switch("S2", switches) |
||
273 | TestResults.add_interfaces(2, switches["S2"], interfaces) |
||
274 | |||
275 | TestResults.create_switch("User2", switches) |
||
276 | TestResults.add_interfaces(3, switches["User2"], interfaces) |
||
277 | |||
278 | TestResults.create_switch("S4", switches) |
||
279 | TestResults.add_interfaces(4, switches["S4"], interfaces) |
||
280 | |||
281 | TestResults.create_switch("S5", switches) |
||
282 | TestResults.add_interfaces(2, switches["S5"], interfaces) |
||
283 | |||
284 | @staticmethod |
||
285 | def setting_links(interfaces, links): |
||
286 | """Generates the links in a a predetermined topology.""" |
||
287 | TestResults.create_link("User1:1", "S2:1", interfaces, links) |
||
288 | |||
289 | TestResults.create_link("User1:2", "S5:1", interfaces, links) |
||
290 | |||
291 | TestResults.create_link("User1:3", "S4:1", interfaces, links) |
||
292 | |||
293 | TestResults.create_link("S2:2", "User2:1", interfaces, links) |
||
294 | |||
295 | TestResults.create_link("User2:2", "S4:2", interfaces, links) |
||
296 | |||
297 | TestResults.create_link("S5:2", "S4:3", interfaces, links) |
||
298 | |||
299 | TestResults.create_link("User2:3", "S4:4", interfaces, links) |
||
300 | |||
301 | @staticmethod |
||
302 | def adding_metadata(links): |
||
303 | """Add the links' metadata in a a predetermined topology.""" |
||
304 | TestResults.add_metadata_to_link( |
||
305 | "User1:1", "S2:1", { |
||
306 | "reliability": 3, "ownership": "B", "delay": 30, |
||
307 | "bandwidth": 20}, links) |
||
308 | |||
309 | TestResults.add_metadata_to_link( |
||
310 | "User1:2", "S5:1", { |
||
311 | "reliability": 1, "ownership": "A", "delay": 5, |
||
312 | "bandwidth": 50}, links) |
||
313 | |||
314 | TestResults.add_metadata_to_link( |
||
315 | "User1:3", "S4:1", { |
||
316 | "reliability": 3, "ownership": "A", "delay": 60, |
||
317 | "bandwidth": 10}, links) |
||
318 | |||
319 | TestResults.add_metadata_to_link( |
||
320 | "S2:2", "User2:1", { |
||
321 | "reliability": 3, "ownership": "B", "delay": 30, |
||
322 | "bandwidth": 20}, links) |
||
323 | |||
324 | TestResults.add_metadata_to_link( |
||
325 | "User2:2", "S4:2", { |
||
326 | "reliability": 3, "ownership": "B", "delay": 30, |
||
327 | "bandwidth": 10}, links) |
||
328 | |||
329 | TestResults.add_metadata_to_link( |
||
330 | "S5:2", "S4:3", { |
||
331 | "reliability": 1, "ownership": "A", "delay": 10, |
||
332 | "bandwidth": 50}, links) |
||
333 | |||
334 | TestResults.add_metadata_to_link( |
||
335 | "User2:3", "S4:4", { |
||
336 | "reliability": 3, "ownership": "A", "delay": 29, |
||
337 | "bandwidth": 20}, links) |
||
338 | |||
339 | @staticmethod |
||
340 | def generate_topology_1(): |
||
341 | """Generates a predetermined topology |
||
342 | - 2nd Variant.""" |
||
343 | switches = {} |
||
344 | interfaces = {} |
||
345 | links = {} |
||
346 | |||
347 | TestResultsMetadata.setting_switches_interfaces_1(interfaces, switches) |
||
348 | |||
349 | TestResultsMetadata.setting_links_1(interfaces, links) |
||
350 | |||
351 | TestResultsMetadata.adding_metadata_1(links) |
||
352 | |||
353 | return switches, links |
||
354 | |||
355 | @staticmethod |
||
356 | def setting_switches_interfaces_1(interfaces, switches): |
||
357 | """Generates the switches in a a predetermined topology |
||
358 | - 2nd variant.""" |
||
359 | TestResults.create_switch("User1", switches) |
||
360 | TestResults.add_interfaces(2, switches["User1"], interfaces) |
||
361 | |||
362 | TestResults.create_switch("User2", switches) |
||
363 | TestResults.add_interfaces(2, switches["User2"], interfaces) |
||
364 | |||
365 | TestResults.create_switch("User3", switches) |
||
366 | TestResults.add_interfaces(2, switches["User3"], interfaces) |
||
367 | |||
368 | TestResults.create_switch("S1", switches) |
||
369 | TestResults.add_interfaces(1, switches["S1"], interfaces) |
||
370 | |||
371 | TestResults.create_switch("S2", switches) |
||
372 | TestResults.add_interfaces(1, switches["S2"], interfaces) |
||
373 | |||
374 | TestResults.create_switch("S3", switches) |
||
375 | TestResults.add_interfaces(2, switches["S3"], interfaces) |
||
376 | |||
377 | @staticmethod |
||
378 | def setting_links_1(interfaces, links): |
||
379 | """Generates the links in a a predetermined topology |
||
380 | - 2nd Variant.""" |
||
381 | TestResults.create_link("User1:1", "S1:1", interfaces, links) |
||
382 | |||
383 | TestResults.create_link("User1:2", "S3:1", interfaces, links) |
||
384 | |||
385 | TestResults.create_link("User2:1", "S2:1", interfaces, links) |
||
386 | |||
387 | TestResults.create_link("User3:1", "S3:2", interfaces, links) |
||
388 | |||
389 | @staticmethod |
||
390 | def adding_metadata_1(links): |
||
391 | """Add the links' metadata in a a predetermined topology |
||
392 | - 2nd Variant.""" |
||
393 | TestResults.add_metadata_to_link( |
||
394 | "User1:1", "S1:1", { |
||
395 | "reliability": 3, "ownership": "B", "delay": 30, |
||
396 | "bandwidth": 20}, links) |
||
397 | |||
398 | TestResults.add_metadata_to_link( |
||
399 | "User1:2", "S3:1", { |
||
400 | "reliability": 1, "ownership": "A", "delay": 5, |
||
401 | "bandwidth": 50}, links) |
||
402 | |||
403 | TestResults.add_metadata_to_link( |
||
404 | "User2:1", "S2:1", { |
||
405 | "reliability": 3, "ownership": "A", "delay": 60, |
||
406 | "bandwidth": 10}, links) |
||
407 | |||
408 | TestResults.add_metadata_to_link( |
||
409 | "User3:1", "S3:2", { |
||
410 | "reliability": 3, "ownership": "B", "delay": 30, |
||
411 | "bandwidth": 20}, links) |
||
412 | |||
413 | @staticmethod |
||
414 | def generate_topology_2(): |
||
415 | """Generates a predetermined topology |
||
416 | - 3rd Variant.""" |
||
417 | switches = {} |
||
418 | interfaces = {} |
||
419 | links = {} |
||
420 | |||
421 | TestResultsMetadata.setting_switches_interfaces(interfaces, switches) |
||
422 | |||
423 | TestResultsMetadata.setting_links(interfaces, links) |
||
424 | |||
425 | TestResultsMetadata.adding_metadata_2(links) |
||
426 | |||
427 | return switches, links |
||
428 | |||
429 | @staticmethod |
||
430 | def adding_metadata_2(links): |
||
431 | """Add the links' metadata in a a predetermined topology |
||
432 | - 3rd Variant.""" |
||
433 | TestResults.add_metadata_to_link( |
||
434 | "User1:1", "S2:1", { |
||
435 | "reliability": 3, "ownership": "B", |
||
436 | "bandwidth": 20}, links) |
||
437 | |||
438 | TestResults.add_metadata_to_link( |
||
439 | "User1:2", "S5:1", { |
||
440 | "reliability": 1, "delay": 5, |
||
441 | "bandwidth": 50}, links) |
||
442 | |||
443 | TestResults.add_metadata_to_link( |
||
444 | "User1:3", "S4:1", { |
||
445 | "ownership": "A", "delay": 60, |
||
446 | "bandwidth": 10}, links) |
||
447 | |||
448 | TestResults.add_metadata_to_link( |
||
449 | "S2:2", "User2:1", { |
||
450 | "reliability": 3, |
||
451 | "bandwidth": 20}, links) |
||
452 | |||
453 | TestResults.add_metadata_to_link( |
||
454 | "User2:2", "S4:2", { |
||
455 | "ownership": "B", |
||
456 | "bandwidth": 10}, links) |
||
457 | |||
458 | TestResults.add_metadata_to_link( |
||
459 | "S5:2", "S4:3", { |
||
460 | "delay": 10, |
||
461 | "bandwidth": 50}, links) |
||
462 | |||
463 | TestResults.add_metadata_to_link( |
||
464 | "User2:3", "S4:4", { |
||
465 | "bandwidth": 20}, links) |
||
466 |