Files
operating-room-monitor-server/tests/test_voice_audit_repository.py

58 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import annotations
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