66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
"""Unit tests for configuration schema validation and env override precedence."""
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from config.loader import load_config
|
|
from config.schema import MQTTConfig
|
|
|
|
|
|
@pytest.fixture
|
|
def minimal_config_yaml(tmp_path):
|
|
config_path = tmp_path / "config.yaml"
|
|
config_path.write_text(
|
|
"""
|
|
mqtt:
|
|
broker: yaml-broker
|
|
port: 1883
|
|
client_id: yaml-client
|
|
odoo:
|
|
base_url: http://yaml-odoo:8069
|
|
database: yaml-db
|
|
username: yaml-user
|
|
api_key: yaml-key
|
|
devices:
|
|
- device_id: machine-1
|
|
mqtt_topic: shelly/machine-1/status
|
|
parser_type: shelly_pm
|
|
machine_name: Machine 1
|
|
session_config:
|
|
standby_threshold_w: 5
|
|
working_threshold_w: 20
|
|
start_debounce_s: 3
|
|
stop_debounce_s: 15
|
|
message_timeout_s: 20
|
|
heartbeat_interval_s: 60
|
|
""".strip()
|
|
)
|
|
return config_path
|
|
|
|
|
|
def test_load_config_respects_env_overrides(minimal_config_yaml, monkeypatch):
|
|
monkeypatch.setenv("MQTT_BROKER", "env-broker")
|
|
monkeypatch.setenv("MQTT_PORT", "1884")
|
|
monkeypatch.setenv("ODOO_BASE_URL", "http://env-odoo:8069")
|
|
monkeypatch.setenv("ODOO_DATABASE", "env-db")
|
|
|
|
config = load_config(str(minimal_config_yaml))
|
|
|
|
assert config.mqtt.broker == "env-broker"
|
|
assert config.mqtt.port == 1884
|
|
assert config.odoo.base_url == "http://env-odoo:8069"
|
|
assert config.odoo.database == "env-db"
|
|
|
|
|
|
def test_load_config_uses_yaml_when_env_not_set(minimal_config_yaml):
|
|
config = load_config(str(minimal_config_yaml))
|
|
|
|
assert config.mqtt.broker == "yaml-broker"
|
|
assert config.mqtt.port == 1883
|
|
assert config.odoo.base_url == "http://yaml-odoo:8069"
|
|
|
|
|
|
def test_mqtt_config_validates_port_range():
|
|
with pytest.raises(ValidationError):
|
|
MQTTConfig(port=70000)
|