- Bump version to 3.0.0 and update docs - Fix Debian payload to include TUI and install /usr/bin/sensorpajen-tui wrapper - Make systemd unit upgrades safer and ignore deb build artifacts
99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
import os
|
|
import pytest
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
import sensorpajen.config as config
|
|
|
|
def test_config_defaults():
|
|
assert config.MQTT_HOST == "localhost"
|
|
assert config.MQTT_PORT == 1883
|
|
assert config.MQTT_USER == "user"
|
|
assert config.MQTT_PASSWORD == "password"
|
|
assert config.MQTT_CLIENT_ID == "sensorpajen"
|
|
|
|
def test_sensor_config_load(tmp_path):
|
|
import sensorpajen.config as config
|
|
|
|
config_file = tmp_path / "sensors.json"
|
|
sensors_data = {
|
|
"sensors": [
|
|
{"mac": "AA:BB:CC:DD:EE:FF", "name": "Living Room"},
|
|
{"mac": "11:22:33:44:55:66", "name": "Kitchen"}
|
|
]
|
|
}
|
|
|
|
import json
|
|
with open(config_file, "w") as f:
|
|
json.dump(sensors_data, f)
|
|
|
|
sensor_cfg = config.SensorConfig(config_file=str(config_file))
|
|
assert sensor_cfg.sensors == {
|
|
"AA:BB:CC:DD:EE:FF": "Living Room",
|
|
"11:22:33:44:55:66": "Kitchen"
|
|
}
|
|
assert sensor_cfg.get_name("AA:BB:CC:DD:EE:FF") == "Living Room"
|
|
assert sensor_cfg.get_name("UNKNOWN") == "UNKNOWN"
|
|
|
|
|
|
def test_sensor_config_comment_load_and_clear(tmp_path):
|
|
import json
|
|
import sensorpajen.config as config
|
|
|
|
config_file = tmp_path / "sensors.json"
|
|
config_file.write_text(
|
|
json.dumps(
|
|
{
|
|
"sensors": [
|
|
{"mac": "AA:BB:CC:DD:EE:FF", "name": "Living Room", "comment": "hello"},
|
|
]
|
|
},
|
|
indent=2,
|
|
)
|
|
)
|
|
|
|
sensor_cfg = config.SensorConfig(config_file=str(config_file))
|
|
assert sensor_cfg.get_comment("AA:BB:CC:DD:EE:FF") == "hello"
|
|
|
|
# Clear comment explicitly (empty string means remove comment key)
|
|
sensor_cfg.add_sensor("AA:BB:CC:DD:EE:FF", "Living Room", "")
|
|
assert sensor_cfg.get_comment("AA:BB:CC:DD:EE:FF") is None
|
|
|
|
saved = json.loads(config_file.read_text())
|
|
assert saved["sensors"][0]["mac"] == "AA:BB:CC:DD:EE:FF"
|
|
assert saved["sensors"][0]["name"] == "Living Room"
|
|
assert "comment" not in saved["sensors"][0]
|
|
|
|
def test_sensor_config_add_remove(tmp_path):
|
|
import sensorpajen.config as config
|
|
config_file = tmp_path / "sensors.json"
|
|
# Start with empty
|
|
with open(config_file, "w") as f:
|
|
import json
|
|
json.dump({"sensors": []}, f)
|
|
|
|
sensor_cfg = config.SensorConfig(config_file=str(config_file))
|
|
|
|
# Add
|
|
sensor_cfg.add_sensor("AA:BB:CC:DD:EE:FF", "Living Room", "Test comment")
|
|
assert sensor_cfg.sensors["AA:BB:CC:DD:EE:FF"] == "Living Room"
|
|
assert sensor_cfg.get_comment("AA:BB:CC:DD:EE:FF") == "Test comment"
|
|
|
|
# Verify persistence
|
|
sensor_cfg2 = config.SensorConfig(config_file=str(config_file))
|
|
assert sensor_cfg2.sensors["AA:BB:CC:DD:EE:FF"] == "Living Room"
|
|
assert sensor_cfg2.get_comment("AA:BB:CC:DD:EE:FF") == "Test comment"
|
|
|
|
# Remove
|
|
sensor_cfg.remove_sensor("AA:BB:CC:DD:EE:FF")
|
|
assert "AA:BB:CC:DD:EE:FF" not in sensor_cfg.sensors
|
|
|
|
# Verify persistence
|
|
sensor_cfg3 = config.SensorConfig(config_file=str(config_file))
|
|
assert "AA:BB:CC:DD:EE:FF" not in sensor_cfg3.sensors
|
|
|
|
def test_sensor_config_missing_file(tmp_path):
|
|
import sensorpajen.config as config
|
|
config_file = tmp_path / "nonexistent.json"
|
|
sensor_cfg = config.SensorConfig(config_file=str(config_file))
|
|
assert sensor_cfg.sensors == {}
|