Files
soul-yongping/scripts/test/process/cleanup_test_data.py
卡若 76965adb23 chore: 清理敏感与开发文档,仅同步代码
- 永久忽略并从仓库移除 开发文档/
- 移除并忽略 .env 与小程序私有配置
- 同步小程序/管理端/API与脚本改动

Made-with: Cursor
2026-03-17 17:50:12 +08:00

76 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- 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())