Passed
Pull Request — master (#73)
by macartur
02:27
created

build.tests.models.test_evc_base   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 212
Duplicated Lines 14.15 %

Importance

Changes 0
Metric Value
eloc 164
dl 30
loc 212
rs 10
c 0
b 0
f 0
wmc 22

11 Methods

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