| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- """
- 测试标注功能脚本
- 快速测试标注平台的完整流程。
- """
- 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": """<View>
- <Text name="text" value="$text"/>
- <Choices name="label" toName="text" choice="single">
- <Choice value="类别A"/>
- <Choice value="类别B"/>
- </Choices>
- </View>"""
- }
-
- 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()
|