Files
soul-yongping/scripts/test/process/cleanup_test_data.py

76 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
清理流程测试产生的数据persons测试自动创建_测试新人物_chapterst 开头的 6 位数字 id
用法cd scripts/test && python process/cleanup_test_data.py
"""
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import requests
API_BASE = os.environ.get("SOUL_API_BASE", "http://localhost:8080").rstrip("/")
ADMIN_USER = os.environ.get("SOUL_ADMIN_USERNAME", "admin")
ADMIN_PASS = os.environ.get("SOUL_ADMIN_PASSWORD", "admin123")
def main():
r = requests.post(
f"{API_BASE}/api/admin",
json={"username": ADMIN_USER, "password": ADMIN_PASS},
timeout=10,
)
if not r.json().get("success") or not r.json().get("token"):
print("登录失败")
return 1
token = r.json()["token"]
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
# 1. 删除测试 Person
rp = requests.get(f"{API_BASE}/api/db/persons", headers=headers, timeout=10)
if rp.json().get("success"):
persons = rp.json().get("persons") or []
for p in persons:
name = p.get("name") or ""
if name.startswith("测试自动创建_") or name.startswith("测试新人物_"):
pid = p.get("personId")
rd = requests.delete(
f"{API_BASE}/api/db/persons?personId={pid}",
headers=headers,
timeout=10,
)
if rd.json().get("success"):
print(f" 已删除 Person: {name} ({pid})")
else:
print(f" 删除 Person 失败: {name} - {rd.json()}")
# 2. 删除测试 Chapterid 形如 t123456
rl = requests.get(
f"{API_BASE}/api/db/book?action=list",
headers=headers,
timeout=10,
)
if rl.json().get("success"):
sections = rl.json().get("sections") or []
for s in sections:
sid = s.get("id") or ""
if re.match(r"^t\d{6}$", sid):
rd = requests.delete(
f"{API_BASE}/api/db/book?id={sid}",
headers=headers,
timeout=10,
)
if rd.json().get("success"):
print(f" 已删除 Chapter: {sid} ({s.get('title', '')})")
else:
print(f" 删除 Chapter 失败: {sid} - {rd.json()}")
print("清理完成")
return 0
if __name__ == "__main__":
sys.exit(main())