Files
dpb/backend/scripts/test_bre_export_smoke_tc.py
34047007@qq.com b95053c52c init: 初始化 dpb 桃育种系统代码库
前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
2026-08-06 00:17:49 +08:00

60 lines
2.2 KiB
Python

"""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_()