Passed
Pull Request — master (#112)
by Rogerio
02:44
created

TestEVC.test_update_uni_a()   A

Complexity

Conditions 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nop 1
dl 16
loc 16
rs 9.75
c 0
b 0
f 0
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
12
from napps.kytos.mef_eline.scheduler import CircuitSchedule
13
from tests.helpers import get_uni_mocked, get_controller_mock
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
    def test_circuit_representation(self):
125
        """Test the method __repr__."""
126
        attributes = {
127
            "controller": get_controller_mock(),
128
            "name": "circuit_name",
129
            "uni_a": get_uni_mocked(is_valid=True),
130
            "uni_z": get_uni_mocked(is_valid=True)
131
        }
132
        evc = EVC(**attributes)
133
        expected_value = f'EVC({evc.id}, {evc.name})'
134
        self.assertEqual(str(evc), expected_value)
135
136
    def test_comparison_method(self):
137
        """Test the method __eq__."""
138
        attributes = {
139
            "controller": get_controller_mock(),
140
            "name": "circuit_name",
141
            "uni_a": get_uni_mocked(is_valid=True),
142
            "uni_z": get_uni_mocked(is_valid=True)
143
        }
144
        evc1 = EVC(**attributes)
145
        evc2 = EVC(**attributes)
146
147
        attributes = {
148
            "controller": get_controller_mock(),
149
            "name": "circuit_name_2",
150
            "uni_a": get_uni_mocked(is_valid=True),
151
            "uni_z": get_uni_mocked(is_valid=True)
152
        }
153
        evc3 = EVC(**attributes)
154
        evc4 = EVC(**attributes)
155
156
        self.assertEqual(evc1 == evc2, True)
157
        self.assertEqual(evc1 == evc3, False)
158
        self.assertEqual(evc2 == evc3, False)
159
        self.assertEqual(evc3 == evc4, True)
160
161
    def test_as_dict(self):
162
        """Test the method as_dict."""
163
        attributes = {
164
            "controller": get_controller_mock(),
165
            "id": "custom_id",
166
            "name": "custom_name",
167
            "uni_a": get_uni_mocked(is_valid=True),
168
            "uni_z": get_uni_mocked(is_valid=True),
169
            "start_date": '2018-08-21T18:44:54',
170
            "end_date": '2018-08-21T18:44:55',
171
            'primary_links': [],
172
            'request_time': '2018-08-21T19:10:41',
173
            'creation_time': '2018-08-21T18:44:54',
174
            'owner': "my_name",
175
            'circuit_scheduler': [
176
                CircuitSchedule.from_dict({"id":234243247, "action":"create",
177
                                          "frequency":"1 * * * *"}),
178
                CircuitSchedule.from_dict({"id":234243239, "action":"create",
179
                                          "interval":{"hours": 2}})
180
            ],
181
            'enabled': True,
182
            'priority': 2
183
        }
184
        evc = EVC(**attributes)
185
186
        expected_dict = {
187
            'id': 'custom_id',
188
            'name': 'custom_name',
189
            'uni_a': attributes['uni_a'].as_dict(),
190
            'uni_z': attributes['uni_z'].as_dict(),
191
            'start_date': '2018-08-21T18:44:54',
192
            'end_date': '2018-08-21T18:44:55',
193
            'bandwidth': 0,
194
            'primary_links': [],
195
            'backup_links': [],
196
            'current_path': [],
197
            'primary_path': [],
198
            'backup_path': [],
199
            'dynamic_backup_path': False,
200
            'request_time': '2018-08-21T19:10:41',
201
            'creation_time': '2018-08-21T18:44:54',
202
            'circuit_scheduler': [
203
                {
204
                    "id": 234243247,
205
                    "action": "create",
206
                    "frequency": "1 * * * *"
207
                },
208
                {
209
                "id": 234243239,
210
                "action": "create",
211
                "interval": {
212
                   "hours": 2
213
                }
214
               }
215
            ],
216
            'active': False,
217
            'enabled': True,
218
            'priority': 2
219
        }
220
        actual_dict = evc.as_dict()
221
        for name, value in expected_dict.items():
222
            actual = actual_dict.get(name)
223
            if name == 'circuit_scheduler':
224
                obj_value = {}
225
                obj_actual = {}
226
227
                for index, actual_item in enumerate(actual):
228
                    actual_item = actual_item.as_dict()
229
                    for actual_name, actual_value in actual_item.items():
230
                        obj_actual[actual_name] = actual_value
231
232
                    # Check the scheduled expected items
233
                    circuit_schedule = value[index]
234
                    for schedule_name, schedule_value in circuit_schedule.items():
235
                        obj_value[schedule_name] = schedule_value
236
                        self.assertEqual(obj_value[schedule_name], obj_actual[schedule_name])
237
            else:
238
                self.assertEqual(value, actual)
239