test_kytos_api_helper.test_get_evcs()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""Test kytos_api_helper.py"""
2
3 1
from httpx import Response
4 1
from unittest.mock import AsyncMock, MagicMock
5 1
from napps.kytos.telemetry_int.kytos_api_helper import (
6
    add_evcs_metadata,
7
    add_proxy_port_metadata,
8
    delete_proxy_port_metadata,
9
    get_evc,
10
    get_stored_flows,
11
    get_evcs,
12
)
13
14
15 1
async def test_get_evcs(evcs_data, monkeypatch) -> None:
16
    """Test get_evcs."""
17 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
18 1
    aclient_mock.get.return_value = Response(200, json=evcs_data, request=MagicMock())
19 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
20 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
21 1
    data = await get_evcs()
22 1
    assert aclient_mock.get.call_args[0][0] == "/evc/?archived=false"
23 1
    assert data == evcs_data
24
25
26 1
async def test_get_evc(evcs_data, monkeypatch) -> None:
27
    """Test get_evc."""
28 1
    evc_id = "3766c105686749"
29 1
    evc_data = evcs_data[evc_id]
30
31 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
32 1
    aclient_mock.get.return_value = Response(200, json=evc_data, request=MagicMock())
33 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
34 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
35
36 1
    data = await get_evc(evc_id)
37 1
    assert aclient_mock.get.call_args[0][0] == f"/evc/{evc_id}"
38 1
    assert data[evc_id] == evc_data
39
40
41 1 View Code Duplication
async def test_get_stored_flows(monkeypatch, intra_evc_evpl_flows_data) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
    """Test get_stored_flows."""
43 1
    evc_data = intra_evc_evpl_flows_data
44 1
    dpid = "00:00:00:00:00:00:00:01"
45 1
    cookies = [evc_data[dpid][0]["flow"]["cookie"]]
46
47 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
48 1
    aclient_mock.request.return_value = Response(
49
        200, json=intra_evc_evpl_flows_data, request=MagicMock()
50
    )
51 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
52 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
53
54 1
    data = await get_stored_flows(cookies)
55 1
    assert (
56
        aclient_mock.request.call_args[0][1] == "/stored_flows?"
57
        "state=installed&state=pending"
58
    )
59 1
    assert len(data) == 1
60 1
    assert list(data.keys()) == cookies
61 1
    assert len(data[cookies[0]]) == 2
62 1
    for flows in data.values():
63 1
        for flow in flows:
64 1
            assert flow["switch"] == dpid
65
66
67 1 View Code Duplication
async def test_get_stored_flows_no_cookies_filter(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
68
    monkeypatch, intra_evc_evpl_flows_data
69
) -> None:
70
    """Test get_stored_flows no cookies."""
71 1
    evc_data = intra_evc_evpl_flows_data
72 1
    dpid = "00:00:00:00:00:00:00:01"
73 1
    cookies = [evc_data[dpid][0]["flow"]["cookie"]]
74
75 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
76 1
    aclient_mock.get.return_value = Response(
77
        200, json=intra_evc_evpl_flows_data, request=MagicMock()
78
    )
79 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
80 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
81
82 1
    data = await get_stored_flows()
83 1
    assert (
84
        aclient_mock.get.call_args[0][0] == "/stored_flows?"
85
        "state=installed&state=pending"
86
    )
87 1
    assert len(data) == 1
88 1
    assert list(data.keys()) == cookies
89 1
    assert len(data[cookies[0]]) == 2
90 1
    for flows in data.values():
91 1
        for flow in flows:
92 1
            assert flow["switch"] == dpid
93
94
95 1
async def test_add_evcs_metadata(monkeypatch):
96
    """test add_evcs_metadata."""
97 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
98 1
    resp = "Operation successful"
99 1
    aclient_mock.post.return_value = Response(201, json=resp, request=MagicMock())
100 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
101 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
102
103 1
    data = await add_evcs_metadata({}, {"some_key": "some_val"})
104 1
    assert not data
105
106 1
    data = await add_evcs_metadata(
107
        {"some_id": {"id": "some_id"}}, {"some_key": "some_val"}
108
    )
109 1
    assert data == resp
110
111
112 1
async def test_add_proxy_port_metadata(monkeypatch):
113
    """test add_proxy_port_metadata."""
114 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
115 1
    resp = "Operation successful"
116 1
    aclient_mock.post.return_value = Response(201, json=resp, request=MagicMock())
117 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
118 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
119 1
    intf_id, port_no = "00:00:00:00:00:00:00:01:1", 7
120 1
    data = await add_proxy_port_metadata(intf_id, port_no)
121 1
    assert data
122
123
124 1
async def test_delete_proxy_port_metadata(monkeypatch):
125
    """test delete_proxy_port_metadata."""
126 1
    aclient_mock, awith_mock = AsyncMock(), MagicMock()
127 1
    resp = "Operation successful"
128 1
    aclient_mock.post.return_value = Response(201, json=resp, request=MagicMock())
129 1
    awith_mock.return_value.__aenter__.return_value = aclient_mock
130 1
    monkeypatch.setattr("httpx.AsyncClient", awith_mock)
131 1
    intf_id = "00:00:00:00:00:00:00:01:1"
132 1
    data = await delete_proxy_port_metadata(intf_id)
133
    assert data
134