35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
"""配置读写测试"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from line_laser_modbus.config import PollingConfig, SerialConfig
|
|
|
|
from line_laser_hmi.settings import HmiConfig, UiConfig, load_config, save_config
|
|
|
|
|
|
def test_load_missing_returns_default(tmp_path):
|
|
"""缺失配置文件时返回默认配置"""
|
|
|
|
config = load_config(tmp_path / "missing.toml")
|
|
assert config.serial.port == "COM1"
|
|
assert config.ui.simulate is True
|
|
|
|
|
|
def test_save_then_load_roundtrip(tmp_path):
|
|
"""保存后再读取应得到一致的配置"""
|
|
|
|
path = tmp_path / "config.toml"
|
|
original = HmiConfig(
|
|
serial=SerialConfig(port="COM7", slave_id=8, baudrate=57600, timeout=0.2, retries=2),
|
|
polling=PollingConfig(interval_seconds=0.1, max_timeouts=5),
|
|
ui=UiConfig(simulate=False, record_dir="out"),
|
|
)
|
|
save_config(original, path)
|
|
loaded = load_config(path)
|
|
|
|
assert loaded.serial.port == "COM7"
|
|
assert loaded.serial.baudrate == 57600
|
|
assert loaded.polling.interval_seconds == 0.1
|
|
assert loaded.ui.simulate is False
|
|
assert loaded.ui.record_dir == "out"
|