test_health_plan.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from app.models.health_plan import PlanTask, HealthPlanSection, HealthPlanResponse
  2. def test_plan_task_valid():
  3. t = PlanTask(action_type="buy", title="购买维生素D3", dimension="wealth", frequency="once")
  4. assert t.frequency == "once"
  5. assert t.notes is None
  6. def test_plan_task_default_frequency_daily():
  7. t = PlanTask(action_type="exercise", title="每周跑步3次", dimension="body")
  8. assert t.frequency == "daily"
  9. def test_plan_task_invalid_action_type():
  10. import pytest
  11. from pydantic import ValidationError
  12. with pytest.raises(ValidationError):
  13. PlanTask(action_type="cook", title="做饭", dimension="body")
  14. def test_plan_task_invalid_dimension():
  15. import pytest
  16. from pydantic import ValidationError
  17. with pytest.raises(ValidationError):
  18. PlanTask(action_type="diet", title="少油少盐", dimension="earth")
  19. def test_plan_response_with_tasks():
  20. resp = HealthPlanResponse(
  21. overview="总览",
  22. sections=[HealthPlanSection(
  23. key="nutrition", title="营养补充", content="## 建议",
  24. tasks=[PlanTask(action_type="buy", title="购买鱼油", dimension="wealth", frequency="once")]
  25. )]
  26. )
  27. assert resp.sections[0].tasks[0].action_type == "buy"
  28. # 序列化后 tasks 字段存在
  29. data = resp.model_dump()
  30. assert "tasks" in data["sections"][0]