build.tests.unit.models.test_evc_base   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 241
Duplicated Lines 19.92 %

Importance

Changes 0
Metric Value
eloc 185
dl 48
loc 241
rs 10
c 0
b 0
f 0
wmc 21

12 Methods

Rating   Name   Duplication   Size   Complexity  
A TestEVC.test_update_name() 16 16 2
A TestEVC.test_circuit_representation() 0 11 1
A TestEVC.test_with_invalid_uni_z() 0 12 2
B TestEVC.test_as_dict() 0 63 2
A TestEVC.test_without_uni_a() 0 8 2
A TestEVC.test_attributes_empty() 0 7 2
A TestEVC.test_update_uni_a() 16 16 2
A TestEVC.test_update_disable() 0 16 1
A TestEVC.test_with_invalid_uni_a() 0 11 2
A TestEVC.test_comparison_method() 0 24 1
A TestEVC.test_update_uni_z() 16 16 2
A TestEVC.test_without_uni_z() 0 11 2

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 EVCBase class."""
2
import sys
3
from unittest import TestCase
4
from unittest.mock import patch
5
6
# pylint: disable=wrong-import-position
7
sys.path.insert(0, '/var/lib/kytos/napps/..')
8
# pylint: enable=wrong-import-position
9
from napps.kytos.mef_eline.models import EVC  # NOQA  pycodestyle
10
from napps.kytos.mef_eline.scheduler import \
11
    CircuitSchedule   # NOQA  pycodestyle
12
from tests.helpers import get_uni_mocked, \
13
    get_controller_mock  # NOQA  pycodestyle
14
15
16
class TestEVC(TestCase):  # pylint: disable=too-many-public-methods
17
    """Tests to verify EVC class."""
18
19
    def test_attributes_empty(self):
20
        """Test if the EVC raises an error with name is required."""
21
        attributes = {"controller": get_controller_mock()}
22
        error_message = "name is required."
23
        with self.assertRaises(ValueError) as handle_error:
24
            EVC(**attributes)
25
        self.assertEqual(str(handle_error.exception), error_message)
26
27
    def test_without_uni_a(self):
28
        """Test if the EVC raises and error with UNI A is required."""
29
        attributes = {"controller": get_controller_mock(),
30
                      "name": "circuit_name"}
31
        error_message = "uni_a is required."
32
        with self.assertRaises(ValueError) as handle_error:
33
            EVC(**attributes)
34
        self.assertEqual(str(handle_error.exception), error_message)
35
36
    def test_with_invalid_uni_a(self):
37
        """Test if the EVC raises and error with invalid UNI A."""
38
        attributes = {
39
            "controller": get_controller_mock(),
40
            "name": "circuit_name",
41
            "uni_a": get_uni_mocked(tag_value=82)
42
        }
43
        error_message = "VLAN tag 82 is not available in uni_a"
44
        with self.assertRaises(ValueError) as handle_error:
45
            EVC(**attributes)
46
        self.assertEqual(str(handle_error.exception), error_message)
47
48
    def test_without_uni_z(self):
49
        """Test if the EVC raises and error with UNI Z is required."""
50
        attributes = {
51
            "controller": get_controller_mock(),
52
            "name": "circuit_name",
53
            "uni_a": get_uni_mocked(is_valid=True)
54
        }
55
        error_message = "uni_z is required."
56
        with self.assertRaises(ValueError) as handle_error:
57
            EVC(**attributes)
58
        self.assertEqual(str(handle_error.exception), error_message)
59
60
    def test_with_invalid_uni_z(self):
61
        """Test if the EVC raises and error with UNI Z is required."""
62
        attributes = {
63
            "controller": get_controller_mock(),
64
            "name": "circuit_name",
65
            "uni_a": get_uni_mocked(is_valid=True),
66
            "uni_z": get_uni_mocked(tag_value=83)
67
        }
68
        error_message = "VLAN tag 83 is not available in uni_z"
69
        with self.assertRaises(ValueError) as handle_error:
70
            EVC(**attributes)
71
        self.assertEqual(str(handle_error.exception), error_message)
72
73 View Code Duplication
    def test_update_name(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
74
        """Test if raises and error when trying to update the name."""
75
        attributes = {
76
            "controller": get_controller_mock(),
77
            "name": "circuit_name",
78
            "uni_a": get_uni_mocked(is_valid=True),
79
            "uni_z": get_uni_mocked(is_valid=True)
80
        }
81
        update_dict = {
82
            "name": "circuit_name_2"
83
        }
84
        error_message = "name can't be be updated."
85
        with self.assertRaises(ValueError) as handle_error:
86
            evc = EVC(**attributes)
87
            evc.update(**update_dict)
88
        self.assertEqual(str(handle_error.exception), error_message)
89
90 View Code Duplication
    def test_update_uni_a(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
91
        """Test if raises and error when trying to update the uni_a."""
92
        attributes = {
93
            "controller": get_controller_mock(),
94
            "name": "circuit_name",
95
            "uni_a": get_uni_mocked(is_valid=True),
96
            "uni_z": get_uni_mocked(is_valid=True)
97
        }
98
        update_dict = {
99
            "uni_a": get_uni_mocked(is_valid=True)
100
        }
101
        error_message = "uni_a can't be be updated."
102
        with self.assertRaises(ValueError) as handle_error:
103
            evc = EVC(**attributes)
104
            evc.update(**update_dict)
105
        self.assertEqual(str(handle_error.exception), error_message)
106
107 View Code Duplication
    def test_update_uni_z(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
108
        """Test if raises and error when trying to update the uni_z."""
109
        attributes = {
110
            "controller": get_controller_mock(),
111
            "name": "circuit_name",
112
            "uni_a": get_uni_mocked(is_valid=True),
113
            "uni_z": get_uni_mocked(is_valid=True)
114
        }
115
        update_dict = {
116
            "uni_z": get_uni_mocked(is_valid=True)
117
        }
118
        error_message = "uni_z can't be be updated."
119
        with self.assertRaises(ValueError) as handle_error:
120
            evc = EVC(**attributes)
121
            evc.update(**update_dict)
122
        self.assertEqual(str(handle_error.exception), error_message)
123
124
    @patch('napps.kytos.mef_eline.models.EVC.sync')
125
    def test_update_disable(self, _sync_mock):
126
        """Test if evc is disabled."""
127
        attributes = {
128
            "controller": get_controller_mock(),
129
            "name": "circuit_name",
130
            "enable": True,
131
            "uni_a": get_uni_mocked(is_valid=True),
132
            "uni_z": get_uni_mocked(is_valid=True)
133
        }
134
        update_dict = {
135
            "enable": False
136
        }
137
        evc = EVC(**attributes)
138
        evc.update(**update_dict)
139
        self.assertIs(evc.is_enabled(), False)
140
141
    def test_circuit_representation(self):
142
        """Test the method __repr__."""
143
        attributes = {
144
            "controller": get_controller_mock(),
145
            "name": "circuit_name",
146
            "uni_a": get_uni_mocked(is_valid=True),
147
            "uni_z": get_uni_mocked(is_valid=True)
148
        }
149
        evc = EVC(**attributes)
150
        expected_value = f'EVC({evc.id}, {evc.name})'
151
        self.assertEqual(str(evc), expected_value)
152
153
    def test_comparison_method(self):
154
        """Test the method __eq__."""
155
        attributes = {
156
            "controller": get_controller_mock(),
157
            "name": "circuit_name",
158
            "uni_a": get_uni_mocked(is_valid=True),
159
            "uni_z": get_uni_mocked(is_valid=True)
160
        }
161
        evc1 = EVC(**attributes)
162
        evc2 = EVC(**attributes)
163
164
        attributes = {
165
            "controller": get_controller_mock(),
166
            "name": "circuit_name_2",
167
            "uni_a": get_uni_mocked(is_valid=True),
168
            "uni_z": get_uni_mocked(is_valid=True)
169
        }
170
        evc3 = EVC(**attributes)
171
        evc4 = EVC(**attributes)
172
173
        self.assertEqual(evc1 == evc2, True)
174
        self.assertEqual(evc1 == evc3, False)
175
        self.assertEqual(evc2 == evc3, False)
176
        self.assertEqual(evc3 == evc4, True)
177
178
    def test_as_dict(self):
179
        """Test the method as_dict."""
180
        attributes = {
181
            "controller": get_controller_mock(),
182
            "id": "custom_id",
183
            "name": "custom_name",
184
            "uni_a": get_uni_mocked(is_valid=True),
185
            "uni_z": get_uni_mocked(is_valid=True),
186
            "start_date": '2018-08-21T18:44:54',
187
            "end_date": '2018-08-21T18:44:55',
188
            'primary_links': [],
189
            'request_time': '2018-08-21T19:10:41',
190
            'creation_time': '2018-08-21T18:44:54',
191
            'owner': "my_name",
192
            'circuit_scheduler': [
193
                CircuitSchedule.from_dict({"id": 234243247, "action": "create",
194
                                          "frequency": "1 * * * *"}),
195
                CircuitSchedule.from_dict({"id": 234243239, "action": "create",
196
                                          "interval": {"hours": 2}})
197
            ],
198
            'enabled': True,
199
            'priority': 2
200
        }
201
        evc = EVC(**attributes)
202
203
        expected_dict = {
204
            'id': 'custom_id',
205
            'name': 'custom_name',
206
            'uni_a': attributes['uni_a'].as_dict(),
207
            'uni_z': attributes['uni_z'].as_dict(),
208
            'start_date': '2018-08-21T18:44:54',
209
            'end_date': '2018-08-21T18:44:55',
210
            'bandwidth': 0,
211
            'primary_links': [],
212
            'backup_links': [],
213
            'current_path': [],
214
            'primary_path': [],
215
            'backup_path': [],
216
            'dynamic_backup_path': False,
217
            'request_time': '2018-08-21T19:10:41',
218
            'creation_time': '2018-08-21T18:44:54',
219
            'circuit_scheduler': [
220
                {
221
                    "id": 234243247,
222
                    "action": "create",
223
                    "frequency": "1 * * * *"
224
                },
225
                {
226
                    "id": 234243239,
227
                    "action": "create",
228
                    "interval": {
229
                        "hours": 2
230
                    }
231
                }
232
            ],
233
            'active': False,
234
            'enabled': True,
235
            'priority': 2
236
        }
237
        actual_dict = evc.as_dict()
238
        for name, value in expected_dict.items():
239
            actual = actual_dict.get(name)
240
            self.assertEqual(value, actual)
241