Completed
Pull Request — master (#58)
by
unknown
02:30
created

TestResultsEdges.test_path9()   C

Complexity

Conditions 10

Size

Total Lines 71
Code Lines 58

Duplication

Lines 22
Ratio 30.99 %

Importance

Changes 0
Metric Value
cc 10
eloc 58
nop 1
dl 22
loc 71
rs 5.5744
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like build.tests.unit.test_results_edges.TestResultsEdges.test_path9() 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
from itertools import combinations
3
4
# Core modules to import
5
from kytos.core.link import Link
6
7
# module under test
8
from tests.unit.test_results import TestResults
9
10
11
class TestResultsEdges(TestResults):
12
    """Tests for the graph class."""
13
14
    def test_path1(self):
15
        """Tests paths between all users using unconstrained path alogrithm."""
16
        self.setup()
17
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
18
        for point_a, point_b in combos:
19
            results = self.get_path(point_a, point_b)
20
            self.assertNotEqual(results, [])
21
22
    def test_path2(self):
23
        """Tests paths between all users using constrained path algorithm,
24
        with no constraints set."""
25
        self.setup()
26
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
27
        for point_a, point_b in combos:
28
            results = self.get_path_constrained(point_a, point_b)
29
            self.assertNotEqual(results, [])
30
31
    def test_path3(self):
32
        """Tests paths between all users using constrained path algorithm,
33
        with the ownership constraint set to B."""
34
        self.setup()
35
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
36
        for point_a, point_b in combos:
37
            results = self.get_path_constrained(
38
                point_a, point_b, base=dict(ownership="B"))
39
            for result in results:
40
                for path in result["paths"]:
41
                    self.assertNotIn("S4:1", path)
42
                    self.assertNotIn("S5:2", path)
43
                    self.assertNotIn("S4:2", path)
44
                    self.assertNotIn("User1:2", path)
45
                    self.assertNotIn("S5:4", path)
46
                    self.assertNotIn("S6:2", path)
47
                    self.assertNotIn("S6:5", path)
48
                    self.assertNotIn("S10:1", path)
49
                    self.assertNotIn("S8:6", path)
50
                    self.assertNotIn("S10:2", path)
51
                    self.assertNotIn("S10:3", path)
52
                    self.assertNotIn("User2:1", path)
53
54
    def test_path4(self):
55
        """Tests paths between all users using constrained path algorithm,
56
        with the reliability constraint set to 3."""
57
        self.setup()
58
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
59
        for point_a, point_b in combos:
60
            results = self.get_path_constrained(
61
                point_a, point_b, base=dict(reliability=3))
62
            for result in results:
63
                for path in result["paths"]:
64
                    self.assertNotIn("S4:1", path)
65
                    self.assertNotIn("S5:2", path)
66
                    self.assertNotIn("S5:3", path)
67
                    self.assertNotIn("S6:1", path)
68
69
    def test_path5(self):
70
        """Tests paths between all users using constrained path algorithm,
71
        with the bandwidth contraint set to 100."""
72
        self.setup()
73
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
74
        for point_a, point_b in combos:
75
            results = self.get_path_constrained(
76
                point_a, point_b, base=dict(bandwidth=100))
77
            for result in results:
78
                for path in result["paths"]:
79
                    self.assertNotIn("S3:1", path)
80
                    self.assertNotIn("S5:1", path)
81
                    self.assertNotIn("User1:4", path)
82
                    self.assertNotIn("User4:3", path)
83
84
    def test_path6(self):
85
        """Tests paths between all users using constrained path algorithm,
86
        with the delay constraint set to 50."""
87
        self.setup()
88
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
89
        for point_a, point_b in combos:
90
            results = self.get_path_constrained(
91
                point_a, point_b, base=dict(delay=50))
92
            for result in results:
93
                for path in result["paths"]:
94
                    self.assertNotIn("S1:1", path)
95
                    self.assertNotIn("S2:1", path)
96
                    self.assertNotIn("S3:1", path)
97
                    self.assertNotIn("S5:1", path)
98
                    self.assertNotIn("S4:2", path)
99
                    self.assertNotIn("User1:2", path)
100
                    self.assertNotIn("S5:5", path)
101
                    self.assertNotIn("S8:2", path)
102
                    self.assertNotIn("S5:6", path)
103
                    self.assertNotIn("User1:3", path)
104
                    self.assertNotIn("S6:3", path)
105
                    self.assertNotIn("S9:1", path)
106
                    self.assertNotIn("S6:4", path)
107
                    self.assertNotIn("S9:2", path)
108
                    self.assertNotIn("S6:5", path)
109
                    self.assertNotIn("S10:1", path)
110
                    self.assertNotIn("S8:5", path)
111
                    self.assertNotIn("S9:4", path)
112
                    self.assertNotIn("User1:4", path)
113
                    self.assertNotIn("User4:3", path)
114
115
    def test_path7(self):
116
        """Tests paths between all users using constrained path algorithm,
117
        with the delay constraint set to 50, the bandwidth constraint set
118
        to 100, the reliability contraint set to 3, and the ownership
119
        constraint set to 'B' """
120
        self.setup()
121
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
122
        for point_a, point_b in combos:
123
            results = self.get_path_constrained(
124
                point_a, point_b, base=dict(delay=50, bandwidth=100,
125
                                            reliability=3,
126
                                            ownership="B"))
127
            for result in results:
128
                for path in result["paths"]:
129
                    # delay = 50 checks
130
                    self.assertNotIn("S1:1", path)
131
                    self.assertNotIn("S2:1", path)
132
                    self.assertNotIn("S3:1", path)
133
                    self.assertNotIn("S5:1", path)
134
                    self.assertNotIn("S4:2", path)
135
                    self.assertNotIn("User1:2", path)
136
                    self.assertNotIn("S5:5", path)
137
                    self.assertNotIn("S8:2", path)
138
                    self.assertNotIn("S5:6", path)
139
                    self.assertNotIn("User1:3", path)
140
                    self.assertNotIn("S6:3", path)
141
                    self.assertNotIn("S9:1", path)
142
                    self.assertNotIn("S6:4", path)
143
                    self.assertNotIn("S9:2", path)
144
                    self.assertNotIn("S6:5", path)
145
                    self.assertNotIn("S10:1", path)
146
                    self.assertNotIn("S8:5", path)
147
                    self.assertNotIn("S9:4", path)
148
                    self.assertNotIn("User1:4", path)
149
                    self.assertNotIn("User4:3", path)
150
151
                    # bandwidth = 100 checks
152
153
                    self.assertNotIn("S3:1", path)
154
                    self.assertNotIn("S5:1", path)
155
                    self.assertNotIn("User1:4", path)
156
                    self.assertNotIn("User4:3", path)
157
158
                    # reliability = 3 checks
159
160
                    self.assertNotIn("S4:1", path)
161
                    self.assertNotIn("S5:2", path)
162
                    self.assertNotIn("S5:3", path)
163
                    self.assertNotIn("S6:1", path)
164
165
                    # ownership = "B" checks
166
167
                    self.assertNotIn("S4:1", path)
168
                    self.assertNotIn("S5:2", path)
169
                    self.assertNotIn("S4:2", path)
170
                    self.assertNotIn("User1:2", path)
171
                    self.assertNotIn("S5:4", path)
172
                    self.assertNotIn("S6:2", path)
173
                    self.assertNotIn("S6:5", path)
174
                    self.assertNotIn("S10:1", path)
175
                    self.assertNotIn("S8:6", path)
176
                    self.assertNotIn("S10:2", path)
177
                    self.assertNotIn("S10:3", path)
178
                    self.assertNotIn("User2:1", path)
179
180
    def test_path8(self):
181
        """Tests paths between all users using constrained path algorithm,
182
        with the delay constraint set to 50, the bandwidth constraint
183
        set to 100, the reliability contraint set to 3, and the ownership
184
        constraint set to 'B'
185
186
        Tests conducted with flexibility enabled"""
187
        self.setup()
188
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
189
        for point_a, point_b in combos:
190
            results = self.get_path_constrained(
191
                point_a, point_b, flexible=dict(delay=50, bandwidth=100,
192
                                                reliability=3,
193
                                                ownership="B"))
194
            for result in results:
195
                # delay = 50 checks
196 View Code Duplication
                if "delay" in result["metrics"]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
197
                    for path in result["paths"]:
198
                        self.assertNotIn("S1:1", path)
199
                        self.assertNotIn("S2:1", path)
200
                        self.assertNotIn("S3:1", path)
201
                        self.assertNotIn("S5:1", path)
202
                        self.assertNotIn("S4:2", path)
203
                        self.assertNotIn("User1:2", path)
204
                        self.assertNotIn("S5:5", path)
205
                        self.assertNotIn("S8:2", path)
206
                        self.assertNotIn("S5:6", path)
207
                        self.assertNotIn("User1:3", path)
208
                        self.assertNotIn("S6:3", path)
209
                        self.assertNotIn("S9:1", path)
210
                        self.assertNotIn("S6:4", path)
211
                        self.assertNotIn("S9:2", path)
212
                        self.assertNotIn("S6:5", path)
213
                        self.assertNotIn("S10:1", path)
214
                        self.assertNotIn("S8:5", path)
215
                        self.assertNotIn("S9:4", path)
216
                        self.assertNotIn("User1:4", path)
217
                        self.assertNotIn("User4:3", path)
218
219
                # bandwidth = 100 checks
220
                if "bandwidth" in result["metrics"]:
221
                    for path in result["paths"]:
222
                        self.assertNotIn("S3:1", path)
223
                        self.assertNotIn("S5:1", path)
224
                        self.assertNotIn("User1:4", path)
225
                        self.assertNotIn("User4:3", path)
226
227
                # reliability = 3 checks
228
                if "reliability" in result["metrics"]:
229
                    for path in result["paths"]:
230
                        self.assertNotIn("S4:1", path)
231
                        self.assertNotIn("S5:2", path)
232
                        self.assertNotIn("S5:3", path)
233
                        self.assertNotIn("S6:1", path)
234
235
                # ownership = "B" checks
236
                if "ownership" in result["metrics"]:
237
                    for path in result["paths"]:
238
                        self.assertNotIn("S4:1", path)
239
                        self.assertNotIn("S5:2", path)
240
                        self.assertNotIn("S4:2", path)
241
                        self.assertNotIn("User1:2", path)
242
                        self.assertNotIn("S5:4", path)
243
                        self.assertNotIn("S6:2", path)
244
                        self.assertNotIn("S6:5", path)
245
                        self.assertNotIn("S10:1", path)
246
                        self.assertNotIn("S8:6", path)
247
                        self.assertNotIn("S10:2", path)
248
                        self.assertNotIn("S10:3", path)
249
                        self.assertNotIn("User2:1", path)
250
251
    def test_path9(self):
252
        """Tests paths between all users using constrained path algorithm,
253
        with the delay constraint set to 50, the bandwidth constraint
254
        set to 100, the reliability contraint set to 3, and the ownership
255
        constraint set to 'B'
256
257
        Tests conducted with all but ownership flexible"""
258
        self.setup()
259
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
260
        for point_a, point_b in combos:
261
            results = self.get_path_constrained(point_a, point_b,
262
                                                base={"ownership": "B"},
263
                                                flexible={"delay": 50,
264
                                                          "bandwidth": 100,
265
                                                          "reliability": 3})
266
            for result in results:
267
                # delay = 50 checks
268 View Code Duplication
                if "delay" in result["metrics"]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
269
                    for path in result["paths"]:
270
                        self.assertNotIn("S1:1", path)
271
                        self.assertNotIn("S2:1", path)
272
                        self.assertNotIn("S3:1", path)
273
                        self.assertNotIn("S5:1", path)
274
                        self.assertNotIn("S4:2", path)
275
                        self.assertNotIn("User1:2", path)
276
                        self.assertNotIn("S5:5", path)
277
                        self.assertNotIn("S8:2", path)
278
                        self.assertNotIn("S5:6", path)
279
                        self.assertNotIn("User1:3", path)
280
                        self.assertNotIn("S6:3", path)
281
                        self.assertNotIn("S9:1", path)
282
                        self.assertNotIn("S6:4", path)
283
                        self.assertNotIn("S9:2", path)
284
                        self.assertNotIn("S6:5", path)
285
                        self.assertNotIn("S10:1", path)
286
                        self.assertNotIn("S8:5", path)
287
                        self.assertNotIn("S9:4", path)
288
                        self.assertNotIn("User1:4", path)
289
                        self.assertNotIn("User4:3", path)
290
291
                # bandwidth = 100 checks
292
                if "bandwidth" in result["metrics"]:
293
                    for path in result["paths"]:
294
                        self.assertNotIn("S3:1", path)
295
                        self.assertNotIn("S5:1", path)
296
                        self.assertNotIn("User1:4", path)
297
                        self.assertNotIn("User4:3", path)
298
299
                # reliability = 3 checks
300
                if "reliability" in result["metrics"]:
301
                    for path in result["paths"]:
302
                        self.assertNotIn("S4:1", path)
303
                        self.assertNotIn("S5:2", path)
304
                        self.assertNotIn("S5:3", path)
305
                        self.assertNotIn("S6:1", path)
306
307
                # ownership = "B" checks
308
                self.assertIn("ownership", result["metrics"])
309
                for path in result["paths"]:
310
                    self.assertNotIn("S4:1", path)
311
                    self.assertNotIn("S5:2", path)
312
                    self.assertNotIn("S4:2", path)
313
                    self.assertNotIn("User1:2", path)
314
                    self.assertNotIn("S5:4", path)
315
                    self.assertNotIn("S6:2", path)
316
                    self.assertNotIn("S6:5", path)
317
                    self.assertNotIn("S10:1", path)
318
                    self.assertNotIn("S8:6", path)
319
                    self.assertNotIn("S10:2", path)
320
                    self.assertNotIn("S10:3", path)
321
                    self.assertNotIn("User2:1", path)
322
323
    def test_path10(self):
324
        """Tests that TypeError is generated by get_path_constrained
325
326
        Tests with ownership using an int type rather than string"""
327
        self.setup()
328
        with self.assertRaises(TypeError):
329
            self.get_path_constrained(
330
                "User1", "User2", base={"ownership": 1})
331
332
    @ staticmethod
333
    def generate_topology():
334
        """Generates a predetermined topology"""
335
        switches = {}
336
        interfaces = {}
337
        links = {}
338
339
        TestResults.create_switch("S1", switches)
340
        TestResults.add_interfaces(2, switches["S1"], interfaces)
341
342
        TestResults.create_switch("S2", switches)
343
        TestResults.add_interfaces(2, switches["S2"], interfaces)
344
345
        TestResults.create_switch("S3", switches)
346
        TestResults.add_interfaces(6, switches["S3"], interfaces)
347
348
        TestResults.create_switch("S4", switches)
349
        TestResults.add_interfaces(2, switches["S4"], interfaces)
350
351
        TestResults.create_switch("S5", switches)
352
        TestResults.add_interfaces(6, switches["S5"], interfaces)
353
354
        TestResults.create_switch("S6", switches)
355
        TestResults.add_interfaces(5, switches["S6"], interfaces)
356
357
        TestResults.create_switch("S7", switches)
358
        TestResults.add_interfaces(2, switches["S7"], interfaces)
359
360
        TestResults.create_switch("S8", switches)
361
        TestResults.add_interfaces(8, switches["S8"], interfaces)
362
363
        TestResults.create_switch("S9", switches)
364
        TestResults.add_interfaces(4, switches["S9"], interfaces)
365
366
        TestResults.create_switch("S10", switches)
367
        TestResults.add_interfaces(3, switches["S10"], interfaces)
368
369
        TestResults.create_switch("S11", switches)
370
        TestResults.add_interfaces(3, switches["S11"], interfaces)
371
372
        TestResults.create_switch("User1", switches)
373
        TestResults.add_interfaces(4, switches["User1"], interfaces)
374
375
        TestResults.create_switch("User2", switches)
376
        TestResults.add_interfaces(2, switches["User2"], interfaces)
377
378
        TestResults.create_switch("User3", switches)
379
        TestResults.add_interfaces(2, switches["User3"], interfaces)
380
381
        TestResults.create_switch("User4", switches)
382
        TestResults.add_interfaces(3, switches["User4"], interfaces)
383
384
        TestResultsEdges._fill_links(links, interfaces)
385
        TestResultsEdges._add_metadata_to_links(links)
386
387
        return (switches, links)
388
389
    @ staticmethod
390
    def _add_metadata_to_links(links):
391
        links["S1:1<->S2:1"].extend_metadata(
392
            {"reliability": 5, "bandwidth": 100, "delay": 105})
393
        links["S1:2<->User1:1"].extend_metadata(
394
            {"reliability": 5, "bandwidth": 100, "delay": 1})
395
        links["S2:2<->User4:1"].extend_metadata(
396
            {"reliability": 5, "bandwidth": 100, "delay": 10})
397
        links["S3:1<->S5:1"].extend_metadata(
398
            {"reliability": 5, "bandwidth": 10, "delay": 112})
399
        links["S3:2<->S7:1"].extend_metadata(
400
            {"reliability": 5, "bandwidth": 100, "delay": 1})
401
        links["S3:3<->S8:1"].extend_metadata(
402
            {"reliability": 5, "bandwidth": 100, "delay": 1})
403
        links["S3:4<->S11:1"].extend_metadata(
404
            {"reliability": 3, "bandwidth": 100, "delay": 6})
405
        links["S3:5<->User3:1"].extend_metadata(
406
            {"reliability": 5, "bandwidth": 100, "delay": 1})
407
        links["S3:6<->User4:2"].extend_metadata(
408
            {"reliability": 5, "bandwidth": 100, "delay": 10})
409
        links["S4:1<->S5:2"].extend_metadata(
410
            {"reliability": 1, "bandwidth": 100, "delay": 30,
411
             "ownership": "A"})
412
        links["S4:2<->User1:2"].extend_metadata(
413
            {"reliability": 3, "bandwidth": 100, "delay": 110,
414
             "ownership": "A"})
415
        links["S5:3<->S6:1"].extend_metadata(
416
            {"reliability": 1, "bandwidth": 100, "delay": 40})
417
        links["S5:4<->S6:2"].extend_metadata(
418
            {"reliability": 3, "bandwidth": 100, "delay": 40,
419
             "ownership": "A"})
420
        links["S5:5<->S8:2"].extend_metadata(
421
            {"reliability": 5, "bandwidth": 100, "delay": 112})
422
        links["S5:6<->User1:3"].extend_metadata(
423
            {"reliability": 3, "bandwidth": 100, "delay": 60})
424
        links["S6:3<->S9:1"].extend_metadata(
425
            {"reliability": 3, "bandwidth": 100, "delay": 60})
426
        links["S6:4<->S9:2"].extend_metadata(
427
            {"reliability": 5, "bandwidth": 100, "delay": 62})
428
        links["S6:5<->S10:1"].extend_metadata(
429
            {"bandwidth": 100, "delay": 108, "ownership": "A"})
430
        links["S7:2<->S8:3"].extend_metadata(
431
            {"reliability": 5, "bandwidth": 100, "delay": 1})
432
        links["S8:4<->S9:3"].extend_metadata(
433
            {"reliability": 3, "bandwidth": 100, "delay": 32})
434
        links["S8:5<->S9:4"].extend_metadata(
435
            {"reliability": 3, "bandwidth": 100, "delay": 110})
436
        links["S8:6<->S10:2"].extend_metadata(
437
            {"reliability": 5, "bandwidth": 100, "ownership": "A"})
438
        links["S8:7<->S11:2"].extend_metadata(
439
            {"reliability": 3, "bandwidth": 100, "delay": 7})
440
        links["S8:8<->User3:2"].extend_metadata(
441
            {"reliability": 5, "bandwidth": 100, "delay": 1})
442
        links["S10:3<->User2:1"].extend_metadata(
443
            {"reliability": 3, "bandwidth": 100, "delay": 10,
444
             "ownership": "A"})
445
        links["S11:3<->User2:2"].extend_metadata(
446
            {"reliability": 3, "bandwidth": 100, "delay": 6})
447
        links["User1:4<->User4:3"].extend_metadata(
448
            {"reliability": 5, "bandwidth": 10, "delay": 105})
449
450
    @ staticmethod
451
    def _fill_links(links, interfaces):
452
        links["S1:1<->S2:1"] = Link(interfaces["S1:1"], interfaces["S2:1"])
453
454
        links["S1:2<->User1:1"] = Link(interfaces["S1:2"],
455
                                       interfaces["User1:1"])
456
457
        links["S2:2<->User4:1"] = Link(interfaces["S2:2"],
458
                                       interfaces["User4:1"])
459
460
        links["S3:1<->S5:1"] = Link(interfaces["S3:1"], interfaces["S5:1"])
461
462
        links["S3:2<->S7:1"] = Link(interfaces["S3:2"], interfaces["S7:1"])
463
464
        links["S3:3<->S8:1"] = Link(interfaces["S3:3"], interfaces["S8:1"])
465
466
        links["S3:4<->S11:1"] = Link(interfaces["S3:4"], interfaces["S11:1"])
467
468
        links["S3:5<->User3:1"] = Link(interfaces["S3:5"],
469
                                       interfaces["User3:1"])
470
471
        links["S3:6<->User4:2"] = Link(interfaces["S3:6"],
472
                                       interfaces["User4:2"])
473
474
        links["S4:1<->S5:2"] = Link(interfaces["S4:1"], interfaces["S5:2"])
475
476
        links["S4:2<->User1:2"] = Link(interfaces["S4:2"],
477
                                       interfaces["User1:2"])
478
479
        links["S5:3<->S6:1"] = Link(interfaces["S5:3"], interfaces["S6:1"])
480
481
        links["S5:4<->S6:2"] = Link(interfaces["S5:4"], interfaces["S6:2"])
482
483
        links["S5:5<->S8:2"] = Link(interfaces["S5:5"], interfaces["S8:2"])
484
485
        links["S5:6<->User1:3"] = Link(interfaces["S5:6"],
486
                                       interfaces["User1:3"])
487
488
        links["S6:3<->S9:1"] = Link(interfaces["S6:3"], interfaces["S9:1"])
489
490
        links["S6:4<->S9:2"] = Link(interfaces["S6:4"], interfaces["S9:2"])
491
492
        links["S6:5<->S10:1"] = Link(interfaces["S6:5"], interfaces["S10:1"])
493
494
        links["S7:2<->S8:3"] = Link(interfaces["S7:2"], interfaces["S8:3"])
495
496
        links["S8:4<->S9:3"] = Link(interfaces["S8:4"], interfaces["S9:3"])
497
498
        links["S8:5<->S9:4"] = Link(interfaces["S8:5"], interfaces["S9:4"])
499
500
        links["S8:6<->S10:2"] = Link(interfaces["S8:6"], interfaces["S10:2"])
501
502
        links["S8:7<->S11:2"] = Link(interfaces["S8:7"], interfaces["S11:2"])
503
504
        links["S8:8<->User3:2"] = Link(interfaces["S8:8"],
505
                                       interfaces["User3:2"])
506
507
        links["S10:3<->User2:1"] = Link(interfaces["S10:3"],
508
                                        interfaces["User2:1"])
509
510
        links["S11:3<->User2:2"] = Link(interfaces["S11:3"],
511
                                        interfaces["User2:2"])
512
513
        links["User1:4<->User4:3"] = Link(interfaces["User1:4"],
514
                                          interfaces["User4:3"])
515