This commit is contained in:
2025-09-11 20:25:05 +08:00
parent a941049482
commit 6a1a67ac07

View File

@@ -0,0 +1,150 @@
import requests
import json
import time
# API基础URL
BASE_URL = "http://localhost:8100/RuYuanZaiYuan"
# 支持的教育阶段
test_education_stages = [
("preschool", "学前"),
("primary", "小学"),
("junior", "初中"),
("senior", "高中")
]
def test_school_chart_interface():
"""测试获取教育阶段图表数据接口"""
print("\n===== 测试 /school/chart 接口 =====")
for stage_code, stage_name in test_education_stages:
url = f"{BASE_URL}/school/chart?education_stage={stage_code}"
print(f"\n测试 {stage_name}({stage_code}) 数据:")
try:
response = requests.get(url)
response.raise_for_status() # 如果状态码不是200抛出异常
data = response.json()
print(f"响应状态码: {response.status_code}")
print(f"响应数据: {json.dumps(data, ensure_ascii=False, indent=2)}")
# 验证返回数据的基本结构
assert "xAxis_data" in data, "返回数据缺少xAxis_data字段"
assert "series_data_0" in data, "返回数据缺少series_data_0字段(城区数据)"
assert "series_data_1" in data, "返回数据缺少series_data_1字段(镇区数据)"
assert "series_data_2" in data, "返回数据缺少series_data_2字段(乡村数据)"
assert "series_data_3" in data, "返回数据缺少series_data_3字段(总人数数据)"
assert "series_data_4" in data, "返回数据缺少series_data_4字段(2022年基数数据)"
assert "education_stage" in data, "返回数据缺少education_stage字段"
assert data["education_stage"] == stage_name, f"教育阶段名称不匹配,期望: {stage_name},实际: {data['education_stage']}"
print(f"{stage_name} 数据验证通过")
except requests.exceptions.RequestException as e:
print(f"❌ 请求失败: {e}")
except AssertionError as e:
print(f"❌ 数据验证失败: {e}")
# 添加短暂延迟,避免请求过于频繁
time.sleep(0.5)
def test_school_inschool_chart_interface():
"""测试获取在校人数图表数据接口"""
print("\n===== 测试 /school/inschool/chart 接口 =====")
for stage_code, stage_name in test_education_stages:
url = f"{BASE_URL}/school/inschool/chart?education_stage={stage_code}"
print(f"\n测试 {stage_name}({stage_code}) 数据:")
try:
response = requests.get(url)
response.raise_for_status() # 如果状态码不是200抛出异常
data = response.json()
print(f"响应状态码: {response.status_code}")
print(f"响应数据: {json.dumps(data, ensure_ascii=False, indent=2)}")
# 验证返回数据的基本结构
assert "xAxis_data" in data, "返回数据缺少xAxis_data字段"
assert "series_data_0" in data, "返回数据缺少series_data_0字段(城区数据)"
assert "series_data_1" in data, "返回数据缺少series_data_1字段(镇区数据)"
assert "series_data_2" in data, "返回数据缺少series_data_2字段(乡村数据)"
assert "series_data_3" in data, "返回数据缺少series_data_3字段(总人数数据)"
assert "series_data_4" in data, "返回数据缺少series_data_4字段(2022年基数数据)"
assert "education_stage" in data, "返回数据缺少education_stage字段"
assert data["education_stage"] == stage_name, f"教育阶段名称不匹配,期望: {stage_name},实际: {data['education_stage']}"
print(f"{stage_name} 数据验证通过")
except requests.exceptions.RequestException as e:
print(f"❌ 请求失败: {e}")
except AssertionError as e:
print(f"❌ 数据验证失败: {e}")
# 添加短暂延迟,避免请求过于频繁
time.sleep(0.5)
def test_default_parameters():
"""测试默认参数情况"""
print("\n===== 测试默认参数 =====")
# 测试默认参数下的school/chart接口
url = f"{BASE_URL}/school/chart"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
print(f"school/chart默认参数响应状态码: {response.status_code}")
print(f"school/chart默认教育阶段: {data.get('education_stage')}")
# 测试默认参数下的school/inschool/chart接口
url = f"{BASE_URL}/school/inschool/chart"
response = requests.get(url)
response.raise_for_status()
data = response.json()
print(f"school/inschool/chart默认参数响应状态码: {response.status_code}")
print(f"school/inschool/chart默认教育阶段: {data.get('education_stage')}")
except Exception as e:
print(f"❌ 默认参数测试失败: {e}")
def test_invalid_parameters():
"""测试无效参数情况"""
print("\n===== 测试无效参数 =====")
# 测试无效的教育阶段参数
invalid_stage = "invalid_stage"
url = f"{BASE_URL}/school/chart?education_stage={invalid_stage}"
try:
response = requests.get(url)
print(f"无效参数响应状态码: {response.status_code}")
if response.status_code == 422: # FastAPI参数验证失败的状态码
print("✅ 无效参数正确被拒绝")
else:
data = response.json()
print(f"无效参数处理结果: {json.dumps(data, ensure_ascii=False, indent=2)}")
except Exception as e:
print(f"❌ 无效参数测试异常: {e}")
def run_all_tests():
"""运行所有测试"""
print("开始测试教育阶段接口...")
# 运行各个测试函数
test_school_chart_interface()
test_school_inschool_chart_interface()
test_default_parameters()
test_invalid_parameters()
print("\n所有测试完成!")
if __name__ == "__main__":
run_all_tests()