TBC/scripts/setup_test_data_api.py
d-robotics 633a797226 AutoFlow v2.0: 合并 Test (Robogo) + data-loop-automation (D-Cloud) 为统一平台
- 16 个业务模块,856 个测试用例,40 个 E2E 测试
- 单一 React 19 前端 + PlatformSwitcher 二合一切换
- Flask 后端支持 scenario/dcloud_module/dcloud_e2e/dcloud_all 四种任务类型
- E2E 测试面板:API 全链路(8条) + UI 点击(3条) + 分步测试(3个)
- SSE 实时日志流 + 飞书告警 + 定时调度
- 完整还原原 ToB 平台 TestPanel E2E 模块
2026-06-23 11:48:29 +08:00

162 lines
5.6 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.

"""预置数据空间测试数据 - 纯 API 版本"""
import json
import time
import httpx
from config.settings import config
from scripts.shared_data import AUTH_STATE_FILE, TEST_FILES, TEST_FOLDERS, get_token
def setup_space_data(client, space_id, space_name):
"""为单个空间预置测试数据"""
print(f'\n 处理空间: "{space_name}" (ID={space_id})')
# 检查现有文件
resp = client.post(
f"/api/cloudDiskApi/files/{space_id}",
json={"path": "", "page": 1, "page_size": 100},
)
existing_files = resp.json().get("data", {}).get("files", [])
existing_names = [f.get("name") for f in existing_files]
print(f" 现有文件数: {len(existing_files)}")
# 创建测试文件夹
for folder_name in TEST_FOLDERS:
if folder_name in existing_names:
print(f" - {folder_name}: 已存在,跳过")
continue
resp = client.post(
f"/api/cloudDiskApi/files/{space_id}/folder",
json={"name": folder_name, "path": ""},
)
result = resp.json()
if result.get("status") == 0:
print(f" - {folder_name}: 创建成功 ✓")
else:
print(f" - {folder_name}: 创建失败 ✗ - {result.get('message', result)}")
# 上传测试文件
for file_info in TEST_FILES:
file_name = file_info["name"]
content = file_info["content"]
if file_name in existing_names:
print(f" - {file_name}: 已存在,跳过")
continue
# 上传文件 (multipart/form-data)
files = {"file": (file_name, content.encode("utf-8"), "text/plain")}
data = {"path": ""}
resp = client.post(
f"/api/cloudDiskApi/files/{space_id}/upload",
files=files,
data=data,
)
result = resp.json()
if result.get("status") == 0:
print(f" - {file_name}: 上传成功 ✓")
else:
print(f" - {file_name}: 上传失败 ✗ - {result.get('message', result)}")
# 验证最终状态
resp = client.post(
f"/api/cloudDiskApi/files/{space_id}",
json={"path": "", "page": 1, "page_size": 100},
)
final_files = resp.json().get("data", {}).get("files", [])
# 统计文件和文件夹
files_count = sum(1 for f in final_files if not f.get("is_dir"))
folders_count = sum(1 for f in final_files if f.get("is_dir"))
print(f" 最终: {files_count} 个文件, {folders_count} 个文件夹")
return files_count > 0
def main():
print("=" * 60)
print("数据空间测试数据预置脚本")
print("=" * 60)
token = get_token()
if not token:
print("错误: 无法获取 token")
return
print(f"Token: {token[:30]}...")
headers = {"Authorization": token}
base_url = config.BASE_URL.rstrip("/")
with httpx.Client(base_url=base_url, headers=headers, timeout=30) as client:
# 1. 获取空间列表
print("\n[1/3] 获取空间列表...")
resp = client.post(
"/api/cloudDiskApi/spaces/list", json={"page": 1, "page_size": 100}
)
data = resp.json()
spaces = data.get("data", {}).get("list", [])
personal_spaces = []
for space in spaces:
name = space.get("name", "")
space_id = space.get("id")
space_type = space.get("space_type")
is_public = space.get("is_public", False)
print(
f' - ID={space_id}, name="{name}", type={space_type}, is_public={is_public}'
)
# 个人空间通常是 space_type=None 且不是共享空间
if space_type is None and not is_public:
personal_spaces.append({"id": space_id, "name": name})
print(f"\n 个人空间数量: {len(personal_spaces)}")
if not personal_spaces:
print(" 错误: 没有找到个人空间!")
return
# 2. 为每个个人空间预置测试数据
print("\n[2/3] 为所有个人空间预置测试数据...")
success_count = 0
for ps in personal_spaces:
if setup_space_data(client, ps["id"], ps["name"]):
success_count += 1
# 3. 如果个人空间少于2个,创建一个新空间
if len(personal_spaces) < 2:
print("\n[3/3] 创建第二个测试空间...")
new_space_name = f"test_space_{int(time.time())}"
resp = client.post(
"/api/cloudDiskApi/spaces",
json={"name": new_space_name, "space_type": None, "is_public": False},
)
result = resp.json()
if result.get("status") == 0:
new_space_id = result.get("data", {}).get("id")
print(f' 创建成功: "{new_space_name}" (ID={new_space_id})')
# 为新空间也添加测试数据
setup_space_data(client, new_space_id, new_space_name)
personal_spaces.append({"id": new_space_id, "name": new_space_name})
else:
print(f" 创建失败: {result.get('message', result)}")
else:
print("\n[3/3] 已有足够的个人空间,跳过创建")
# 汇总
print("\n" + "=" * 60)
print("预置完成汇总:")
print(f" - 个人空间数: {len(personal_spaces)}")
print(f" - 有测试数据的空间: {success_count}")
print(f" - 测试文件夹: {TEST_FOLDERS}")
print(f" - 测试文件: {[f['name'] for f in TEST_FILES]}")
print("=" * 60)
if __name__ == "__main__":
main()