"""bre 全模块导出冒烟(TestClient)。对 23 个有 /export 的模块逐一 POST,断言 200 + xlsx。 运行: cd d:\dpb\dpb\backend C:\ai\miniconda3\envs\dpb\python.exe -X utf8 scripts/test_bre_export_smoke_tc.py """ import os os.environ["ENVIRONMENT"] = "dev" os.environ["PYTHONUTF8"] = "1" import sys sys.path.insert(0, r"d:\dpb\dpb\backend") import main from fastapi.testclient import TestClient DOMAINS = [ "germplasm", "cross_combination", "target", "trial", "trial_study", "trial_study_entry", "treatment", "group", "pedigree", "plan", "site", "plot", "personnel", "pollination", "seed_treatment", "seedling", "planting", "tree", "tree_evaluation", "trait_observation", "tree_photo", "trait", "selection_rule", "selection_result", ] def login(client): d = {"username": "super", "password": "123456", "grant_type": "password", "login_type": "PC端"} r = client.post("/api/v1/system/auth/login", data=d) b = r.json() if r.status_code == 200 and b.get("code") == 0: return b["data"]["access_token"] key = client.get("/api/v1/system/auth/captcha/get").json()["data"]["key"] client.post("/api/v1/system/auth/captcha/slider/complete", json={"captcha_key": key}) d["captcha_key"] = key r = client.post("/api/v1/system/auth/login", data=d) assert r.status_code == 200 and r.json().get("code") == 0, f"LOGIN FAIL {r.text[:200]}" return r.json()["data"]["access_token"] def main_(): with TestClient(create_app()) as client: token = login(client) headers = {"Authorization": f"Bearer {token}"} ok = 0 for d in DOMAINS: r = client.post(f"/api/v1/bre/{d}/export", headers=headers, params={"page": 1, "page_size": 5}) ct = r.headers.get("content-type", "") good = r.status_code == 200 and "spreadsheetml" in ct and len(r.content) > 100 print(f"[{'OK ' if good else 'FAIL'}] {d}/export -> HTTP {r.status_code} ct={ct[:40]} bytes={len(r.content)}") if not good: print(" BODY:", str(r.text[:300])) sys.exit(1) ok += 1 print(f"\nALL {ok} EXPORT OK ✅") if __name__ == "__main__": create_app = main.create_app main_()