Skip to content

Commit f79350d

Browse files
committed
Add basic unittests
1 parent 83b123f commit f79350d

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

netdata/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for the Python Netdata client."""

netdata/tests/test_chart.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Test the chart data retrieval."""
2+
import pytest
3+
from pytest_httpx import HTTPXMock
4+
5+
from netdata import Netdata, exceptions
6+
7+
RESPONSE_VALID = {
8+
"labels": [
9+
"time",
10+
"guest_nice",
11+
"guest",
12+
"steal",
13+
"softirq",
14+
"irq",
15+
"user",
16+
"system",
17+
"nice",
18+
"iowait",
19+
],
20+
"data": [
21+
[1634256154, 0, 0, 0, 0.2506266, 3.759398, 32.45614, 6.015038, 0, 0.1253133]
22+
],
23+
}
24+
25+
26+
@pytest.mark.asyncio
27+
async def test_chart_valid(httpx_mock: HTTPXMock):
28+
"""Test a valid response."""
29+
httpx_mock.add_response(json=RESPONSE_VALID)
30+
31+
client = Netdata("localhost")
32+
await client.get_chart("system.cpu")
33+
34+
assert client.values.get("system") == 6.015038

netdata/tests/test_connection.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Test the connection."""
2+
import pytest
3+
from pytest_httpx import HTTPXMock
4+
5+
from netdata import Netdata
6+
import httpx
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_timeout(httpx_mock: HTTPXMock):
11+
"""Test if the connection is hitting the timeout."""
12+
13+
def raise_timeout(request, extensions: dict):
14+
"""Set the timeout for the requests."""
15+
raise httpx.ReadTimeout(
16+
f"Unable to read within {extensions['timeout']}", request=request
17+
)
18+
19+
httpx_mock.add_callback(raise_timeout)
20+
21+
with pytest.raises(httpx.ReadTimeout):
22+
client = Netdata("localhost")
23+
await client.get_info()

netdata/tests/test_info.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Test the chart data retrieval."""
2+
import pytest
3+
from pytest_httpx import HTTPXMock
4+
5+
from netdata import Netdata
6+
7+
RESPONSE_VALID = {
8+
"version": "v1.31.0",
9+
"uid": "792bb46a-fb11-11e7-b935-e6a17492adc8",
10+
"mirrored_hosts": ["london3"],
11+
"mirrored_hosts_status": [
12+
{
13+
"guid": "792bb46a-fb11-11e7-b935-e6a17492adc8",
14+
"claim_id": "792bb46a-fb11-11e7-b935-e6a17492adc8",
15+
}
16+
],
17+
}
18+
19+
20+
@pytest.mark.asyncio
21+
async def test_info_valid(httpx_mock: HTTPXMock):
22+
"""Test a valid response."""
23+
httpx_mock.add_response(json=RESPONSE_VALID)
24+
25+
client = Netdata("localhost")
26+
await client.get_info()
27+
28+
assert client.info.get("version") == "v1.31.0"

0 commit comments

Comments
 (0)