Passed
Pull Request — master (#114)
by Rogerio
04:03
created

TestEVC._check_schedule_dict()   A

Complexity

Conditions 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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