test_annotation.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. 测试标注功能脚本
  3. 快速测试标注平台的完整流程。
  4. """
  5. import requests
  6. import json
  7. BASE_URL = "http://localhost:8000"
  8. def test_complete_workflow():
  9. """测试完整的标注工作流程"""
  10. print("=" * 60)
  11. print("测试标注平台完整工作流程")
  12. print("=" * 60)
  13. print()
  14. # 1. 创建项目
  15. print("1. 创建测试项目...")
  16. project_data = {
  17. "name": "测试项目",
  18. "description": "用于测试的简单文本分类项目",
  19. "config": """<View>
  20. <Text name="text" value="$text"/>
  21. <Choices name="label" toName="text" choice="single">
  22. <Choice value="类别A"/>
  23. <Choice value="类别B"/>
  24. </Choices>
  25. </View>"""
  26. }
  27. response = requests.post(f"{BASE_URL}/api/projects", json=project_data)
  28. if response.status_code != 201:
  29. print(f"✗ 创建项目失败: {response.text}")
  30. return
  31. project = response.json()
  32. print(f"✓ 项目创建成功: {project['id']}")
  33. print()
  34. # 2. 创建任务
  35. print("2. 创建测试任务...")
  36. task_data = {
  37. "project_id": project['id'],
  38. "name": "测试任务",
  39. "data": {
  40. "text": "这是一个测试文本"
  41. }
  42. }
  43. response = requests.post(f"{BASE_URL}/api/tasks", json=task_data)
  44. if response.status_code != 201:
  45. print(f"✗ 创建任务失败: {response.text}")
  46. return
  47. task = response.json()
  48. print(f"✓ 任务创建成功: {task['id']}")
  49. print()
  50. # 3. 模拟标注
  51. print("3. 创建标注...")
  52. annotation_data = {
  53. "task_id": task['id'],
  54. "user_id": "test_user",
  55. "result": {
  56. "label": "类别A"
  57. }
  58. }
  59. response = requests.post(f"{BASE_URL}/api/annotations", json=annotation_data)
  60. if response.status_code != 201:
  61. print(f"✗ 创建标注失败: {response.text}")
  62. return
  63. annotation = response.json()
  64. print(f"✓ 标注创建成功: {annotation['id']}")
  65. print()
  66. # 4. 查询标注
  67. print("4. 查询标注结果...")
  68. response = requests.get(f"{BASE_URL}/api/annotations/{annotation['id']}")
  69. if response.status_code != 200:
  70. print(f"✗ 查询标注失败: {response.text}")
  71. return
  72. result = response.json()
  73. print(f"✓ 标注结果: {json.dumps(result, indent=2, ensure_ascii=False)}")
  74. print()
  75. # 5. 更新任务状态
  76. print("5. 更新任务状态...")
  77. response = requests.put(
  78. f"{BASE_URL}/api/tasks/{task['id']}",
  79. json={"status": "completed"}
  80. )
  81. if response.status_code != 200:
  82. print(f"✗ 更新任务失败: {response.text}")
  83. return
  84. updated_task = response.json()
  85. print(f"✓ 任务状态更新为: {updated_task['status']}")
  86. print()
  87. # 6. 清理测试数据
  88. print("6. 清理测试数据...")
  89. requests.delete(f"{BASE_URL}/api/projects/{project['id']}")
  90. print("✓ 测试数据已清理")
  91. print()
  92. print("=" * 60)
  93. print("✓ 所有测试通过!")
  94. print("=" * 60)
  95. def check_sample_data():
  96. """检查示例数据"""
  97. print("=" * 60)
  98. print("检查示例数据")
  99. print("=" * 60)
  100. print()
  101. # 查询项目
  102. print("项目列表:")
  103. response = requests.get(f"{BASE_URL}/api/projects")
  104. if response.status_code == 200:
  105. projects = response.json()
  106. for project in projects:
  107. print(f" - {project['name']} (ID: {project['id']}, 任务数: {project['task_count']})")
  108. else:
  109. print(f" ✗ 查询失败: {response.text}")
  110. print()
  111. # 查询任务
  112. print("任务列表:")
  113. response = requests.get(f"{BASE_URL}/api/tasks")
  114. if response.status_code == 200:
  115. tasks = response.json()
  116. for task in tasks:
  117. print(f" - {task['name']} (状态: {task['status']}, 进度: {task['progress']}%)")
  118. else:
  119. print(f" ✗ 查询失败: {response.text}")
  120. print()
  121. # 查询标注
  122. print("标注列表:")
  123. response = requests.get(f"{BASE_URL}/api/annotations")
  124. if response.status_code == 200:
  125. annotations = response.json()
  126. print(f" 共 {len(annotations)} 条标注记录")
  127. for annotation in annotations[:5]: # 只显示前5条
  128. print(f" - 任务: {annotation['task_id']}, 用户: {annotation['user_id']}")
  129. else:
  130. print(f" ✗ 查询失败: {response.text}")
  131. print()
  132. if __name__ == "__main__":
  133. import sys
  134. try:
  135. if len(sys.argv) > 1 and sys.argv[1] == "check":
  136. check_sample_data()
  137. else:
  138. test_complete_workflow()
  139. except requests.exceptions.ConnectionError:
  140. print("✗ 错误: 无法连接到后端服务器")
  141. print(" 请确保后端服务器正在运行: python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000")
  142. except Exception as e:
  143. print(f"✗ 发生错误: {e}")
  144. import traceback
  145. traceback.print_exc()