68 lines
2.2 KiB
Python
68 lines
2.2 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_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"
|
|
|
|
# Verify persistence
|
|
sensor_cfg2 = config.SensorConfig(config_file=str(config_file))
|
|
assert sensor_cfg2.sensors["AA:BB:CC:DD:EE:FF"] == "Living Room"
|
|
|
|
# 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 == {}
|