| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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"]
- def test_indicator_constraint_contains_names():
- from app.agents.report_parse_agent import _indicator_constraint
- s = _indicator_constraint(["总分", "菌群健康"])
- assert "总分" in s and "菌群健康" in s
- assert _indicator_constraint(None) == ""
- assert _indicator_constraint([]) == ""
- @pytest.mark.asyncio
- async def test_report_parse_accepts_indicators_field():
- 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,
- "indicators": ["总分", "菌群健康"],
- })
- # 字段被接受(非 422 校验错误),仅文件不存在
- assert resp.status_code == 400
- assert "文件不存在" in resp.json()["detail"]
|