1
|
|
|
"""This test suite cover the db models, however, most have been |
2
|
|
|
indirectly tested on test_main.py, so for now it's mostly to cover |
3
|
|
|
the remaning parts.""" |
4
|
|
|
|
5
|
1 |
|
from datetime import datetime |
6
|
|
|
|
7
|
1 |
|
from napps.kytos.topology.db.models import (DocumentBaseModel, |
8
|
|
|
InterfaceDetailDoc, SwitchDoc) |
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
def test_document_base_model_dict() -> None: |
12
|
|
|
"""test_document_base_model_dict.""" |
13
|
1 |
|
_id = "some_id" |
14
|
1 |
|
utcnow = datetime.utcnow() |
15
|
1 |
|
payload = {"_id": _id, "inserted_at": utcnow, "updated_at": utcnow} |
16
|
1 |
|
model = DocumentBaseModel(**payload) |
17
|
1 |
|
assert model.model_dump() == {**payload, **{"id": _id}} |
18
|
1 |
|
assert "_id" not in model.model_dump(exclude={"_id"}) |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
def test_switch_doc_preset_interfaces() -> None: |
22
|
|
|
"""test_switch_doc_preset_interfaces.""" |
23
|
1 |
|
dpid = "00:00:00:00:00:00:00:01" |
24
|
1 |
|
interface_id = f"{dpid}:1" |
25
|
1 |
|
interfaces = { |
26
|
|
|
interface_id: { |
27
|
|
|
"id": interface_id, |
28
|
|
|
"port_number": 1, |
29
|
|
|
"lldp": True, |
30
|
|
|
"enabled": True, |
31
|
|
|
"active": True, |
32
|
|
|
"mac": "some_mac", |
33
|
|
|
"speed": 0, |
34
|
|
|
"name": "some_name", |
35
|
|
|
"switch": dpid, |
36
|
|
|
} |
37
|
|
|
} |
38
|
1 |
|
payload = { |
39
|
|
|
"_id": dpid, |
40
|
|
|
"enabled": True, |
41
|
|
|
"active": True, |
42
|
|
|
"interfaces": interfaces, |
43
|
|
|
} |
44
|
1 |
|
model = SwitchDoc(**payload) |
45
|
1 |
|
assert model |
46
|
1 |
|
assert interface_id == model.interfaces[0].id |
47
|
|
|
|
48
|
|
|
|
49
|
1 |
|
def test_switch_doc_preset_interfaces_speed_none() -> None: |
50
|
|
|
"""test_switch_doc_preset_interfaces speed none.""" |
51
|
1 |
|
dpid = "00:00:00:00:00:00:00:01" |
52
|
1 |
|
interface_id = f"{dpid}:1" |
53
|
1 |
|
interfaces = { |
54
|
|
|
interface_id: { |
55
|
|
|
"id": interface_id, |
56
|
|
|
"port_number": 1, |
57
|
|
|
"lldp": True, |
58
|
|
|
"enabled": True, |
59
|
|
|
"active": True, |
60
|
|
|
"mac": "some_mac", |
61
|
|
|
"speed": None, |
62
|
|
|
"name": "some_name", |
63
|
|
|
"switch": dpid, |
64
|
|
|
} |
65
|
|
|
} |
66
|
1 |
|
payload = { |
67
|
|
|
"_id": dpid, |
68
|
|
|
"enabled": True, |
69
|
|
|
"active": True, |
70
|
|
|
"interfaces": interfaces, |
71
|
|
|
} |
72
|
1 |
|
model = SwitchDoc(**payload) |
73
|
1 |
|
assert model |
74
|
1 |
|
assert interface_id == model.interfaces[0].id |
75
|
1 |
|
assert model.interfaces[0].speed is None |
76
|
|
|
|
77
|
|
|
|
78
|
1 |
|
def test_switch_doc_no_preset_interfaces() -> None: |
79
|
|
|
"""test_switch_doc_no_preset_interfaces.""" |
80
|
1 |
|
dpid = "00:00:00:00:00:00:00:01" |
81
|
1 |
|
interfaces = [] |
82
|
1 |
|
payload = { |
83
|
|
|
"_id": dpid, |
84
|
|
|
"enabled": True, |
85
|
|
|
"active": True, |
86
|
|
|
"interfaces": interfaces, |
87
|
|
|
} |
88
|
1 |
|
model = SwitchDoc(**payload) |
89
|
1 |
|
assert model |
90
|
1 |
|
assert model.interfaces == interfaces |
91
|
|
|
|
92
|
|
|
|
93
|
1 |
|
def test_interface_detail_doc() -> None: |
94
|
|
|
"""Test InterfaceDetailDoc""" |
95
|
1 |
|
payload = { |
96
|
|
|
"available_tags": {"vlan": [[200, 300], [500, 550]]}, |
97
|
|
|
"tag_ranges": {"vlan": [[100, 4095]]}, |
98
|
|
|
"special_available_tags": {"vlan": ["any"]}, |
99
|
|
|
"special_tags": {"vlan": ["any", "untagged"]} |
100
|
|
|
} |
101
|
1 |
|
model = InterfaceDetailDoc(**payload) |
102
|
|
|
assert model |
103
|
|
|
|