test_unit_grouping.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 单元分类功能集成测试
  5. 该测试文件用于验证单元分类功能是否正确集成到项目中
  6. """
  7. import requests
  8. import json
  9. from typing import Dict, Any
  10. def test_unit_grouping_api():
  11. """
  12. 测试单元分类API接口
  13. """
  14. base_url = "http://localhost:8000"
  15. print("=" * 60)
  16. print("单元分类功能集成测试")
  17. print("=" * 60)
  18. # 测试1: 获取单元h_xtfx分类结果
  19. print("\n1. 测试获取单元h_xtfx分类结果")
  20. print("-" * 40)
  21. try:
  22. response = requests.get(f"{base_url}/api/unit-grouping/h_xtfx")
  23. if response.status_code == 200:
  24. data = response.json()
  25. print(f"✓ 请求成功")
  26. print(f"✓ 成功状态: {data.get('success', False)}")
  27. if data.get('success', False):
  28. statistics = data.get('statistics', {})
  29. print(f"✓ 总单元数: {statistics.get('total_units', 0)}")
  30. print(f"✓ 有数据的单元数: {statistics.get('units_with_data', 0)}")
  31. print(f"✓ 无数据的单元数: {statistics.get('units_without_data', 0)}")
  32. category_dist = statistics.get('category_distribution', {})
  33. print(f"✓ 类别分布:")
  34. for category, count in category_dist.items():
  35. print(f" - {category}: {count}")
  36. # 显示前5个单元的结果
  37. unit_data = data.get('data', {})
  38. if unit_data:
  39. print(f"✓ 前5个单元结果样例:")
  40. for i, (unit_id, h_xtfx) in enumerate(list(unit_data.items())[:5]):
  41. print(f" - 单元 {unit_id}: {h_xtfx}")
  42. else:
  43. print(f"✗ 服务返回失败: {data.get('error', 'Unknown error')}")
  44. else:
  45. print(f"✗ 请求失败,状态码: {response.status_code}")
  46. print(f"✗ 错误信息: {response.text}")
  47. except requests.exceptions.ConnectionError:
  48. print("✗ 连接失败,请确保服务正在运行 (uvicorn main:app --reload)")
  49. except Exception as e:
  50. print(f"✗ 测试异常: {str(e)}")
  51. # 测试2: 获取统计信息
  52. print("\n2. 测试获取统计信息")
  53. print("-" * 40)
  54. try:
  55. response = requests.get(f"{base_url}/api/unit-grouping/statistics")
  56. if response.status_code == 200:
  57. data = response.json()
  58. print(f"✓ 请求成功")
  59. print(f"✓ 成功状态: {data.get('success', False)}")
  60. if data.get('success', False):
  61. statistics = data.get('statistics', {})
  62. print(f"✓ 统计信息获取成功")
  63. print(f" - 总单元数: {statistics.get('total_units', 0)}")
  64. print(f" - 有数据的单元数: {statistics.get('units_with_data', 0)}")
  65. print(f" - 无数据的单元数: {statistics.get('units_without_data', 0)}")
  66. else:
  67. print(f"✗ 请求失败,状态码: {response.status_code}")
  68. print(f"✗ 错误信息: {response.text}")
  69. except requests.exceptions.ConnectionError:
  70. print("✗ 连接失败,请确保服务正在运行")
  71. except Exception as e:
  72. print(f"✗ 测试异常: {str(e)}")
  73. # 测试3: 获取特定单元的h_xtfx值
  74. print("\n3. 测试获取特定单元的h_xtfx值")
  75. print("-" * 40)
  76. try:
  77. # 首先获取一个存在的单元ID
  78. response = requests.get(f"{base_url}/api/unit-grouping/h_xtfx")
  79. if response.status_code == 200:
  80. data = response.json()
  81. if data.get('success', False):
  82. unit_data = data.get('data', {})
  83. if unit_data:
  84. # 取第一个单元进行测试
  85. test_unit_id = list(unit_data.keys())[0]
  86. # 测试获取特定单元
  87. response = requests.get(f"{base_url}/api/unit-grouping/unit/{test_unit_id}")
  88. if response.status_code == 200:
  89. unit_result = response.json()
  90. print(f"✓ 请求成功")
  91. print(f"✓ 单元 {test_unit_id} 的h_xtfx值: {unit_result.get('h_xtfx')}")
  92. else:
  93. print(f"✗ 请求失败,状态码: {response.status_code}")
  94. else:
  95. print("✗ 没有可用的单元数据进行测试")
  96. else:
  97. print("✗ 无法获取单元数据进行测试")
  98. else:
  99. print("✗ 无法获取单元数据进行测试")
  100. except requests.exceptions.ConnectionError:
  101. print("✗ 连接失败,请确保服务正在运行")
  102. except Exception as e:
  103. print(f"✗ 测试异常: {str(e)}")
  104. print("\n" + "=" * 60)
  105. print("测试完成")
  106. print("=" * 60)
  107. # 测试4: 测试新的ORM接口
  108. print("\n4. 测试新的ORM接口功能")
  109. print("-" * 40)
  110. # 测试点位统计信息
  111. try:
  112. response = requests.get(f"{base_url}/api/unit-grouping/points/statistics")
  113. if response.status_code == 200:
  114. data = response.json()
  115. print(f"✓ 点位统计信息获取成功")
  116. distribution = data.get('distribution', {})
  117. print(f"✓ 总点位数: {data.get('total_points', 0)}")
  118. for category, stats in distribution.items():
  119. print(f" - {category}: {stats['count']} ({stats['percentage']}%)")
  120. else:
  121. print(f"✗ 获取点位统计信息失败,状态码: {response.status_code}")
  122. except Exception as e:
  123. print(f"✗ 测试点位统计信息异常: {str(e)}")
  124. # 测试数据库摘要信息
  125. try:
  126. response = requests.get(f"{base_url}/api/unit-grouping/database/summary")
  127. if response.status_code == 200:
  128. data = response.json()
  129. print(f"✓ 数据库摘要信息获取成功")
  130. summary = data.get('summary', {})
  131. print(f" - 总单元数: {summary.get('total_units', 0)}")
  132. print(f" - 总点位数: {summary.get('total_points', 0)}")
  133. print(f" - h_xtfx分类数: {summary.get('h_xtfx_categories', 0)}")
  134. else:
  135. print(f"✗ 获取数据库摘要信息失败,状态码: {response.status_code}")
  136. except Exception as e:
  137. print(f"✗ 测试数据库摘要信息异常: {str(e)}")
  138. # 测试批量获取单元信息
  139. try:
  140. response = requests.get(f"{base_url}/api/unit-grouping/units/batch?unit_ids=1&unit_ids=2&unit_ids=3")
  141. if response.status_code == 200:
  142. data = response.json()
  143. print(f"✓ 批量获取单元信息成功")
  144. print(f" - 请求数量: {data.get('total_requested', 0)}")
  145. print(f" - 找到数量: {data.get('total_found', 0)}")
  146. else:
  147. print(f"✗ 批量获取单元信息失败,状态码: {response.status_code}")
  148. except Exception as e:
  149. print(f"✗ 测试批量获取单元信息异常: {str(e)}")
  150. # 输出使用说明
  151. print("\n使用说明:")
  152. print("1. 启动服务: uvicorn main:app --reload")
  153. print("2. 访问API文档: http://localhost:8000/docs")
  154. print("3. 主要接口:")
  155. print(" - GET /api/unit-grouping/h_xtfx - 获取所有单元的h_xtfx分类结果")
  156. print(" - GET /api/unit-grouping/statistics - 获取统计信息")
  157. print(" - GET /api/unit-grouping/unit/{unit_id} - 获取特定单元的h_xtfx值")
  158. print(" - GET /api/unit-grouping/points/statistics - 获取点位统计信息(ORM)")
  159. print(" - GET /api/unit-grouping/database/summary - 获取数据库摘要信息(ORM)")
  160. print(" - GET /api/unit-grouping/units/batch - 批量获取单元信息(ORM)")
  161. print(" - GET /api/unit-grouping/points/by-area - 按区域获取点位数据(ORM)")
  162. if __name__ == "__main__":
  163. test_unit_grouping_api()