Update asr library preview
This commit is contained in:
@@ -287,3 +287,61 @@ class TestASRModelAPI:
|
||||
response = client.post("/api/asr", json=data)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["vendor"] == vendor
|
||||
|
||||
def test_preview_asr_model_success(self, client, sample_asr_model_data, monkeypatch):
|
||||
"""Test ASR preview endpoint with OpenAI-compatible transcriptions API."""
|
||||
from app.routers import asr as asr_router
|
||||
|
||||
create_response = client.post("/api/asr", json=sample_asr_model_data)
|
||||
model_id = create_response.json()["id"]
|
||||
|
||||
class DummyResponse:
|
||||
status_code = 200
|
||||
|
||||
def json(self):
|
||||
return {"text": "你好,这是测试转写", "language": "zh", "confidence": 0.98}
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
return '{"text":"ok"}'
|
||||
|
||||
class DummyClient:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
def post(self, url, headers=None, data=None, files=None):
|
||||
assert url.endswith("/audio/transcriptions")
|
||||
assert headers["Authorization"] == f"Bearer {sample_asr_model_data['api_key']}"
|
||||
assert data["model"] == sample_asr_model_data["model_name"]
|
||||
assert files["file"][0] == "sample.wav"
|
||||
return DummyResponse()
|
||||
|
||||
monkeypatch.setattr(asr_router.httpx, "Client", DummyClient)
|
||||
|
||||
response = client.post(
|
||||
f"/api/asr/{model_id}/preview",
|
||||
files={"file": ("sample.wav", b"fake-wav-bytes", "audio/wav")},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["success"] is True
|
||||
assert payload["transcript"] == "你好,这是测试转写"
|
||||
assert payload["language"] == "zh"
|
||||
|
||||
def test_preview_asr_model_reject_non_audio(self, client, sample_asr_model_data):
|
||||
"""Test ASR preview endpoint rejects non-audio file."""
|
||||
create_response = client.post("/api/asr", json=sample_asr_model_data)
|
||||
model_id = create_response.json()["id"]
|
||||
|
||||
response = client.post(
|
||||
f"/api/asr/{model_id}/preview",
|
||||
files={"file": ("sample.txt", b"text-data", "text/plain")},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert "Only audio files are supported" in response.text
|
||||
|
||||
Reference in New Issue
Block a user