test_upload_api.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import requests
  2. import json
  3. import os
  4. BASE_URL = "http://localhost:8448"
  5. UPLOAD_API_URL = f"{BASE_URL}/watershed/model/upload"
  6. LIST_API_URL = f"{BASE_URL}/watershed/model/list/test"
  7. def test_upload_model():
  8. print("开始测试上传模型接口...")
  9. try:
  10. # 准备上传文件(使用一个小的测试文件)
  11. test_file_path = "test_upload.txt"
  12. with open(test_file_path, "w") as f:
  13. f.write("这是一个测试文件")
  14. # 准备请求数据
  15. files = {
  16. 'file': open(test_file_path, 'rb')
  17. }
  18. data = {
  19. 'modelName': '测试模型',
  20. 'modelType': 'RESERVOIR',
  21. 'modelFormat': 'TXT',
  22. 'uploadUnit': '测试部门',
  23. 'status': 'NORMAL',
  24. 'coordinates': '120.1234,30.5678'
  25. }
  26. print(f"请求 URL: {UPLOAD_API_URL}")
  27. print(f"请求数据: {json.dumps(data, ensure_ascii=False)}")
  28. response = requests.post(UPLOAD_API_URL, data=data, files=files)
  29. print(f"响应状态码: {response.status_code}")
  30. print(f"响应内容:\n{response.text}")
  31. if response.status_code == 200:
  32. result = response.json()
  33. print("\n✅ 上传成功")
  34. print(f"响应数据: {json.dumps(result, indent=2, ensure_ascii=False)}")
  35. # 检查是否返回了模型数据
  36. if 'data' in result:
  37. model = result['data']
  38. print("\n返回的模型数据:")
  39. for key, value in model.items():
  40. print(f" {key}: {value}")
  41. # 检查创建时间是否设置
  42. if 'created_at' in model and model['created_at']:
  43. print("\n✅ 创建时间字段已正确设置")
  44. else:
  45. print("\n❌ 创建时间字段为空")
  46. else:
  47. print(f"\n❌ 请求失败: {response.text}")
  48. except Exception as e:
  49. print(f"\n❌ 请求发生异常: {e}")
  50. finally:
  51. # 清理测试文件
  52. if os.path.exists(test_file_path):
  53. os.remove(test_file_path)
  54. def test_get_models():
  55. print("\n--- 检查模型列表 ---")
  56. try:
  57. params = {
  58. 'page': 1,
  59. 'size': 20
  60. }
  61. response = requests.get(LIST_API_URL, params=params)
  62. if response.status_code == 200:
  63. data = response.json()
  64. print(f"模型总数: {data['total']}")
  65. if 'rows' in data and data['rows']:
  66. print("\n最近上传的模型:")
  67. for model in data['rows'][-3:]: # 显示最后3条记录
  68. print(f"\n模型ID: {model['id']}")
  69. print(f"模型名称: {model['name']}")
  70. print(f"创建时间: {model['created_at']}")
  71. print(f"更新时间: {model['updated_at']}")
  72. else:
  73. print("暂无模型数据")
  74. else:
  75. print(f"获取模型列表失败: {response.text}")
  76. except Exception as e:
  77. print(f"获取模型列表异常: {e}")
  78. if __name__ == "__main__":
  79. test_upload_model()
  80. test_get_models()