test_calendar_api_route.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """
  2. 测试日历API路由接口
  3. """
  4. import sys
  5. import os
  6. import requests
  7. import json
  8. from datetime import date
  9. # 添加项目路径到sys.path
  10. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  11. def test_calendar_api_route():
  12. """测试日历API路由接口"""
  13. print("=== 测试日历API路由接口 ===")
  14. # API基础URL(根据实际部署情况调整)
  15. base_url = "http://localhost:5000" # 假设Flask应用运行在5000端口
  16. api_endpoint = "/api/data_parse/get-calendar-info"
  17. # 测试用例1: 有效日期格式
  18. print("\n1. 测试有效日期格式:")
  19. test_date = "2025-01-19"
  20. print(f"查询日期: {test_date}")
  21. try:
  22. response = requests.get(f"{base_url}{api_endpoint}", params={'date': test_date})
  23. print(f"HTTP状态码: {response.status_code}")
  24. print(f"响应内容: {response.text}")
  25. if response.status_code == 200:
  26. result = response.json()
  27. if result.get('return_code') == 200:
  28. print("✅ API调用成功!")
  29. calendar_data = result.get('result', {})
  30. if calendar_data:
  31. print(f" - ID: {calendar_data.get('id')}")
  32. print(f" - 阳历: {calendar_data.get('yangli')}")
  33. print(f" - 阴历: {calendar_data.get('yinli')}")
  34. print(f" - 五行: {calendar_data.get('wuxing')}")
  35. print(f" - 宜: {calendar_data.get('yi')}")
  36. print(f" - 忌: {calendar_data.get('ji')}")
  37. else:
  38. print(f"❌ 业务逻辑失败: {result.get('error')}")
  39. else:
  40. print(f"❌ HTTP请求失败: {response.status_code}")
  41. except requests.exceptions.ConnectionError:
  42. print("❌ 连接失败 - 请确保Flask应用正在运行")
  43. except Exception as e:
  44. print(f"❌ 测试过程中发生异常: {e}")
  45. # 测试用例2: 无效日期格式
  46. print("\n2. 测试无效日期格式:")
  47. invalid_date = "2025-1-19"
  48. print(f"查询日期: {invalid_date}")
  49. try:
  50. response = requests.get(f"{base_url}{api_endpoint}", params={'date': invalid_date})
  51. print(f"HTTP状态码: {response.status_code}")
  52. print(f"响应内容: {response.text}")
  53. if response.status_code == 400:
  54. print("✅ 正确返回400错误状态码")
  55. else:
  56. print(f"❌ 期望400状态码,实际得到: {response.status_code}")
  57. except requests.exceptions.ConnectionError:
  58. print("❌ 连接失败 - 请确保Flask应用正在运行")
  59. except Exception as e:
  60. print(f"❌ 测试过程中发生异常: {e}")
  61. # 测试用例3: 缺少日期参数
  62. print("\n3. 测试缺少日期参数:")
  63. print("不传递date参数")
  64. try:
  65. response = requests.get(f"{base_url}{api_endpoint}")
  66. print(f"HTTP状态码: {response.status_code}")
  67. print(f"响应内容: {response.text}")
  68. if response.status_code == 400:
  69. print("✅ 正确返回400错误状态码")
  70. else:
  71. print(f"❌ 期望400状态码,实际得到: {response.status_code}")
  72. except requests.exceptions.ConnectionError:
  73. print("❌ 连接失败 - 请确保Flask应用正在运行")
  74. except Exception as e:
  75. print(f"❌ 测试过程中发生异常: {e}")
  76. # 测试用例4: 空日期参数
  77. print("\n4. 测试空日期参数:")
  78. empty_date = ""
  79. print(f"查询日期: '{empty_date}'")
  80. try:
  81. response = requests.get(f"{base_url}{api_endpoint}", params={'date': empty_date})
  82. print(f"HTTP状态码: {response.status_code}")
  83. print(f"响应内容: {response.text}")
  84. if response.status_code == 400:
  85. print("✅ 正确返回400错误状态码")
  86. else:
  87. print(f"❌ 期望400状态码,实际得到: {response.status_code}")
  88. except requests.exceptions.ConnectionError:
  89. print("❌ 连接失败 - 请确保Flask应用正在运行")
  90. except Exception as e:
  91. print(f"❌ 测试过程中发生异常: {e}")
  92. print("\n✓ 所有API路由测试完成!")
  93. def test_api_documentation():
  94. """测试API文档和接口规范"""
  95. print("\n=== 测试API文档和接口规范 ===")
  96. # 检查API接口规范
  97. print("API接口规范:")
  98. print("- 端点: GET /api/data_parse/get-calendar-info")
  99. print("- 参数: date (YYYY-MM-DD格式)")
  100. print("- 返回格式: 标准化的JSON响应")
  101. # 检查返回字段
  102. print("\n返回字段说明:")
  103. print("- reason: 操作结果描述")
  104. print("- return_code: HTTP状态码")
  105. print("- result: 黄历信息数据对象")
  106. print("- error: 错误信息(失败时)")
  107. print("\n✓ API文档测试完成!")
  108. def main():
  109. """主测试函数"""
  110. print("开始测试日历API路由接口...\n")
  111. try:
  112. test_api_documentation()
  113. test_calendar_api_route()
  114. print("\n🎉 所有测试完成!")
  115. print("\nAPI接口特性:")
  116. print("- ✅ GET方法访问")
  117. print("- ✅ 日期参数验证")
  118. print("- ✅ 标准化JSON响应")
  119. print("- ✅ 完整的错误处理")
  120. print("- ✅ 详细的日志记录")
  121. print("- ✅ 符合RESTful规范")
  122. print("\n使用方法:")
  123. print("GET /api/data_parse/get-calendar-info?date=2025-01-19")
  124. except Exception as e:
  125. print(f"❌ 测试失败: {e}")
  126. import traceback
  127. traceback.print_exc()
  128. if __name__ == "__main__":
  129. main()