test_upload_model.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import requests
  2. import json
  3. import os
  4. import tempfile
  5. BASE_URL = "http://localhost:8448"
  6. UPLOAD_API_URL = f"{BASE_URL}/watershed/model/upload"
  7. LIST_API_URL = f"{BASE_URL}/watershed/model/list/test"
  8. def test_upload_model():
  9. print("开始测试上传模型接口...")
  10. try:
  11. # 创建一个临时测试文件
  12. with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as f:
  13. f.write(b"test content")
  14. temp_file_path = f.name
  15. print(f"临时测试文件创建成功: {temp_file_path}")
  16. # 准备上传参数
  17. files = {'file': open(temp_file_path, 'rb')}
  18. data = {
  19. 'modelName': '上传测试模型',
  20. 'modelType': 'HYDROPOWER',
  21. 'modelFormat': 'OBJ',
  22. 'uploadUnit': '测试部门',
  23. 'status': 'NORMAL',
  24. 'coordinates': '116.403874,39.915168'
  25. }
  26. print(f"请求 URL: {UPLOAD_API_URL}")
  27. print(f"请求参数: {json.dumps(data, indent=2, ensure_ascii=False)}")
  28. # 发送 POST 请求
  29. response = requests.post(UPLOAD_API_URL, files=files, data=data)
  30. print(f"响应状态码: {response.status_code}")
  31. print(f"响应内容:\n{response.text}")
  32. if response.status_code == 200:
  33. data = response.json()
  34. print("\n✅ 上传成功")
  35. print(f"响应数据结构: {json.dumps(data, indent=2, ensure_ascii=False)}")
  36. if 'data' in data and data['data']:
  37. model = data['data']
  38. print("\n上传的模型信息:")
  39. for key in model.keys():
  40. print(f" {key}: {model[key]}")
  41. # 检查创建时间和更新时间是否已设置
  42. if 'created_at' in model and model['created_at']:
  43. print("\n✅ 创建时间已正确设置")
  44. else:
  45. print("\n❌ 创建时间未设置")
  46. if 'updated_at' in model and model['updated_at']:
  47. print("✅ 更新时间已正确设置")
  48. else:
  49. print("❌ 更新时间未设置")
  50. # 查询模型列表,检查是否包含刚刚上传的模型
  51. print("\n\n查询模型列表:")
  52. list_response = requests.get(LIST_API_URL, params={'page': 1, 'size': 10})
  53. print(f"列表查询响应状态码: {list_response.status_code}")
  54. if list_response.status_code == 200:
  55. list_data = list_response.json()
  56. print(f"列表查询成功,共找到 {list_data['total']} 个模型")
  57. if list_data['rows']:
  58. print("\n第一个模型的字段:")
  59. first_model = list_data['rows'][0]
  60. for key in first_model.keys():
  61. print(f" {key}: {first_model[key]}")
  62. else:
  63. print(f"\n❌ 请求失败: {response.text}")
  64. # 清理临时文件
  65. os.unlink(temp_file_path)
  66. except Exception as e:
  67. print(f"\n❌ 请求发生异常: {e}")
  68. try:
  69. os.unlink(temp_file_path)
  70. except:
  71. pass
  72. if __name__ == "__main__":
  73. test_upload_model()