add tests/test_collector.py

This commit is contained in:
sascha 2026-07-31 09:17:27 +02:00
parent 14f6bf7fdc
commit 470a19f5d0

33
tests/test_collector.py Normal file
View file

@ -0,0 +1,33 @@
import importlib.util
from pathlib import Path
spec = importlib.util.spec_from_file_location("collector", Path(__file__).parents[1] / "collector.py")
c = importlib.util.module_from_spec(spec)
spec.loader.exec_module(c)
def test_scalar_values():
assert c.extract_values(b"21.5") == {"value": 21.5}
assert c.extract_values(b"ON") == {"value": 1.0}
assert c.extract_values(b"offline") == {"value": 0.0}
def test_json_is_flattened_and_sensitive_fields_are_removed():
values = c.extract_values(b'{"temperature":21.2,"battery":88,"location":{"lat":48.1,"lon":11.5},"token":123,"online":true}')
assert values == {"temperature": 21.2, "battery": 88.0, "online": 1.0}
def test_strings_and_binary_payloads_are_not_stored():
assert c.extract_values(b"Sascha ist zuhause") == {}
assert c.extract_values(b"\xff\x00") == {}
def test_topic_policy():
assert c.topic_allowed("zigbee2mqtt/sensor/temperature")
assert not c.topic_allowed("camera/front/image")
assert not c.topic_allowed("homeassistant/sensor/foo/config")
def test_line_protocol_escaping():
line = c.line("mqtt value", {"topic": "room, one"}, {"value": 1.5}, 123)
assert line == "mqtt\\ value,topic=room\\,\\ one value=1.5 123"