Files
operating-room-monitor-server/tests/test_consumption_tsv_log.py
Kevin 0c05463617 feat: 语音确认、联调与运维增强
- 语音:序数解析(第一个/第二个等)、解析失败计数与 API detail.retry_remaining;
  百度 ASR 固定 dev_pid 为普通话;SurgeryPipelineError 支持 extra 并入 HTTP detail。
- Demo:demo 路由与假 RTSP、客户端 index 与 README;BackendResolver 与配置调整。
- 可观测:消耗 TSV 日志、语音文件日志、终端 Markdown 辅助;相关测试与依赖更新。
- 注意:.env 仍被 gitignore,本地密钥不会进入本提交。

Made-with: Cursor
2026-04-23 14:24:20 +08:00

124 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""consumption_log.txt 兼容 TSV 格式。"""
import pytest
from app.config import settings
from app.services.consumable_vision_algorithm import ClsTop3
from app.services.consumption_tsv_log import (
HEADER,
_RANGE_SEP,
append_consumption_tsv_line,
build_consumption_markdown,
build_tsv_line,
init_consumption_log_file,
short_camera_label,
)
def test_short_camera_label() -> None:
assert short_camera_label("or-cam-01") == "cam01"
assert short_camera_label("or-cam-2") == "cam02"
def test_build_tsv_line_matches_sample_shape(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(settings, "consumption_log_timezone", "UTC")
best = ClsTop3(
t1_name="一次性医用灭菌棉签",
t1_conf=0.9997,
t2_name="cls2",
t2_conf=0.0003,
t3_name="cls3",
t3_conf=0.0002,
t1_pid="2237844",
t2_pid="11765-1-101",
t3_pid="21504-1-1",
)
# 墙钟:拉流起点对齐到 2024-01-01T00:00:00Z时间窗 +0s…+45s
w0 = 1704067200.0
line = build_tsv_line(
name_to_code={},
best=best,
doctor_id="DOCTOR_PLACEHOLDER",
camera_id="or-cam-01",
wall_start_epoch=w0,
wall_end_epoch=w0 + 45.0,
)
parts = line.rstrip("\n").split("\t")
assert len(parts) == 7
assert parts[0] == "2237844"
assert parts[1] == "一次性医用灭菌棉签 0.9997"
assert parts[2] == "cls2"
assert parts[3] == "cls3"
assert parts[4] == "1"
assert parts[5] == "DOCTOR_PLACEHOLDER"
assert (
parts[6]
== "cam01@2024-01-01T00:00:00.000+00:00"
+ _RANGE_SEP
+ "2024-01-01T00:00:45.000+00:00"
)
def test_header_columns() -> None:
cols = HEADER.strip().split("\t")
assert cols[0] == "物品id"
assert cols[-1] == "时间戳"
def test_per_surgery_file_init_and_append(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(settings, "consumption_tsv_log_enabled", True)
monkeypatch.setattr(
settings,
"consumption_tsv_log_path",
str(tmp_path / "{surgery_id}.txt"),
)
init_consumption_log_file("or-001")
append_consumption_tsv_line("or-001", "row1\n")
append_consumption_tsv_line("or-001", "row2\n")
p = tmp_path / "or-001.txt"
assert p.read_text(encoding="utf-8") == HEADER + "row1\n" + "row2\n"
init_consumption_log_file("or-001")
assert p.read_text(encoding="utf-8") == HEADER
def test_build_consumption_markdown_top123_columns(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(settings, "consumption_log_timezone", "UTC")
best = ClsTop3(
t1_name="一次性医用灭菌棉签",
t1_conf=0.9997,
t2_name="cls2",
t2_conf=0.0003,
t3_name="cls3",
t3_conf=0.0002,
t1_pid="2237844",
t2_pid="11765-1-101",
t3_pid="21504-1-1",
)
w0 = 1704067200.0
md = build_consumption_markdown(
name_to_code={},
best=best,
doctor_id="DOCTOR_PLACEHOLDER",
camera_id="or-cam-01",
wall_start_epoch=w0,
wall_end_epoch=w0 + 45.0,
)
assert "Top1 物品id" in md and "Top1 物品名称" in md and "Top1 置信度" in md
assert "Top2 物品名称" in md and "Top3 物品名称" in md
assert "Top2 物品id" not in md
assert "2237844" in md
assert "一次性医用灭菌棉签" in md
assert "0.9997" in md
assert "cls2" in md and "cls3" in md
assert "11765-1-101" not in md and "21504-1-1" not in md
assert "0.0003" not in md and "0.0002" not in md
assert "DOCTOR_PLACEHOLDER" in md
assert "| 1 |" in md
# 终端为可读时间戳,非落盘用 ISO@cam
assert "2024-01-01 00:00:00.000" in md and "2024-01-01 00:00:45.000" in md
assert "cam01" in md and " · " in md and _RANGE_SEP in md
assert "cam01@2024-01" not in md