test_report_parse.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import pytest
  2. from unittest.mock import patch, AsyncMock
  3. from httpx import AsyncClient, ASGITransport
  4. from app.main import app
  5. @pytest.mark.asyncio
  6. async def test_report_parse_nonexistent_file():
  7. transport = ASGITransport(app=app)
  8. async with AsyncClient(transport=transport, base_url="http://test") as client:
  9. resp = await client.post("/api/v1/report/parse", json={
  10. "file_path": "/nonexistent.pdf",
  11. "family_id": 1,
  12. "user_id": 1,
  13. })
  14. assert resp.status_code == 400
  15. data = resp.json()
  16. assert "文件不存在" in data["detail"]
  17. @pytest.mark.asyncio
  18. async def test_report_parse_with_real_pdf_algorithm_path():
  19. transport = ASGITransport(app=app)
  20. async with AsyncClient(transport=transport, base_url="http://test") as client:
  21. resp = await client.post("/api/v1/report/parse", json={
  22. "file_path": "/app/cfc/docs/参考资料/501999942-某人.pdf",
  23. "family_id": 1,
  24. "user_id": 1,
  25. })
  26. assert resp.status_code == 200
  27. data = resp.json()
  28. assert data["code"] == 200
  29. result = data["data"]
  30. assert "format" in result
  31. assert "overview" in result
  32. assert "disease_risks" in result
  33. assert "nutrition" in result
  34. assert "amino_acids" in result
  35. assert isinstance(result["overview"], dict)
  36. assert isinstance(result["disease_risks"], list)
  37. assert isinstance(result["amino_acids"], list)
  38. @pytest.mark.asyncio
  39. async def test_report_parse_invalid_json_response():
  40. transport = ASGITransport(app=app)
  41. async with AsyncClient(transport=transport, base_url="http://test") as client:
  42. with patch("app.agents.report_parse_agent.ReportParseAgent._parse_with_llm",
  43. new_callable=AsyncMock, return_value=[1, 2, 3]):
  44. with patch("app.agents.report_parse_agent.parse_report_pdf_with_fallback",
  45. return_value={"format": "inline", "overview": {},
  46. "_parse_incomplete": True}):
  47. resp = await client.post("/api/v1/report/parse", json={
  48. "file_path": "/app/cfc/docs/参考资料/501999942-某人.pdf",
  49. "family_id": 1,
  50. "user_id": 1,
  51. })
  52. assert resp.status_code == 200
  53. data = resp.json()
  54. assert data["code"] == 500
  55. assert "解析失败" in data["message"]