test_study_report_api.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 学习报告API接口测试脚本
  5. 测试学习报告相关的API接口功能
  6. """
  7. import requests
  8. import json
  9. import time
  10. # 配置
  11. BASE_URL = "http://localhost:8080/api"
  12. USER_ID = 1
  13. GOAL_ID = 1
  14. def test_generate_study_report():
  15. """测试生成学习报告"""
  16. print("\n=== 测试生成学习报告 ===")
  17. url = f"{BASE_URL}/study-reports/generate"
  18. params = {
  19. "userId": USER_ID,
  20. "goalId": GOAL_ID
  21. }
  22. try:
  23. response = requests.post(url, params=params, timeout=10)
  24. print(f"请求URL: {url}")
  25. print(f"请求参数: {params}")
  26. print(f"响应状态码: {response.status_code}")
  27. print(f"响应内容: {response.text}")
  28. if response.status_code == 200:
  29. data = response.json()
  30. if data.get('code') == 200:
  31. print("✅ 生成学习报告成功")
  32. return True
  33. else:
  34. print(f"❌ 生成学习报告失败: {data.get('message')}")
  35. return False
  36. else:
  37. print(f"❌ HTTP请求失败: {response.status_code}")
  38. return False
  39. except Exception as e:
  40. print(f"❌ 请求异常: {e}")
  41. return False
  42. def test_get_latest_study_report():
  43. """测试获取最近学习报告"""
  44. print("\n=== 测试获取最近学习报告 ===")
  45. url = f"{BASE_URL}/study-reports/latest"
  46. params = {"userId": USER_ID}
  47. try:
  48. response = requests.get(url, params=params, timeout=10)
  49. print(f"请求URL: {url}")
  50. print(f"请求参数: {params}")
  51. print(f"响应状态码: {response.status_code}")
  52. print(f"响应内容: {response.text}")
  53. if response.status_code == 200:
  54. data = response.json()
  55. if data.get('code') == 200:
  56. print("✅ 获取最近学习报告成功")
  57. return True
  58. else:
  59. print(f"❌ 获取最近学习报告失败: {data.get('message')}")
  60. return False
  61. else:
  62. print(f"❌ HTTP请求失败: {response.status_code}")
  63. return False
  64. except Exception as e:
  65. print(f"❌ 请求异常: {e}")
  66. return False
  67. def test_get_all_study_reports():
  68. """测试获取用户所有学习报告"""
  69. print("\n=== 测试获取用户所有学习报告 ===")
  70. url = f"{BASE_URL}/study-reports/user/{USER_ID}"
  71. try:
  72. response = requests.get(url, timeout=10)
  73. print(f"请求URL: {url}")
  74. print(f"响应状态码: {response.status_code}")
  75. print(f"响应内容: {response.text}")
  76. if response.status_code == 200:
  77. data = response.json()
  78. if data.get('code') == 200:
  79. reports = data.get('data', [])
  80. print(f"✅ 获取用户所有学习报告成功,共 {len(reports)} 个报告")
  81. return True
  82. else:
  83. print(f"❌ 获取用户所有学习报告失败: {data.get('message')}")
  84. return False
  85. else:
  86. print(f"❌ HTTP请求失败: {response.status_code}")
  87. return False
  88. except Exception as e:
  89. print(f"❌ 请求异常: {e}")
  90. return False
  91. def test_get_goal_study_report():
  92. """测试获取指定目标的学习报告"""
  93. print("\n=== 测试获取指定目标的学习报告 ===")
  94. url = f"{BASE_URL}/study-reports/latest/goal/{GOAL_ID}"
  95. params = {"userId": USER_ID}
  96. try:
  97. response = requests.get(url, params=params, timeout=10)
  98. print(f"请求URL: {url}")
  99. print(f"请求参数: {params}")
  100. print(f"响应状态码: {response.status_code}")
  101. print(f"响应内容: {response.text}")
  102. if response.status_code == 200:
  103. data = response.json()
  104. if data.get('code') == 200:
  105. print("✅ 获取指定目标的学习报告成功")
  106. return True
  107. else:
  108. print(f"❌ 获取指定目标的学习报告失败: {data.get('message')}")
  109. return False
  110. else:
  111. print(f"❌ HTTP请求失败: {response.status_code}")
  112. return False
  113. except Exception as e:
  114. print(f"❌ 请求异常: {e}")
  115. return False
  116. def main():
  117. """主函数"""
  118. print("学习报告API接口测试开始...")
  119. print(f"测试用户ID: {USER_ID}")
  120. print(f"测试目标ID: {GOAL_ID}")
  121. # 测试结果统计
  122. test_results = []
  123. # 执行测试
  124. test_results.append(test_generate_study_report())
  125. time.sleep(1) # 等待1秒
  126. test_results.append(test_get_latest_study_report())
  127. time.sleep(1)
  128. test_results.append(test_get_all_study_reports())
  129. time.sleep(1)
  130. test_results.append(test_get_goal_study_report())
  131. # 统计结果
  132. success_count = sum(test_results)
  133. total_count = len(test_results)
  134. print("\n=== 测试结果汇总 ===")
  135. print(f"总测试数: {total_count}")
  136. print(f"成功数: {success_count}")
  137. print(f"失败数: {total_count - success_count}")
  138. print(f"成功率: {success_count/total_count*100:.1f}%")
  139. if success_count == total_count:
  140. print("🎉 所有学习报告API接口测试通过!")
  141. else:
  142. print("⚠️ 部分学习报告API接口测试失败,请检查服务器日志")
  143. if __name__ == "__main__":
  144. main()