test_api.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """Quick API test for backend."""
  2. import json
  3. import urllib.request
  4. BASE = "http://127.0.0.1:8000"
  5. def req(method, path, data=None):
  6. url = BASE + path
  7. body = json.dumps(data).encode() if data else None
  8. r = urllib.request.Request(url, data=body, method=method)
  9. r.add_header("Content-Type", "application/json")
  10. try:
  11. with urllib.request.urlopen(r) as resp:
  12. return json.loads(resp.read())
  13. except urllib.error.HTTPError as e:
  14. return {"error": e.code, "body": e.read().decode()}
  15. # 1. Health
  16. print("=== Health ===")
  17. print(req("GET", "/api/health"))
  18. # 2. Create project
  19. print("\n=== Create Project ===")
  20. proj = req("POST", "/api/projects", {
  21. "name": "MARS Airgap Optimization",
  22. "topology": "SSSR",
  23. "description": "Test project for P2-M1 API validation",
  24. "model_path": "models/MARS-12S10P_SSSR_D76-C150_V5.0-0819.mot",
  25. "boundary_conditions": {"outer_diameter_mm": 76, "speed_rpm": 5000, "current_a": 21},
  26. })
  27. print(json.dumps(proj, indent=2))
  28. proj_id = proj.get("id")
  29. # 3. List projects
  30. print("\n=== List Projects ===")
  31. print(req("GET", "/api/projects"))
  32. # 4. Create plan
  33. print("\n=== Create Plan ===")
  34. plan_data = {
  35. "plan_id": "SP-TEST-001",
  36. "model_path": "models/MARS-12S10P_SSSR_D76-C150_V5.0-0819.mot",
  37. "topology": "SSSR",
  38. "variables": [
  39. {"name": "Airgap", "display_name": "Airgap", "unit": "mm", "values": [0.6, 1.0, 1.5]}
  40. ],
  41. "cases": [],
  42. }
  43. plan = req("POST", "/api/plans", {
  44. "project_id": proj_id,
  45. "name": "Airgap Scan 0.6-1.5mm",
  46. "plan_data": plan_data,
  47. "notes": "Test plan for API validation",
  48. })
  49. print(json.dumps(plan, indent=2))
  50. plan_id = plan.get("id")
  51. plan_uuid = plan.get("plan_id")
  52. # 5. Download plan
  53. print("\n=== Download Plan ===")
  54. dl = req("GET", f"/api/plans/{plan_id}/download")
  55. print(f"plan_id: {dl.get('plan_id')}")
  56. print(f"variables: {dl.get('plan_data', {}).get('variables')}")
  57. # 6. Download by UUID
  58. print("\n=== Download by UUID ===")
  59. dl2 = req("GET", f"/api/plans/by-plan-id/{plan_uuid}/download")
  60. print(f"plan_id: {dl2.get('plan_id')}")
  61. # 7. List plans
  62. print("\n=== List Plans ===")
  63. plans = req("GET", f"/api/plans?project_id={proj_id}")
  64. print(f"total: {plans.get('total')}")
  65. print("\n=== ALL API TESTS PASSED ===")