178 lines
5.4 KiB
Python
178 lines
5.4 KiB
Python
"""FastAPI 接口测试(使用临时曲库,跳过音频渲染以加速)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
_TMP_DIR = Path(tempfile.mkdtemp(prefix="qbh_api_test_"))
|
|
os.environ["QBH_DB"] = str(_TMP_DIR / "test.db")
|
|
os.environ["QBH_AUDIO_DIR"] = str(_TMP_DIR / "audio")
|
|
os.environ["QBH_SKIP_AUDIO"] = "1"
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
from app.main import app # noqa: E402
|
|
from qbh.audio import encode_wav_bytes # noqa: E402
|
|
from qbh.synth import render_melody # noqa: E402
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def client():
|
|
with TestClient(app) as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def query_wav(client):
|
|
"""用后端自己的曲库造一条可检索的查询音频。"""
|
|
from app.main import state
|
|
|
|
melody = state.library.melodies()[0]
|
|
audio = render_melody(melody.transpose(3), 16000, timbre="hum", seed=31)
|
|
return encode_wav_bytes(audio, 16000), melody.song_id
|
|
|
|
|
|
def test_status(client):
|
|
data = client.get("/api/status").json()
|
|
assert data["n_songs"] > 0
|
|
assert data["n_grams"] > 0
|
|
assert "config" in data
|
|
|
|
|
|
def test_songs_list(client):
|
|
data = client.get("/api/songs").json()
|
|
assert len(data["songs"]) > 0
|
|
song = data["songs"][0]
|
|
for key in ("id", "title", "n_notes", "source", "tags"):
|
|
assert key in song
|
|
|
|
|
|
def test_song_detail(client):
|
|
song_id = client.get("/api/songs").json()["songs"][0]["id"]
|
|
data = client.get(f"/api/songs/{song_id}").json()
|
|
assert data["song"]["id"] == song_id
|
|
assert data["melody"]["midis"]
|
|
assert data["contour"] == data["melody"]["contour"] if "contour" in data["melody"] else True
|
|
|
|
|
|
def test_song_detail_404(client):
|
|
assert client.get("/api/songs/does-not-exist").status_code == 404
|
|
|
|
|
|
def test_index_page(client):
|
|
response = client.get("/")
|
|
assert response.status_code == 200
|
|
assert "基于旋律的音乐哼唱检索系统" in response.text
|
|
|
|
|
|
def test_static_assets(client):
|
|
for asset in ("/static/app.js", "/static/style.css"):
|
|
assert client.get(asset).status_code == 200
|
|
|
|
|
|
def test_search_multipart(client, query_wav):
|
|
blob, song_id = query_wav
|
|
response = client.post(
|
|
"/api/search",
|
|
files={"file": ("query.wav", blob, "audio/wav")},
|
|
data={"top_k": "5"},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
data = response.json()
|
|
assert data["hits"]
|
|
assert data["query"]["n_notes"] > 0
|
|
assert data["elapsed_ms"] >= 0
|
|
assert any(hit["song_id"] == song_id for hit in data["hits"])
|
|
|
|
|
|
def test_search_base64(client, query_wav):
|
|
blob, _ = query_wav
|
|
response = client.post(
|
|
"/api/search",
|
|
json={"audio_base64": base64.b64encode(blob).decode("ascii"), "top_k": 3},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert len(response.json()["hits"]) <= 3
|
|
|
|
|
|
def test_search_rejects_garbage(client):
|
|
response = client.post(
|
|
"/api/search",
|
|
files={"file": ("bad.wav", b"not audio at all", "audio/wav")},
|
|
)
|
|
assert response.status_code == 400
|
|
|
|
|
|
def test_demo_endpoint(client):
|
|
response = client.post("/api/demo", json={"difficulty": "easy", "seed": 123, "top_k": 5})
|
|
assert response.status_code == 200, response.text
|
|
data = response.json()
|
|
assert data["hits"]
|
|
assert data["demo"]["answer"]
|
|
assert data["demo"]["qid"]
|
|
assert data["track"]["voiced_ratio"] > 0
|
|
|
|
audio = client.get(data["demo"]["audio_url"])
|
|
assert audio.status_code == 200
|
|
assert audio.headers["content-type"] == "audio/wav"
|
|
|
|
|
|
def test_auth_flow(client):
|
|
assert client.get("/api/auth/me").json()["logged_in"] is False
|
|
|
|
response = client.post(
|
|
"/api/auth/register", json={"username": "tester", "password": "secret123"}
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert client.get("/api/auth/me").json()["logged_in"] is True
|
|
|
|
# 重复注册
|
|
response = client.post(
|
|
"/api/auth/register", json={"username": "tester", "password": "secret123"}
|
|
)
|
|
assert response.status_code == 400
|
|
|
|
assert client.post("/api/auth/logout").status_code == 200
|
|
assert client.get("/api/auth/me").json()["logged_in"] is False
|
|
|
|
response = client.post(
|
|
"/api/auth/login", json={"username": "tester", "password": "secret123"}
|
|
)
|
|
assert response.status_code == 200
|
|
response = client.post("/api/auth/login", json={"username": "tester", "password": "bad"})
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_history_endpoint(client):
|
|
client.post("/api/demo", json={"difficulty": "easy", "seed": 55, "top_k": 3})
|
|
data = client.get("/api/history?limit=5").json()
|
|
assert "history" in data
|
|
assert len(data["history"]) > 0
|
|
assert "top_song" in data["history"][0]
|
|
|
|
|
|
def test_library_rebuild(client):
|
|
response = client.post("/api/library/rebuild")
|
|
assert response.status_code == 200
|
|
assert response.json()["ok"] is True
|
|
assert client.get("/api/status").json()["n_songs"] > 0
|
|
|
|
|
|
def test_eval_run(client):
|
|
response = client.post(
|
|
"/api/eval/run", json={"difficulty": "easy", "per_song": 1, "seed": 1, "top_k": 5}
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
report = response.json()["report"]
|
|
assert report["n_queries"] > 0
|
|
assert 0.0 <= report["top1"] <= 1.0
|
|
|
|
latest = client.get("/api/eval/latest").json()
|
|
assert latest["available"] is True
|