Files
line-laser-hmi/tests/test_recorder.py
T
2026-06-16 10:07:15 +08:00

47 lines
1.2 KiB
Python

"""CSV 录制测试"""
from __future__ import annotations
import csv
from line_laser_modbus.models import DeviceSnapshot, DeviceStatus, ModeCommand, Pose6D
from line_laser_hmi.recorder import CSV_HEADER, SnapshotRecorder
def _snapshot() -> DeviceSnapshot:
"""构造一个测试用快照"""
return DeviceSnapshot(
ModeCommand.ONLINE_TRACKING,
DeviceStatus.TRACKING_OK,
Pose6D(1.0, 2.0, 3.0, 4.0, 5.0, 6.0),
timestamp=1234,
)
def test_record_writes_header_and_rows(tmp_path):
"""录制应写入表头并按快照追加数据行"""
recorder = SnapshotRecorder(tmp_path)
path = recorder.start()
recorder.write(_snapshot())
recorder.write(_snapshot())
assert recorder.rows == 2
recorder.stop()
with open(path, newline="", encoding="utf-8") as file:
rows = list(csv.reader(file))
assert tuple(rows[0]) == CSV_HEADER
assert len(rows) == 3
assert rows[1][2] == "ONLINE_TRACKING"
def test_write_without_start_is_ignored(tmp_path):
"""未开始录制时写入应被忽略"""
recorder = SnapshotRecorder(tmp_path)
recorder.write(_snapshot())
assert recorder.rows == 0
assert recorder.is_recording is False