test_xlsx_export.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. """Tests for the plan-results Excel export (TEST-066).
  2. Covers:
  3. 1. afmcore.xlsx_report unit level: column assembly (thermal columns appear
  4. only when thermal metrics are present) and the two-row-header writer.
  5. 2. API level: GET /api/plans/{id}/export-xlsx on a temp SQLite DB via
  6. FastAPI TestClient - verifies xlsx payload, header structure, category
  7. groups, error paths (404 / 400).
  8. Run: python scripts/test_xlsx_export.py (from repo root)
  9. All source is ASCII only.
  10. """
  11. from __future__ import annotations
  12. import os
  13. import sys
  14. import tempfile
  15. from pathlib import Path
  16. REPO_ROOT = Path(__file__).resolve().parents[1]
  17. BACKEND_DIR = REPO_ROOT / "web" / "backend"
  18. SRC_DIR = REPO_ROOT / "src"
  19. for p in (str(BACKEND_DIR), str(SRC_DIR)):
  20. if p not in sys.path:
  21. sys.path.insert(0, p)
  22. # Isolate the DB before any app module is imported.
  23. _tmp_db = tempfile.NamedTemporaryFile(prefix="afm_xlsx_test_", suffix=".db", delete=False)
  24. _tmp_db.close()
  25. os.environ["AFM_DB_PATH"] = _tmp_db.name
  26. FAILURES = []
  27. def check(name: str, cond: bool, detail: str = "") -> None:
  28. tag = "PASS" if cond else "FAIL"
  29. print(f"[{tag}] {name}" + (f" -- {detail}" if detail and not cond else ""))
  30. if not cond:
  31. FAILURES.append(name)
  32. # ---------------------------------------------------------------------------
  33. # Part 1: unit tests for afmcore.xlsx_report
  34. # ---------------------------------------------------------------------------
  35. from afmcore.xlsx_report import ( # noqa: E402
  36. CAT_EM,
  37. CAT_SCAN_INFO,
  38. CAT_THERMAL,
  39. build_export_columns,
  40. write_summary_xlsx,
  41. )
  42. def unit_tests() -> None:
  43. params = ["Airgap", "Magnet_Thickness"]
  44. # Case A: EM-only data (thermal_mode off) - no thermal columns.
  45. keys, cats, headers = build_export_columns(
  46. params, ["tavg_nm", "ripple_pct", "efficiency_pct", "total_losses_w"]
  47. )
  48. check("unit: fixed cols first", keys[:4] == ["run_index", "status", "solve_time_s", "error"])
  49. check("unit: params in scan-info group",
  50. keys[4:6] == params and all(c == CAT_SCAN_INFO for c in cats[4:6]))
  51. check("unit: no thermal columns for EM-only data", CAT_THERMAL not in cats)
  52. check("unit: EM group present", CAT_EM in cats)
  53. check("unit: tavg header", headers[keys.index("tavg_nm")] == "\u5e73\u5747\u8f6c\u77e9[Nm]")
  54. # Case B: thermal data present - thermal group appears after EM.
  55. keys, cats, headers = build_export_columns(
  56. params,
  57. ["tavg_nm", "efficiency_pct", "winding_temp_c", "magnet_temp_c",
  58. "temp_rise_c", "thermal_resistance_k_w"],
  59. {"Airgap": "\u6c14\u9699[mm]"},
  60. )
  61. check("unit: thermal group present", CAT_THERMAL in cats)
  62. check("unit: thermal after EM", cats.index(CAT_THERMAL) > cats.index(CAT_EM))
  63. check("unit: param label used", headers[keys.index("Airgap")] == "\u6c14\u9699[mm]")
  64. check("unit: winding temp header",
  65. headers[keys.index("winding_temp_c")] == "\u7ed5\u7ec4\u6e29\u5ea6[C]")
  66. # Case C: unknown metric key still exported (no data loss).
  67. keys, cats, headers = build_export_columns([], ["tavg_nm", "mystery_metric"])
  68. check("unit: unknown metric kept", "mystery_metric" in keys)
  69. # Writer round-trip: write 2 data rows, read back structure.
  70. out = Path(tempfile.mkdtemp()) / "roundtrip.xlsx"
  71. rows = [
  72. {"run_index": 1, "status": "OK", "solve_time_s": 170.3, "error": "",
  73. "Airgap": 1.2, "tavg_nm": 0.545, "winding_temp_c": 54.86},
  74. {"run_index": 2, "status": "FAILED", "solve_time_s": 0, "error": "boom",
  75. "Airgap": 1.4, "tavg_nm": "", "winding_temp_c": ""},
  76. ]
  77. write_summary_xlsx(out, keys, cats, headers, rows)
  78. import openpyxl
  79. wb = openpyxl.load_workbook(out)
  80. ws = wb.active
  81. check("unit: sheet name", ws.title == "scan_results")
  82. check("unit: row1 is category groups", ws.cell(1, 1).value == CAT_SCAN_INFO)
  83. check("unit: row2 is headers", ws.cell(2, 1).value == "\u5e8f\u53f7")
  84. check("unit: data starts row 3", ws.cell(3, 1).value == 1)
  85. check("unit: failed row error cell", ws.cell(4, 4).value == "boom")
  86. check("unit: freeze panes", ws.freeze_panes == "B3")
  87. check("unit: autofilter", ws.auto_filter.ref == f"A2:{openpyxl.utils.get_column_letter(len(keys))}4")
  88. check("unit: merged category groups", len(ws.merged_cells.ranges) > 0)
  89. # ---------------------------------------------------------------------------
  90. # Part 2: API tests against a temp DB via TestClient
  91. # ---------------------------------------------------------------------------
  92. def api_tests() -> None:
  93. from fastapi.testclient import TestClient
  94. from app.main import app
  95. from app.database import SessionLocal, init_db
  96. from app.models.simulation_plan import SimulationPlan
  97. from app.models.simulation_result import SimulationResult
  98. init_db()
  99. client = TestClient(app)
  100. with SessionLocal() as db:
  101. from app.models.project import Project
  102. project = Project(name="xlsx-export-test-project", topology="SSSR")
  103. db.add(project)
  104. db.commit()
  105. db.refresh(project)
  106. project_pk = project.id
  107. plan = SimulationPlan(
  108. plan_id="SP-XLSX-TEST", name="\u70ed\u4eff\u771f\u5bfc\u51fa\u9a8c\u8bc1",
  109. status="completed", project_id=project_pk,
  110. )
  111. plan.set_plan_dict({
  112. "variables": [
  113. {"name": "Airgap", "name_cn": "\u6c14\u9699", "unit": "mm",
  114. "values": [1.0, 1.2]},
  115. ],
  116. })
  117. db.add(plan)
  118. db.commit()
  119. db.refresh(plan)
  120. plan_pk = plan.id
  121. rows = [
  122. (1, "OK", 178.0, "", {"Airgap": 1.0},
  123. {"tavg_nm": 0.522, "ripple_pct": 2.82, "efficiency_pct": 86.06,
  124. "total_losses_w": 41.9, "winding_temp_c": 52.59,
  125. "magnet_temp_c": 67.61, "temp_rise_c": 27.57,
  126. "thermal_resistance_k_w": 5.657}),
  127. (2, "OK", 181.2, "", {"Airgap": 1.2},
  128. {"tavg_nm": 0.545, "ripple_pct": 2.99, "efficiency_pct": 84.91,
  129. "total_losses_w": 47.1, "winding_temp_c": 54.86,
  130. "magnet_temp_c": 62.74, "temp_rise_c": 29.91,
  131. "thermal_resistance_k_w": 2.058}),
  132. ]
  133. for idx, status, secs, err, params, metrics in rows:
  134. r = SimulationResult(
  135. plan_id=plan_pk, run_index=idx, status=status,
  136. solve_time_s=secs, error_message=err,
  137. )
  138. r.set_params(params)
  139. r.set_metrics(metrics)
  140. db.add(r)
  141. # A second plan with EM-only results (thermal_mode off).
  142. plan2 = SimulationPlan(plan_id="SP-XLSX-EM", name="\u7eaf\u7535\u626b\u63cf", status="completed", project_id=project_pk)
  143. plan2.set_plan_dict({"variables": []})
  144. db.add(plan2)
  145. db.commit()
  146. db.refresh(plan2)
  147. r2 = SimulationResult(plan_id=plan2.id, run_index=1, status="OK",
  148. solve_time_s=170.0)
  149. r2.set_params({})
  150. r2.set_metrics({"tavg_nm": 0.5, "efficiency_pct": 86.0})
  151. db.add(r2)
  152. db.commit()
  153. plan2_pk = plan2.id
  154. # 1) Happy path: thermal plan export.
  155. resp = client.get(f"/api/plans/{plan_pk}/export-xlsx")
  156. check("api: export 200", resp.status_code == 200, f"got {resp.status_code}")
  157. check("api: xlsx content-type",
  158. "spreadsheetml" in resp.headers.get("content-type", ""))
  159. disp = resp.headers.get("content-disposition", "")
  160. check("api: attachment disposition", "attachment" in disp and "filename*=" in disp)
  161. out = Path(tempfile.mkdtemp()) / "api_export.xlsx"
  162. out.write_bytes(resp.content)
  163. import openpyxl
  164. wb = openpyxl.load_workbook(out)
  165. ws = wb.active
  166. headers = [ws.cell(2, c).value for c in range(1, ws.max_column + 1)]
  167. cats = [ws.cell(1, c).value for c in range(1, ws.max_column + 1)]
  168. check("api: thermal headers present",
  169. "\u7ed5\u7ec4\u6e29\u5ea6[C]" in headers and "\u70ed\u963b[K/W]" in headers)
  170. check("api: thermal category present", CAT_THERMAL in cats)
  171. check("api: param header uses Chinese label", "\u6c14\u9699[mm]" in headers)
  172. check("api: two data rows", ws.max_row == 4)
  173. check("api: row3 winding temp", abs(ws.cell(3, headers.index("\u7ed5\u7ec4\u6e29\u5ea6[C]") + 1).value - 52.59) < 1e-6)
  174. # 2) EM-only plan: no thermal columns.
  175. resp2 = client.get(f"/api/plans/{plan2_pk}/export-xlsx")
  176. check("api: EM-only export 200", resp2.status_code == 200)
  177. out2 = Path(tempfile.mkdtemp()) / "api_export_em.xlsx"
  178. out2.write_bytes(resp2.content)
  179. ws2 = openpyxl.load_workbook(out2).active
  180. headers2 = [ws2.cell(2, c).value for c in range(1, ws2.max_column + 1)]
  181. check("api: EM-only has no thermal columns",
  182. "\u7ed5\u7ec4\u6e29\u5ea6[C]" not in headers2 and CAT_THERMAL not in
  183. [ws2.cell(1, c).value for c in range(1, ws2.max_column + 1)])
  184. # 3) Error paths.
  185. check("api: 404 for unknown plan", client.get("/api/plans/99999/export-xlsx").status_code == 404)
  186. with SessionLocal() as db:
  187. empty = SimulationPlan(plan_id="SP-XLSX-EMPTY", name="\u7a7a\u65b9\u6848", status="draft", project_id=project_pk)
  188. empty.set_plan_dict({"variables": []})
  189. db.add(empty)
  190. db.commit()
  191. db.refresh(empty)
  192. empty_pk = empty.id
  193. check("api: 400 when no results",
  194. client.get(f"/api/plans/{empty_pk}/export-xlsx").status_code == 400)
  195. if __name__ == "__main__":
  196. unit_tests()
  197. api_tests()
  198. print()
  199. if FAILURES:
  200. print(f"FAILED: {len(FAILURES)} -> {FAILURES}")
  201. sys.exit(1)
  202. print("ALL PASS")