test_create_model.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import requests
  2. import json
  3. BASE_URL = "http://localhost:8448"
  4. CREATE_API_URL = f"{BASE_URL}/watershed/model"
  5. LIST_API_URL = f"{BASE_URL}/watershed/model/list/test"
  6. def test_create_model():
  7. print("开始测试创建模型接口...")
  8. try:
  9. # 创建模型的测试数据
  10. test_data = {
  11. "name": "测试模型",
  12. "type": "RESERVOIR",
  13. "format": "OBJ",
  14. "uploadUnit": "测试单位",
  15. "status": "NORMAL",
  16. "coordinates": "116.403874,39.915168"
  17. }
  18. print(f"请求 URL: {CREATE_API_URL}")
  19. print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}")
  20. # 发送 POST 请求
  21. response = requests.post(CREATE_API_URL, json=test_data)
  22. print(f"响应状态码: {response.status_code}")
  23. print(f"响应内容:\n{response.text}")
  24. if response.status_code == 200:
  25. data = response.json()
  26. print("\n✅ 创建成功")
  27. print(f"响应数据结构: {json.dumps(data, indent=2, ensure_ascii=False)}")
  28. if 'data' in data and data['data']:
  29. model = data['data']
  30. print("\n创建的模型信息:")
  31. for key in model.keys():
  32. print(f" {key}: {model[key]}")
  33. # 检查创建时间和更新时间是否已设置
  34. if 'created_at' in model and model['created_at']:
  35. print("\n✅ 创建时间已正确设置")
  36. else:
  37. print("\n❌ 创建时间未设置")
  38. if 'updated_at' in model and model['updated_at']:
  39. print("✅ 更新时间已正确设置")
  40. else:
  41. print("❌ 更新时间未设置")
  42. # 查询模型列表,检查是否包含刚刚创建的模型
  43. print("\n\n查询模型列表:")
  44. list_response = requests.get(LIST_API_URL, params={'page': 1, 'size': 10})
  45. print(f"列表查询响应状态码: {list_response.status_code}")
  46. if list_response.status_code == 200:
  47. list_data = list_response.json()
  48. print(f"列表查询成功,共找到 {list_data['total']} 个模型")
  49. if list_data['rows']:
  50. print("\n第一个模型的字段:")
  51. first_model = list_data['rows'][0]
  52. for key in first_model.keys():
  53. print(f" {key}: {first_model[key]}")
  54. else:
  55. print(f"\n❌ 请求失败: {response.text}")
  56. except Exception as e:
  57. print(f"\n❌ 请求发生异常: {e}")
  58. if __name__ == "__main__":
  59. test_create_model()