Skip to content

Commit

Permalink
tests: add basic load_data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
defanator committed Oct 16, 2024
1 parent 9365107 commit 892e9bd
Showing 1 changed file with 185 additions and 0 deletions.
185 changes: 185 additions & 0 deletions tests/zont_api_load_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
"""
Tests for Zont load_data API
"""

import logging
from unittest.mock import patch, MagicMock
from zont_api import ZontAPI

from tests.zont_device import MOCK_API_RESPONSE_SINGLE_DEVICE

__author__ = "Andrei Belov"
__license__ = "MIT"
__copyright__ = f"Copyright (c) {__author__}"


DATA_TYPES_ALL = [
"z3k_temperature",
"z3k_heating_circuit",
"z3k_boiler_adapter",
"z3k_analog_input",
]

MOCK_DATA_EMPTY_RESPONSE = {
"ok": True,
"responses": [
{
"device_id": 42,
"ok": True,
"time_truncated": False,
"z3k_temperature": {},
"z3k_heating_circuit": {},
"z3k_boiler_adapter": {},
"z3k_analog_input": {},
}
],
}

MOCK_DATA_RESPONSE_WITH_METRICS = {
"ok": True,
"responses": [
{
"device_id": 42,
"ok": True,
"time_truncated": False,
"z3k_temperature": {
"4242": [
[1000, 42.2],
[-60, 42.2],
[-60, 42.2],
]
},
"z3k_heating_circuit": {
"4343": {
"worktime": [
[1000, 0],
[-60, 0],
[-60, 0],
],
"status": [
[1000, 0],
[-120, 0],
],
}
},
"z3k_boiler_adapter": {
"4444": {
"s": [
[1000, []],
[-60, []],
[-60, []],
],
"ot": [
[1000, 7.0],
[-60, 7.0],
[-60, 6.0],
],
}
},
"z3k_analog_input": {
"4545": {
"voltage": [
[1000, 23.8],
[-120, 23.8],
],
"value": [
[1000, 238],
[-120, 238],
],
}
},
"timings": {
"z3k_temperature": {
"wall": 0.05566287040710449,
"proc": 0.002107100000102946,
},
"z3k_boiler_adapter": {
"wall": 0.08274435997009277,
"proc": 0.002168881000216061,
},
"z3k_heating_circuit": {
"wall": 0.05684089660644531,
"proc": 0.002351291000195488,
},
"z3k_analog_input": {
"wall": 0.08126425743103027,
"proc": 0.0019758700000238605,
},
},
}
],
}


@patch("zont_api.zont_api.requests")
def test_load_data_no_series(mock_requests, caplog):
"""
Load data with valid response but no serires
"""
caplog.set_level(logging.DEBUG)

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = MOCK_API_RESPONSE_SINGLE_DEVICE
mock_requests.post.return_value = mock_response

zapi = ZontAPI(token="testtoken", client="testclient")
assert isinstance(zapi, ZontAPI)
devices = zapi.get_devices()
assert len(devices) == 1
assert devices[0].id == 42

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = MOCK_DATA_EMPTY_RESPONSE
mock_requests.post.return_value = mock_response

data = zapi.load_data(
devices[0].id, data_types=DATA_TYPES_ALL, interval=(1000, 1120)
)

assert data.get("device_id") == 42
for key_name in DATA_TYPES_ALL:
assert key_name in data.keys(), f"{key_name} present in load_data response"
assert isinstance(data.get(key_name), dict), f"{key_name} is dict"
assert bool(data.get(key_name)) is False, f"{key_name} dict is empty"

# repeat the same request without explicit data_types and interval
data = zapi.load_data(devices[0].id)
assert data.get("device_id") == 42


@patch("zont_api.zont_api.requests")
def test_load_data_with_metrics(mock_requests, caplog):
"""
Load data with valid response and some metrics
"""
caplog.set_level(logging.DEBUG)

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = MOCK_API_RESPONSE_SINGLE_DEVICE
mock_requests.post.return_value = mock_response

zapi = ZontAPI(token="testtoken", client="testclient")
assert isinstance(zapi, ZontAPI)
devices = zapi.get_devices()
assert len(devices) == 1
assert devices[0].id == 42

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = MOCK_DATA_RESPONSE_WITH_METRICS
mock_requests.post.return_value = mock_response

data = zapi.load_data(
devices[0].id, data_types=DATA_TYPES_ALL, interval=(1000, 1120)
)

assert data.get("device_id") == 42
for key_name in DATA_TYPES_ALL:
assert key_name in data.keys(), f"{key_name} present in load_data response"
assert isinstance(data.get(key_name), dict), f"{key_name} is dict"
assert bool(data.get(key_name)) is True, f"{key_name} dict is not empty"

assert "timings" not in data.keys(), "timings section was removed"

0 comments on commit 892e9bd

Please sign in to comment.