Files
operating-room-monitor-server/tests/test_voice_audit_repository.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

106 lines
3.4 KiB
Python

from __future__ import annotations
import asyncio
import json
import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
import app.db.models # noqa: F401
from app.db.base import Base
from app.db.models import VoiceConfirmationAudit
from app.repositories.voice_audits import VoiceAuditRepository
@pytest.fixture
async def db_session() -> AsyncSession:
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
session = factory()
yield session
await session.close()
await engine.dispose()
@pytest.mark.asyncio
async def test_save_audit_persists_fields(db_session: AsyncSession) -> None:
repo = VoiceAuditRepository()
opts = json.dumps([{"label": "纱布", "confidence": 0.4}], ensure_ascii=False)
async with db_session.begin():
await repo.save_audit(
db_session,
surgery_id="123456",
confirmation_id="cid-1",
status="recognized",
audio_object_key="surgeries/123456/x.wav",
audio_content_type="audio/wav",
audio_size_bytes=100,
audio_sha256="a" * 64,
asr_text="纱布",
resolved_label="纱布",
options_snapshot_json=opts,
error_message=None,
)
async with db_session.begin():
res = await db_session.execute(select(VoiceConfirmationAudit))
rows = res.scalars().all()
assert len(rows) == 1
r = rows[0]
assert r.surgery_id == "123456"
assert r.confirmation_id == "cid-1"
assert r.status == "recognized"
assert r.asr_text == "纱布"
assert r.resolved_label == "纱布"
assert r.options_snapshot_json == opts
assert r.error_message is None
@pytest.mark.asyncio
async def test_list_by_surgery_order_and_total(db_session: AsyncSession) -> None:
repo = VoiceAuditRepository()
async with db_session.begin():
await repo.save_audit(
db_session,
surgery_id="111111",
confirmation_id="a",
status="parse_failed",
audio_object_key=None,
audio_content_type=None,
audio_size_bytes=None,
audio_sha256=None,
asr_text="",
resolved_label=None,
options_snapshot_json="[]",
error_message="x",
)
await asyncio.sleep(0.02)
async with db_session.begin():
await repo.save_audit(
db_session,
surgery_id="111111",
confirmation_id="b",
status="recognized",
audio_object_key="k.wav",
audio_content_type="audio/wav",
audio_size_bytes=10,
audio_sha256="b" * 64,
asr_text="纱布",
resolved_label="纱布",
options_snapshot_json="[]",
error_message=None,
)
async with db_session.begin():
rows, total = await repo.list_by_surgery(db_session, "111111", limit=10, offset=0)
assert total == 2
assert [r.confirmation_id for r in rows] == ["b", "a"]
async with db_session.begin():
page2, total2 = await repo.list_by_surgery(
db_session, "111111", limit=1, offset=1
)
assert total2 == 2
assert len(page2) == 1
assert page2[0].confirmation_id == "a"