|
1
|
|
|
"""Tests for DB models.""" |
|
2
|
1 |
|
import pytest |
|
3
|
1 |
|
from pydantic import ValidationError |
|
4
|
|
|
|
|
5
|
1 |
|
from db.models import (DocumentBaseModel, EVCBaseDoc, EVCUpdateDoc, |
|
6
|
|
|
LinkConstraints, TAGDoc) |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
1 |
|
class TestDBModels(): |
|
10
|
|
|
"""Test the DB models""" |
|
11
|
|
|
|
|
12
|
1 |
|
def setup_method(self): |
|
13
|
|
|
"""Setup method.""" |
|
14
|
1 |
|
self.evc_dict = { |
|
15
|
|
|
"uni_a": { |
|
16
|
|
|
"interface_id": "00:00:00:00:00:00:00:04:1", |
|
17
|
|
|
"tag": { |
|
18
|
|
|
"tag_type": 'vlan', |
|
19
|
|
|
"value": 100, |
|
20
|
|
|
}, |
|
21
|
|
|
}, |
|
22
|
|
|
"uni_z": { |
|
23
|
|
|
"interface_id": "00:00:00:00:00:00:00:02:3", |
|
24
|
|
|
"tag": { |
|
25
|
|
|
"tag_type": 'vlan', |
|
26
|
|
|
"value": 100, |
|
27
|
|
|
} |
|
28
|
|
|
}, |
|
29
|
|
|
"name": "EVC 2", |
|
30
|
|
|
"dynamic_backup_path": True, |
|
31
|
|
|
"creation_time": "2022-04-06T21:34:10", |
|
32
|
|
|
"sb_priority": 81, |
|
33
|
|
|
"active": False, |
|
34
|
|
|
"enabled": False, |
|
35
|
|
|
"circuit_scheduler": [], |
|
36
|
|
|
"queue_id": None |
|
37
|
|
|
} |
|
38
|
1 |
|
self.evc_update = { |
|
39
|
|
|
"uni_a": { |
|
40
|
|
|
"interface_id": "00:00:00:00:00:00:00:04:1", |
|
41
|
|
|
"tag": { |
|
42
|
|
|
"tag_type": 'vlan', |
|
43
|
|
|
"value": 100, |
|
44
|
|
|
}, |
|
45
|
|
|
}, |
|
46
|
|
|
"uni_z": { |
|
47
|
|
|
"interface_id": "00:00:00:00:00:00:00:02:3", |
|
48
|
|
|
"tag": { |
|
49
|
|
|
"tag_type": 'vlan', |
|
50
|
|
|
"value": 100, |
|
51
|
|
|
} |
|
52
|
|
|
}, |
|
53
|
|
|
"name": "EVC 2", |
|
54
|
|
|
"dynamic_backup_path": True, |
|
55
|
|
|
"sb_priority": 81, |
|
56
|
|
|
"enabled": False, |
|
57
|
|
|
"circuit_scheduler": [], |
|
58
|
|
|
"queue_id": None |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
def test_evcbasedoc(self): |
|
62
|
|
|
"""Test EVCBaseDoc model""" |
|
63
|
|
|
|
|
64
|
1 |
|
evc = EVCBaseDoc(**self.evc_dict) |
|
65
|
1 |
|
assert evc.name == "EVC 2" |
|
66
|
1 |
|
assert evc.uni_a.interface_id == "00:00:00:00:00:00:00:04:1" |
|
67
|
1 |
|
assert evc.uni_z.interface_id == "00:00:00:00:00:00:00:02:3" |
|
68
|
1 |
|
assert evc.dynamic_backup_path |
|
69
|
1 |
|
assert evc.sb_priority == 81 |
|
70
|
1 |
|
assert evc.service_level == 0 |
|
71
|
1 |
|
assert not evc.active |
|
72
|
1 |
|
assert not evc.enabled |
|
73
|
1 |
|
assert not evc.circuit_scheduler |
|
74
|
|
|
|
|
75
|
1 |
|
def test_evcupdatedoc(self): |
|
76
|
|
|
"""Test EVCUpdateDoc model""" |
|
77
|
1 |
|
evc = EVCUpdateDoc(**self.evc_update) |
|
78
|
1 |
|
assert evc.name == "EVC 2" |
|
79
|
1 |
|
assert evc.uni_a.interface_id == "00:00:00:00:00:00:00:04:1" |
|
80
|
1 |
|
assert evc.uni_z.interface_id == "00:00:00:00:00:00:00:02:3" |
|
81
|
1 |
|
assert evc.dynamic_backup_path |
|
82
|
1 |
|
assert evc.sb_priority == 81 |
|
83
|
1 |
|
assert not evc.enabled |
|
84
|
1 |
|
assert not evc.circuit_scheduler |
|
85
|
|
|
|
|
86
|
1 |
|
def test_evcbasedoc_error(self): |
|
87
|
|
|
"""Test failure EVCBaseDoc model creation""" |
|
88
|
|
|
|
|
89
|
1 |
|
self.evc_dict["queue_id"] = "error" |
|
90
|
|
|
|
|
91
|
1 |
|
with pytest.raises(ValidationError): |
|
92
|
1 |
|
EVCBaseDoc(**self.evc_dict) |
|
93
|
|
|
|
|
94
|
1 |
|
def test_document_base_model_dict(self): |
|
95
|
|
|
"""test_document_base_model_dict.""" |
|
96
|
1 |
|
self.evc_dict["_id"] = "some_id" |
|
97
|
1 |
|
model = DocumentBaseModel(**self.evc_dict) |
|
98
|
1 |
|
assert "_id" not in model.dict(exclude={"_id"}) |
|
99
|
|
|
|
|
100
|
1 |
|
def test_tagdoc_value(self): |
|
101
|
|
|
"""Test TAGDoc value restrictions""" |
|
102
|
1 |
|
tag_mask = {"tag_type": 'vlan', "value": "untagged"} |
|
103
|
1 |
|
tag = TAGDoc(**tag_mask) |
|
104
|
1 |
|
assert tag.tag_type == 'vlan' |
|
105
|
1 |
|
assert tag.value == "untagged" |
|
106
|
|
|
|
|
107
|
1 |
|
tag_mask = {"tag_type": 'vlan', "value": "any"} |
|
108
|
1 |
|
tag = TAGDoc(**tag_mask) |
|
109
|
1 |
|
assert tag.tag_type == 'vlan' |
|
110
|
1 |
|
assert tag.value == "any" |
|
111
|
|
|
|
|
112
|
1 |
|
tag_list = {"tag_type": 'vlan', "value": [[1, 10]]} |
|
113
|
1 |
|
tag = TAGDoc(**tag_list) |
|
114
|
1 |
|
assert tag.tag_type == 'vlan' |
|
115
|
1 |
|
assert tag.value == [[1, 10]] |
|
116
|
|
|
|
|
117
|
1 |
|
def test_tagdoc_fail(self): |
|
118
|
|
|
"""Test TAGDoc value fail case""" |
|
119
|
1 |
|
tag_fail = {"tag_type": 'vlan', "value": "test_fail"} |
|
120
|
1 |
|
with pytest.raises(ValueError): |
|
121
|
1 |
|
TAGDoc(**tag_fail) |
|
122
|
|
|
|
|
123
|
1 |
|
@pytest.mark.parametrize( |
|
124
|
|
|
'attribute,acceptable_value', |
|
125
|
|
|
[ |
|
126
|
|
|
('bandwidth', 1.0), |
|
127
|
|
|
('bandwidth', 1), |
|
128
|
|
|
('ownership', 'Test A'), |
|
129
|
|
|
('reliability', 0.9), |
|
130
|
|
|
('reliability', 1), |
|
131
|
|
|
('utilization', 30.0), |
|
132
|
|
|
('utilization', 30), |
|
133
|
|
|
('delay', 10.0), |
|
134
|
|
|
('delay', 10), |
|
135
|
|
|
('priority', 1), |
|
136
|
|
|
('not_ownership', []), |
|
137
|
|
|
('not_ownership', ['Test B']), |
|
138
|
|
|
('not_ownership', ['Test B', 'Test C']), |
|
139
|
|
|
] |
|
140
|
|
|
) |
|
141
|
1 |
|
def test_link_metrics( |
|
142
|
|
|
self, |
|
143
|
|
|
attribute, |
|
144
|
|
|
acceptable_value, |
|
145
|
|
|
): |
|
146
|
|
|
""" |
|
147
|
|
|
Test attributes of link constraints across |
|
148
|
|
|
various acceptable values |
|
149
|
|
|
""" |
|
150
|
1 |
|
constraints = { |
|
151
|
|
|
attribute: acceptable_value |
|
152
|
|
|
} |
|
153
|
1 |
|
LinkConstraints(**constraints) |
|
154
|
|
|
|
|
155
|
1 |
|
@pytest.mark.parametrize( |
|
156
|
|
|
'attribute,unacceptable_value', |
|
157
|
|
|
[ |
|
158
|
|
|
('bandwidth', 'E'), |
|
159
|
|
|
('reliability', 'D'), |
|
160
|
|
|
('utilization', 'C'), |
|
161
|
|
|
('delay', 'B'), |
|
162
|
|
|
('priority', 'A'), |
|
163
|
|
|
('not_ownership', 'Yo'), |
|
164
|
|
|
] |
|
165
|
|
|
) |
|
166
|
1 |
|
def test_link_unacceptable_metrics( |
|
167
|
|
|
self, |
|
168
|
|
|
attribute, |
|
169
|
|
|
unacceptable_value, |
|
170
|
|
|
): |
|
171
|
|
|
""" |
|
172
|
|
|
Test attributes of link constraints across |
|
173
|
|
|
various unacceptable values |
|
174
|
|
|
""" |
|
175
|
1 |
|
constraints = { |
|
176
|
|
|
attribute: unacceptable_value |
|
177
|
|
|
} |
|
178
|
1 |
|
with pytest.raises(ValidationError): |
|
179
|
|
|
LinkConstraints(**constraints) |
|
180
|
|
|
|