|
@@ -0,0 +1,61 @@
|
|
|
|
|
+import pytest
|
|
|
|
|
+from unittest.mock import patch, AsyncMock
|
|
|
|
|
+from httpx import AsyncClient, ASGITransport
|
|
|
|
|
+from app.main import app
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_report_parse_nonexistent_file():
|
|
|
|
|
+ transport = ASGITransport(app=app)
|
|
|
|
|
+ async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
|
|
|
+ resp = await client.post("/api/v1/report/parse", json={
|
|
|
|
|
+ "file_path": "/nonexistent.pdf",
|
|
|
|
|
+ "family_id": 1,
|
|
|
|
|
+ "user_id": 1,
|
|
|
|
|
+ })
|
|
|
|
|
+ assert resp.status_code == 400
|
|
|
|
|
+ data = resp.json()
|
|
|
|
|
+ assert "文件不存在" in data["detail"]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_report_parse_with_real_pdf_algorithm_path():
|
|
|
|
|
+ transport = ASGITransport(app=app)
|
|
|
|
|
+ async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
|
|
|
+ resp = await client.post("/api/v1/report/parse", json={
|
|
|
|
|
+ "file_path": "/app/cfc/docs/参考资料/501999942-某人.pdf",
|
|
|
|
|
+ "family_id": 1,
|
|
|
|
|
+ "user_id": 1,
|
|
|
|
|
+ })
|
|
|
|
|
+ assert resp.status_code == 200
|
|
|
|
|
+ data = resp.json()
|
|
|
|
|
+ assert data["code"] == 200
|
|
|
|
|
+ result = data["data"]
|
|
|
|
|
+ assert "format" in result
|
|
|
|
|
+ assert "overview" in result
|
|
|
|
|
+ assert "disease_risks" in result
|
|
|
|
|
+ assert "nutrition" in result
|
|
|
|
|
+ assert "amino_acids" in result
|
|
|
|
|
+ assert isinstance(result["overview"], dict)
|
|
|
|
|
+ assert isinstance(result["disease_risks"], list)
|
|
|
|
|
+ assert isinstance(result["amino_acids"], list)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_report_parse_invalid_json_response():
|
|
|
|
|
+ transport = ASGITransport(app=app)
|
|
|
|
|
+ async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
|
|
|
+ with patch("app.agents.report_parse_agent.ReportParseAgent._parse_with_llm",
|
|
|
|
|
+ new_callable=AsyncMock, return_value=[1, 2, 3]):
|
|
|
|
|
+ with patch("app.agents.report_parse_agent.parse_report_pdf_with_fallback",
|
|
|
|
|
+ return_value={"format": "inline", "overview": {},
|
|
|
|
|
+ "_parse_incomplete": True}):
|
|
|
|
|
+ resp = await client.post("/api/v1/report/parse", json={
|
|
|
|
|
+ "file_path": "/app/cfc/docs/参考资料/501999942-某人.pdf",
|
|
|
|
|
+ "family_id": 1,
|
|
|
|
|
+ "user_id": 1,
|
|
|
|
|
+ })
|
|
|
|
|
+ assert resp.status_code == 200
|
|
|
|
|
+ data = resp.json()
|
|
|
|
|
+ assert data["code"] == 500
|
|
|
|
|
+ assert "解析失败" in data["message"]
|