فهرست منبع

test: 端到端测试 — report_parse API + ReportParseAgent + LLM非dict防护

Xiaogang Liao 3 ماه پیش
والد
کامیت
656cce5a9d
1فایلهای تغییر یافته به همراه61 افزوده شده و 0 حذف شده
  1. 61 0
      cfc-langgraph/tests/test_report_parse.py

+ 61 - 0
cfc-langgraph/tests/test_report_parse.py

@@ -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"]