1
|
|
|
"""Tests for the DB controller.""" |
2
|
1 |
|
from unittest.mock import MagicMock |
3
|
|
|
|
4
|
1 |
|
from controllers import ELineController |
5
|
|
|
|
6
|
|
|
|
7
|
1 |
|
class TestControllers(): |
8
|
|
|
"""Test DB Controllers""" |
9
|
|
|
|
10
|
1 |
|
def setup_method(self) -> None: |
11
|
|
|
"""Setup method""" |
12
|
1 |
|
self.eline = ELineController(MagicMock()) |
13
|
1 |
|
self.evc_dict = { |
14
|
|
|
"id": "1234", |
15
|
|
|
"uni_a": { |
16
|
|
|
"interface_id": "00:00:00:00:00:00:00:01:1", |
17
|
|
|
"tag": { |
18
|
|
|
"tag_type": 'vlan', |
19
|
|
|
"value": 200, |
20
|
|
|
} |
21
|
|
|
}, |
22
|
|
|
"uni_z": { |
23
|
|
|
"interface_id": "00:00:00:00:00:00:00:02:2", |
24
|
|
|
"tag": { |
25
|
|
|
"tag_type": 'vlan', |
26
|
|
|
"value": 200, |
27
|
|
|
} |
28
|
|
|
}, |
29
|
|
|
"name": "EVC 1", |
30
|
|
|
"dynamic_backup_path": True, |
31
|
|
|
"creation_time": "2022-05-06T21:34:10", |
32
|
|
|
"priority": 100, |
33
|
|
|
"active": False, |
34
|
|
|
"enabled": True, |
35
|
|
|
"circuit_scheduler": [] |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
def test_bootstrap_indexes(self): |
39
|
|
|
"""Test bootstrap_indexes""" |
40
|
1 |
|
self.eline.bootstrap_indexes() |
41
|
1 |
|
expected_indexes = [ |
42
|
|
|
("evcs", [("circuit_scheduler", 1)]), |
43
|
|
|
("evcs", [("archived", 1)]), |
44
|
|
|
] |
45
|
1 |
|
mock = self.eline.mongo.bootstrap_index |
46
|
1 |
|
assert mock.call_count == len(expected_indexes) |
47
|
|
|
|
48
|
1 |
|
def test_get_circuits(self): |
49
|
|
|
"""Test get_circuits""" |
50
|
|
|
|
51
|
1 |
|
assert "circuits" in self.eline.get_circuits() |
52
|
1 |
|
assert self.eline.db.evcs.aggregate.call_count == 1 |
53
|
|
|
|
54
|
1 |
|
def test_get_circuits_archived_false(self): |
55
|
|
|
"""Test get_circuits with archive being false""" |
56
|
1 |
|
self.eline.get_circuits(archived=None) |
57
|
1 |
|
args = self.eline.db.evcs.aggregate.call_args[0][0][0] |
58
|
1 |
|
assert args["$match"] == {} |
59
|
|
|
|
60
|
1 |
|
def test_get_circuits_archived_true(self): |
61
|
|
|
"""Test get_circuits with archive being true""" |
62
|
1 |
|
self.eline.get_circuits(archived="true") |
63
|
1 |
|
args = self.eline.db.evcs.aggregate.call_args[0][0][0] |
64
|
1 |
|
assert args["$match"] == {'archived': True} |
65
|
|
|
|
66
|
1 |
|
def test_get_circuits_metadata(self): |
67
|
|
|
"""Test get_circuits with metadata""" |
68
|
1 |
|
metadata = {"metadata.test": "123"} |
69
|
1 |
|
self.eline.get_circuits(archived=True, metadata=metadata) |
70
|
1 |
|
args = self.eline.db.evcs.aggregate.call_args[0][0][0] |
71
|
1 |
|
assert args["$match"]["metadata.test"] == 123 |
72
|
|
|
|
73
|
1 |
|
def test_upsert_evc(self): |
74
|
|
|
"""Test upsert_evc""" |
75
|
|
|
|
76
|
1 |
|
self.eline.upsert_evc(self.evc_dict) |
77
|
1 |
|
assert self.eline.db.evcs.find_one_and_update.call_count == 1 |
78
|
|
|
|
79
|
1 |
|
def test_update_evcs_metadata(self): |
80
|
|
|
"""Test update_evcs_metadata""" |
81
|
1 |
|
circuit_ids = ["123", "456", "789"] |
82
|
1 |
|
metadata = {"info": "testing"} |
83
|
1 |
|
self.eline.update_evcs_metadata(circuit_ids, metadata, "add") |
84
|
1 |
|
arg = self.eline.db.evcs.bulk_write.call_args[0][0] |
85
|
1 |
|
assert len(arg) == 3 |
86
|
1 |
|
assert self.eline.db.evcs.bulk_write.call_count == 1 |
87
|
|
|
|
88
|
1 |
|
def test_update_evcs(self): |
89
|
|
|
"""Test update_evcs""" |
90
|
1 |
|
evc2 = dict(self.evc_dict | {"id": "456"}) |
91
|
1 |
|
self.eline.update_evcs([self.evc_dict, evc2]) |
92
|
1 |
|
arg = self.eline.db.evcs.bulk_write.call_args[0][0] |
93
|
1 |
|
assert len(arg) == 2 |
94
|
|
|
assert self.eline.db.evcs.bulk_write.call_count == 1 |
95
|
|
|
|