Test Failed
Pull Request — master (#64)
by
unknown
02:28
created

build.tests.unit.test_models   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 150
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A TestMW.setUp() 0 11 1
A TestMW.test_end_mw_case_3() 0 17 1
A TestMW.test_as_dict() 0 16 1
A TestMW.test_start_mw_case_2() 0 16 1
A TestMW.test_end_mw_case_1() 0 16 1
A TestMW.test_start_mw_case_3() 0 16 1
A TestMW.test_end_mw_case_2() 0 17 1
A TestMW.test_start_mw_case_1() 0 15 1
1
"""Tests for the models module."""
2
import datetime
3
from unittest import TestCase
4
from unittest.mock import patch, MagicMock
5
6
from attrs import evolve
7
import pytz
8
from kytos.lib.helpers import get_controller_mock
9
from napps.kytos.maintenance.models import MaintenanceWindow as MW, Status
10
11
TIME_FMT = "%Y-%m-%dT%H:%M:%S%z"
12
13
14
class TestMW(TestCase):
15
    """Test of the MaintenanceWindow class."""
16
17
    # pylint: disable=protected-access
18
19
    def setUp(self):
20
        """Initialize before tests are executed."""
21
        self.controller = get_controller_mock()
22
        self.start = datetime.datetime.now(pytz.utc)
23
        self.start += datetime.timedelta(days=1)
24
        self.end = self.start + datetime.timedelta(hours=6)
25
        self.switches = [
26
            "01:23:45:67:89:ab:cd:ef"
27
        ]
28
        self.maintenance = MW(start = self.start, end = self.end,
29
                              switches = self.switches)
30
31
    def test_as_dict(self):
32
        """Test as_dict method."""
33
        mw_dict = self.maintenance.dict()
34
        expected_dict = {
35
            'description': '',
36
            'start': self.start,
37
            'end': self.end,
38
            'id': self.maintenance.id,
39
            'switches': self.switches,
40
            'interfaces': [],
41
            'links': [],
42
            'status': Status.PENDING,
43
            'inserted_at': None,
44
            'updated_at': None,
45
        }
46
        self.assertEqual(mw_dict, expected_dict)
47
48
    @patch('kytos.core.buffers.KytosEventBuffer.put')
49
    def test_start_mw_case_1(self, buffer_put_mock):
50
        """Test the method that starts a maintenance."""
51
        maintenance = self.maintenance.copy(
52
            update = {
53
                'switches': [
54
                    '01:23:45:67:89:ab:cd:ef',
55
                    '01:23:45:67:65:ab:cd:ef'
56
                ],
57
                'interfaces': [],
58
                'links': [],
59
            }
60
        )
61
        maintenance.start_mw(self.controller)
62
        buffer_put_mock.assert_called_once()
63
64
    @patch('kytos.core.buffers.KytosEventBuffer.put')
65
    def test_start_mw_case_2(self, buffer_put_mock):
66
        """Test the method that starts a maintenance."""
67
        interface_id = "interface_1"
68
        maintenance = self.maintenance.copy(
69
            update = {
70
                'switches': [
71
                    '01:23:45:67:89:ab:cd:ef',
72
                    '01:23:45:67:65:ab:cd:ef'
73
                ],
74
                'interfaces': [interface_id],
75
                'links': [],
76
            }
77
        )
78
        maintenance.start_mw(self.controller)
79
        self.assertEqual(buffer_put_mock.call_count, 2)
80
81
    @patch('kytos.core.buffers.KytosEventBuffer.put')
82
    def test_start_mw_case_3(self, buffer_put_mock):
83
        """Test the method that starts a maintenance."""
84
        interface_id = "interface_1"
85
        link1 = "link_1"
86
        link2 = "link_2"
87
        maintenance = self.maintenance.copy(
88
            update = {
89
                'switches': [
90
                ],
91
                'interfaces': [interface_id],
92
                'links': [link1, link2],
93
            }
94
        )
95
        maintenance.start_mw(self.controller)
96
        self.assertEqual(buffer_put_mock.call_count, 2)
97
98
    @patch('kytos.core.buffers.KytosEventBuffer.put')
99
    def test_end_mw_case_1(self, buffer_put_mock):
100
        """Test the method that starts a maintenance."""
101
        maintenance = self.maintenance.copy(
102
            update = {
103
                'switches': [
104
                    '01:23:45:67:89:ab:cd:ef',
105
                    '01:23:45:67:65:ab:cd:ef'
106
                ],
107
                'interfaces': [],
108
                'links': [],
109
                'status': Status.RUNNING,
110
            }
111
        )
112
        maintenance.end_mw(self.controller)
113
        buffer_put_mock.assert_called_once()
114
115
    @patch('kytos.core.buffers.KytosEventBuffer.put')
116
    def test_end_mw_case_2(self, buffer_put_mock):
117
        """Test the method that starts a maintenance."""
118
        interface_id = "interface_1"
119
        maintenance = self.maintenance.copy(
120
            update = {
121
                'switches': [
122
                    '01:23:45:67:89:ab:cd:ef',
123
                    '01:23:45:67:65:ab:cd:ef'
124
                ],
125
                'interfaces': [interface_id],
126
                'links': [],
127
                'status': Status.RUNNING,
128
            }
129
        )
130
        maintenance.end_mw(self.controller)
131
        self.assertEqual(buffer_put_mock.call_count, 2)
132
133
    @patch('kytos.core.buffers.KytosEventBuffer.put')
134
    def test_end_mw_case_3(self, buffer_put_mock):
135
        """Test the method that starts a maintenance."""
136
        interface_id = "interface_1"
137
        link1 = "link_1"
138
        link2 = "link_2"
139
        maintenance = self.maintenance.copy(
140
            update = {
141
                'switches': [
142
                ],
143
                'interfaces': [interface_id],
144
                'links': [link1, link2],
145
                'status': Status.RUNNING,
146
            }
147
        )
148
        maintenance.end_mw(self.controller)
149
        self.assertEqual(buffer_put_mock.call_count, 2)
150