""" 测试标注功能脚本 快速测试标注平台的完整流程。 """ import requests import json BASE_URL = "http://localhost:8000" def test_complete_workflow(): """测试完整的标注工作流程""" print("=" * 60) print("测试标注平台完整工作流程") print("=" * 60) print() # 1. 创建项目 print("1. 创建测试项目...") project_data = { "name": "测试项目", "description": "用于测试的简单文本分类项目", "config": """ """ } response = requests.post(f"{BASE_URL}/api/projects", json=project_data) if response.status_code != 201: print(f"✗ 创建项目失败: {response.text}") return project = response.json() print(f"✓ 项目创建成功: {project['id']}") print() # 2. 创建任务 print("2. 创建测试任务...") task_data = { "project_id": project['id'], "name": "测试任务", "data": { "text": "这是一个测试文本" } } response = requests.post(f"{BASE_URL}/api/tasks", json=task_data) if response.status_code != 201: print(f"✗ 创建任务失败: {response.text}") return task = response.json() print(f"✓ 任务创建成功: {task['id']}") print() # 3. 模拟标注 print("3. 创建标注...") annotation_data = { "task_id": task['id'], "user_id": "test_user", "result": { "label": "类别A" } } response = requests.post(f"{BASE_URL}/api/annotations", json=annotation_data) if response.status_code != 201: print(f"✗ 创建标注失败: {response.text}") return annotation = response.json() print(f"✓ 标注创建成功: {annotation['id']}") print() # 4. 查询标注 print("4. 查询标注结果...") response = requests.get(f"{BASE_URL}/api/annotations/{annotation['id']}") if response.status_code != 200: print(f"✗ 查询标注失败: {response.text}") return result = response.json() print(f"✓ 标注结果: {json.dumps(result, indent=2, ensure_ascii=False)}") print() # 5. 更新任务状态 print("5. 更新任务状态...") response = requests.put( f"{BASE_URL}/api/tasks/{task['id']}", json={"status": "completed"} ) if response.status_code != 200: print(f"✗ 更新任务失败: {response.text}") return updated_task = response.json() print(f"✓ 任务状态更新为: {updated_task['status']}") print() # 6. 清理测试数据 print("6. 清理测试数据...") requests.delete(f"{BASE_URL}/api/projects/{project['id']}") print("✓ 测试数据已清理") print() print("=" * 60) print("✓ 所有测试通过!") print("=" * 60) def check_sample_data(): """检查示例数据""" print("=" * 60) print("检查示例数据") print("=" * 60) print() # 查询项目 print("项目列表:") response = requests.get(f"{BASE_URL}/api/projects") if response.status_code == 200: projects = response.json() for project in projects: print(f" - {project['name']} (ID: {project['id']}, 任务数: {project['task_count']})") else: print(f" ✗ 查询失败: {response.text}") print() # 查询任务 print("任务列表:") response = requests.get(f"{BASE_URL}/api/tasks") if response.status_code == 200: tasks = response.json() for task in tasks: print(f" - {task['name']} (状态: {task['status']}, 进度: {task['progress']}%)") else: print(f" ✗ 查询失败: {response.text}") print() # 查询标注 print("标注列表:") response = requests.get(f"{BASE_URL}/api/annotations") if response.status_code == 200: annotations = response.json() print(f" 共 {len(annotations)} 条标注记录") for annotation in annotations[:5]: # 只显示前5条 print(f" - 任务: {annotation['task_id']}, 用户: {annotation['user_id']}") else: print(f" ✗ 查询失败: {response.text}") print() if __name__ == "__main__": import sys try: if len(sys.argv) > 1 and sys.argv[1] == "check": check_sample_data() else: test_complete_workflow() except requests.exceptions.ConnectionError: print("✗ 错误: 无法连接到后端服务器") print(" 请确保后端服务器正在运行: python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000") except Exception as e: print(f"✗ 发生错误: {e}") import traceback traceback.print_exc()