|
1
|
|
|
"""Tests for the models module.""" |
|
2
|
1 |
|
import pytest |
|
3
|
|
|
|
|
4
|
1 |
|
from datetime import datetime, timedelta, timezone |
|
5
|
1 |
|
import pytz |
|
6
|
1 |
|
from kytos.lib.helpers import get_controller_mock |
|
7
|
1 |
|
from napps.kytos.maintenance.models import MaintenanceWindow as MW, Status |
|
8
|
|
|
|
|
9
|
1 |
|
TIME_FMT = "%Y-%m-%dT%H:%M:%S%z" |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
class TestMW: |
|
13
|
|
|
"""Test of the MaintenanceWindow class.""" |
|
14
|
|
|
|
|
15
|
|
|
# pylint: disable=protected-access |
|
16
|
|
|
|
|
17
|
1 |
|
def setup_method(self): |
|
18
|
|
|
"""Initialize before tests are executed.""" |
|
19
|
1 |
|
self.controller = get_controller_mock() |
|
20
|
1 |
|
self.start = datetime.now(pytz.utc) |
|
21
|
1 |
|
self.start += timedelta(days=1) |
|
22
|
1 |
|
self.end = self.start + timedelta(hours=6) |
|
23
|
1 |
|
self.switches = [ |
|
24
|
|
|
"01:23:45:67:89:ab:cd:ef" |
|
25
|
|
|
] |
|
26
|
1 |
|
self.maintenance = MW( |
|
27
|
|
|
start=self.start, |
|
28
|
|
|
end=self.end, |
|
29
|
|
|
switches=self.switches |
|
30
|
|
|
) |
|
31
|
|
|
|
|
32
|
1 |
|
def test_as_dict(self): |
|
33
|
|
|
"""Test as_dict method.""" |
|
34
|
1 |
|
mw_dict = self.maintenance.model_dump() |
|
35
|
1 |
|
expected_dict = { |
|
36
|
|
|
'description': '', |
|
37
|
|
|
'start': self.start, |
|
38
|
|
|
'end': self.end, |
|
39
|
|
|
'id': self.maintenance.id, |
|
40
|
|
|
'switches': self.switches, |
|
41
|
|
|
'interfaces': [], |
|
42
|
|
|
'links': [], |
|
43
|
|
|
'status': Status.PENDING, |
|
44
|
|
|
'inserted_at': None, |
|
45
|
|
|
'updated_at': None, |
|
46
|
|
|
} |
|
47
|
1 |
|
assert mw_dict == expected_dict |
|
48
|
|
|
|
|
49
|
1 |
|
def test_start_in_past(self): |
|
50
|
1 |
|
start = datetime.now(pytz.utc) - timedelta(days=1) |
|
51
|
|
|
|
|
52
|
1 |
|
pytest.raises(ValueError, MW, |
|
53
|
|
|
start=start, |
|
54
|
|
|
end=self.end, |
|
55
|
|
|
switches=self.switches, |
|
56
|
|
|
) |
|
57
|
|
|
|
|
58
|
1 |
|
def test_end_before_start(self): |
|
59
|
1 |
|
end = datetime.now(pytz.utc) - timedelta(days=1) |
|
60
|
|
|
|
|
61
|
1 |
|
pytest.raises(ValueError, MW, |
|
62
|
|
|
start=self.start, |
|
63
|
|
|
end=end, |
|
64
|
|
|
switches=self.switches, |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
1 |
|
def test_items_empty(self): |
|
68
|
|
|
|
|
69
|
1 |
|
pytest.raises(ValueError, MW, |
|
70
|
|
|
start=self.start, |
|
71
|
|
|
end=self.end, |
|
72
|
|
|
) |
|
73
|
|
|
|
|
74
|
1 |
|
def test_no_end(self): |
|
75
|
|
|
"""Test MW without end time.""" |
|
76
|
1 |
|
window = MW.model_validate( |
|
77
|
|
|
{"start": self.start, "switches": self.switches} |
|
78
|
|
|
) |
|
79
|
|
|
assert window.end == datetime.max.replace(tzinfo=timezone.utc) |
|
80
|
|
|
|