Test Failed
Pull Request — master (#74)
by
unknown
02:33
created

build.tests.integration.test_paths_edges   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 252
Duplicated Lines 35.32 %

Importance

Changes 0
Metric Value
eloc 186
dl 89
loc 252
rs 10
c 0
b 0
f 0
wmc 28

10 Methods

Rating   Name   Duplication   Size   Complexity  
B TestPathsEdges.test_cspf_paths_mandatory_with_flexible() 0 74 6
A TestPathsEdges.test_cspf_bandwidth_between_u1_u4() 0 28 2
A TestPathsEdges.test_ownership_type_error() 0 7 2
A TestPathsEdges.test_cspf_delay_between_u2_u3() 20 20 2
A TestPathsEdges.test_constrained_k_shortest_paths_among_users() 16 16 4
A TestPathsEdges.test_cspf_flexible_between_s4_s6() 0 19 2
A TestPathsEdges.test_cspf_delay_spf_attribute_between_u1_u4() 0 15 2
A TestPathsEdges.test_cspf_reliability_between_u1_u2() 21 21 2
A TestPathsEdges.test_cspf_ownership_between_s4_s6() 20 20 2
A TestPathsEdges.test_k_shortest_paths_among_users() 12 12 4

How to fix   Duplicated Code   

Duplicated Code

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:

1
"""Module to test the KytosGraph in graph.py."""
2
from itertools import combinations
3
4
# pylint: disable=import-error
5
from tests.integration.edges_settings import EdgesSettings
6
7
8
class TestPathsEdges(EdgesSettings):
9
    """TestPathsEdges."""
10
11 View Code Duplication
    def test_k_shortest_paths_among_users(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
        """Tests paths between all users using unconstrained path algorithm."""
13
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
14
        self.initializer()
15
16
        for source, destination in combos:
17
            with self.subTest(source=source, destination=destination):
18
                paths = self.graph.k_shortest_paths(source, destination)
19
                assert paths
20
                for path in paths:
21
                    assert path[0] == source
22
                    assert path[-1] == destination
23
24 View Code Duplication
    def test_constrained_k_shortest_paths_among_users(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
        """Tests paths between all users using constrained path algorithm,
26
        with no constraints set.
27
        """
28
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
29
        self.initializer()
30
31
        for source, destination in combos:
32
            with self.subTest(source=source, destination=destination):
33
                paths = self.graph.constrained_k_shortest_paths(
34
                    source, destination
35
                )
36
                assert paths
37
                for path in paths:
38
                    assert path["hops"][0] == source
39
                    assert path["hops"][-1] == destination
40
41
    def test_cspf_delay_spf_attribute_between_u1_u4(self):
42
        """Test CSPF delay spf attribute between user1 and user4."""
43
        self.initializer()
44
        source = "User1"
45
        destination = "User4"
46
        spf_attribute = "delay"
47
        paths = self.graph.constrained_k_shortest_paths(
48
            source, destination, weight=spf_attribute
49
        )
50
        assert paths
51
        for path in paths:
52
            assert path["hops"][0] == source
53
            assert path["hops"][-1] == destination
54
        paths = self.graph.path_cost_builder(paths, weight=spf_attribute)
55
        assert paths[0]["cost"] == 105 + 1 + 1
56
57 View Code Duplication
    def test_cspf_reliability_between_u1_u2(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
58
        """Test CSPF reliability constraint between user1 and user2."""
59
        self.initializer()
60
        source = "User1"
61
        destination = "User2"
62
        paths = self.graph.constrained_k_shortest_paths(
63
            source, destination, mandatory_metrics={"reliability": 10}
64
        )
65
        assert not paths
66
67
        paths = self.graph.constrained_k_shortest_paths(
68
            source, destination, mandatory_metrics={"reliability": 3}
69
        )
70
        assert paths
71
72
        for path in paths:
73
            assert path["hops"][0] == source
74
            assert path["hops"][-1] == destination
75
            assert path["metrics"] == {"reliability": 3}
76
        paths = self.graph.path_cost_builder(paths)
77
        assert paths[0]["cost"] == 12
78
79
    def test_cspf_bandwidth_between_u1_u4(self):
80
        """Test CSPF bandwidth constraint between user1 and user4."""
81
        self.initializer()
82
        source = "User1"
83
        destination = "User4"
84
        spf_attribute = "delay"
85
        paths = self.graph.constrained_k_shortest_paths(
86
            source,
87
            destination,
88
            weight=spf_attribute,
89
            mandatory_metrics={"bandwidth": 200},
90
        )
91
        assert not paths
92
93
        paths = self.graph.constrained_k_shortest_paths(
94
            source,
95
            destination,
96
            weight=spf_attribute,
97
            mandatory_metrics={"bandwidth": 100},
98
        )
99
        assert paths
100
101
        for path in paths:
102
            assert path["hops"][0] == source
103
            assert path["hops"][-1] == destination
104
            assert path["metrics"] == {"bandwidth": 100}
105
        paths = self.graph.path_cost_builder(paths, weight=spf_attribute)
106
        assert paths[0]["cost"] == 122
107
108 View Code Duplication
    def test_cspf_delay_between_u2_u3(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
        """Test CSPF delay constraint between user2 and user3."""
110
        self.initializer()
111
        source = "User2"
112
        destination = "User3"
113
114
        paths = self.graph.constrained_k_shortest_paths(
115
            source, destination, mandatory_metrics={"delay": 1}
116
        )
117
        assert not paths
118
119
        paths = self.graph.constrained_k_shortest_paths(
120
            source, destination, mandatory_metrics={"delay": 50}
121
        )
122
        assert paths
123
        for path in paths:
124
            assert path["hops"][0] == source
125
            assert path["hops"][-1] == destination
126
        paths = self.graph.path_cost_builder(paths)
127
        assert paths[0]["cost"] >= 3
128
129 View Code Duplication
    def test_cspf_ownership_between_s4_s6(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
130
        """Test CSPF ownership constraint between switch4 and switch6."""
131
        self.initializer()
132
        source = "S6:2"
133
        destination = "S4:2"
134
135
        paths = self.graph.constrained_k_shortest_paths(
136
            source, destination, mandatory_metrics={"ownership": "B"}
137
        )
138
        assert not paths
139
140
        paths = self.graph.constrained_k_shortest_paths(
141
            source, destination, mandatory_metrics={"ownership": "A"}
142
        )
143
        assert paths
144
        for path in paths:
145
            assert path["hops"][0] == source
146
            assert path["hops"][-1] == destination
147
        paths = self.graph.path_cost_builder(paths)
148
        assert paths[0]["cost"] >= 3
149
150
    def test_cspf_flexible_between_s4_s6(self):
151
        """Test CSPF flexible constraint between switch4 and switch6."""
152
        self.initializer()
153
        source = "S6:2"
154
        destination = "S4:2"
155
156
        paths = self.graph.constrained_k_shortest_paths(
157
            source,
158
            destination,
159
            mandatory_metrics={"reliability": 2},
160
            flexible_metrics={"bandwidth": 60},
161
            minimium_hits=1,
162
        )
163
        assert paths
164
        for path in paths:
165
            assert path["hops"][0] == source
166
            assert path["hops"][-1] == destination
167
        paths = self.graph.path_cost_builder(paths)
168
        assert paths[0]["cost"] >= 3
169
170
    def test_cspf_paths_mandatory_with_flexible(self):
171
        """Tests paths between all users using constrained path algorithm,
172
        with the delay constraint set to 50, the bandwidth constraint
173
        set to 100, the reliability constraint set to 3, and the ownership
174
        constraint set to 'B'
175
176
        Tests conducted with all but ownership flexible
177
        """
178
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
179
        self.initializer()
180
181
        for source, destination in combos:
182
            paths = self.graph.constrained_k_shortest_paths(
183
                source,
184
                destination,
185
                mandatory_metrics={"ownership": "B"},
186
                flexible_metrics={
187
                    "delay": 50,
188
                    "bandwidth": 100,
189
                    "reliability": 3,
190
                },
191
            )
192
            for path in paths:
193
                # delay = 50 checks
194
                if "delay" in path["metrics"]:
195
                    self.assertNotIn("S1:1", path["hops"])
196
                    self.assertNotIn("S2:1", path["hops"])
197
                    self.assertNotIn("S3:1", path["hops"])
198
                    self.assertNotIn("S5:1", path["hops"])
199
                    self.assertNotIn("S4:2", path["hops"])
200
                    self.assertNotIn("User1:2", path["hops"])
201
                    self.assertNotIn("S5:5", path["hops"])
202
                    self.assertNotIn("S8:2", path["hops"])
203
                    self.assertNotIn("S5:6", path["hops"])
204
                    self.assertNotIn("User1:3", path["hops"])
205
                    self.assertNotIn("S6:3", path["hops"])
206
                    self.assertNotIn("S9:1", path["hops"])
207
                    self.assertNotIn("S6:4", path["hops"])
208
                    self.assertNotIn("S9:2", path["hops"])
209
                    self.assertNotIn("S6:5", path["hops"])
210
                    self.assertNotIn("S10:1", path["hops"])
211
                    self.assertNotIn("S8:5", path["hops"])
212
                    self.assertNotIn("S9:4", path["hops"])
213
                    self.assertNotIn("User1:4", path["hops"])
214
                    self.assertNotIn("User4:3", path["hops"])
215
216
                # bandwidth = 100 checks
217
                if "bandwidth" in path["metrics"]:
218
                    self.assertNotIn("S3:1", path["hops"])
219
                    self.assertNotIn("S5:1", path["hops"])
220
                    self.assertNotIn("User1:4", path["hops"])
221
                    self.assertNotIn("User4:3", path["hops"])
222
223
                # reliability = 3 checks
224
                if "reliability" in path["metrics"]:
225
                    self.assertNotIn("S4:1", path["hops"])
226
                    self.assertNotIn("S5:2", path["hops"])
227
                    self.assertNotIn("S5:3", path["hops"])
228
                    self.assertNotIn("S6:1", path["hops"])
229
230
                # ownership = "B" checks
231
                self.assertIn("ownership", path["metrics"])
232
                self.assertNotIn("S4:1", path["hops"])
233
                self.assertNotIn("S5:2", path["hops"])
234
                self.assertNotIn("S4:2", path["hops"])
235
                self.assertNotIn("User1:2", path["hops"])
236
                self.assertNotIn("S5:4", path["hops"])
237
                self.assertNotIn("S6:2", path["hops"])
238
                self.assertNotIn("S6:5", path["hops"])
239
                self.assertNotIn("S10:1", path["hops"])
240
                self.assertNotIn("S8:6", path["hops"])
241
                self.assertNotIn("S10:2", path["hops"])
242
                self.assertNotIn("S10:3", path["hops"])
243
                self.assertNotIn("User2:1", path["hops"])
244
245
    def test_ownership_type_error(self):
246
        """Tests that TypeError."""
247
        self.initializer()
248
249
        with self.assertRaises(TypeError):
250
            self.graph.constrained_k_shortest_paths(
251
                "User1", "User2", mandatory_metrics={"ownership": 1}
252
            )
253