Test Failed
Push — master ( 1f710a...437722 )
by Antonio
03:41
created

build.tests.unit.models.test_evc_base   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 223
Duplicated Lines 21.52 %

Importance

Changes 0
Metric Value
eloc 171
dl 48
loc 223
rs 10
c 0
b 0
f 0
wmc 20

11 Methods

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