This commit is contained in:
2025-09-11 21:23:39 +08:00
parent b821e316ca
commit 9349d582b6
5 changed files with 86 additions and 68 deletions

View File

@@ -12,8 +12,10 @@ class EducationDataModel:
'vocational': '中职' 'vocational': '中职'
} }
# 数据文件路径 # 获取脚本所在目录的绝对路径
DATA_DIR = '../Data' BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# 使用绝对路径定义数据目录和文件
DATA_DIR = os.path.join(BASE_DIR, '../Data')
SCHOOL_COUNT_FILE = os.path.join(DATA_DIR, 'SchoolCount.json') SCHOOL_COUNT_FILE = os.path.join(DATA_DIR, 'SchoolCount.json')
TEACHER_COUNT_FILE = os.path.join(DATA_DIR, 'TeacherCount.json') TEACHER_COUNT_FILE = os.path.join(DATA_DIR, 'TeacherCount.json')
ENROLLMENT_COUNT_FILE = os.path.join(DATA_DIR, 'ZaiXiaoShengCount.json') ENROLLMENT_COUNT_FILE = os.path.join(DATA_DIR, 'ZaiXiaoShengCount.json')

Binary file not shown.

View File

@@ -4,6 +4,7 @@ import time
# API基础URL # API基础URL
BASE_URL = "http://localhost:8100/RuYuanZaiYuan" BASE_URL = "http://localhost:8100/RuYuanZaiYuan"
EDUCATION_DATA_BASE_URL = "http://localhost:8100/EducationData"
# 支持的教育阶段 # 支持的教育阶段
test_education_stages = [ test_education_stages = [
@@ -13,6 +14,9 @@ test_education_stages = [
("senior", "高中") ("senior", "高中")
] ]
# 测试年份
test_years = [2023, 2022, 2024]
def test_school_chart_interface(): def test_school_chart_interface():
"""测试获取教育阶段图表数据接口""" """测试获取教育阶段图表数据接口"""
@@ -51,13 +55,23 @@ def test_school_chart_interface():
time.sleep(0.5) time.sleep(0.5)
def test_school_inschool_chart_interface(): # @router.get("/school/inschool/chart")
"""测试获取在校人数图表数据接口""" # async def get_in_school_chart_config(
print("\n===== 测试 /school/inschool/chart 接口 =====") # education_stage: str = Query(default="preschool", pattern="^(preschool|primary|junior|senior)$",
# description="教育阶段: preschool(学前), primary(小学), junior(初中), senior(高中)")
# ):
# return RuYuanZaiYuanModel.generate_in_school_education_config(education_stage)
#
for stage_code, stage_name in test_education_stages:
url = f"{BASE_URL}/school/inschool/chart?education_stage={stage_code}" def test_education_data_by_year():
print(f"\n测试 {stage_name}({stage_code}) 数据:") """测试获取指定年份教育数据接口"""
print("\n===== 测试 /EducationData/byYear 接口 =====")
# 测试不同年份的数据
for year in test_years:
url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={year}"
print(f"\n测试 {year} 年数据:")
try: try:
response = requests.get(url) response = requests.get(url)
@@ -68,16 +82,31 @@ def test_school_inschool_chart_interface():
print(f"响应数据: {json.dumps(data, ensure_ascii=False, indent=2)}") print(f"响应数据: {json.dumps(data, ensure_ascii=False, indent=2)}")
# 验证返回数据的基本结构 # 验证返回数据的基本结构
assert "xAxis_data" in data, "返回数据缺少xAxis_data字段" assert "code" in data, "返回数据缺少code字段"
assert "series_data_0" in data, "返回数据缺少series_data_0字段(城区数据)" assert "message" in data, "返回数据缺少message字段"
assert "series_data_1" in data, "返回数据缺少series_data_1字段(镇区数据)" assert "data" in data, "返回数据缺少data字段"
assert "series_data_2" in data, "返回数据缺少series_data_2字段(乡村数据)" assert data["code"] == 200, f"状态码不匹配,期望: 200实际: {data['code']}"
assert "series_data_3" in data, "返回数据缺少series_data_3字段(总人数数据)" assert isinstance(data["data"], list), "data字段应为列表类型"
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} 数据验证通过") # 验证数据内容
if data["data"]:
# 检查每个数据项的结构
for item in data["data"]:
assert "education_stage" in item, "数据项缺少education_stage字段"
assert "school_count" in item, "数据项缺少school_count字段"
assert "teacher_count_10k" in item, "数据项缺少teacher_count_10k字段"
assert "enrollment_count_10k" in item, "数据项缺少enrollment_count_10k字段"
assert "admission_count_10k" in item, "数据项缺少admission_count_10k字段"
# 检查数值类型
assert isinstance(item["school_count"], (int, float)), "school_count应为数值类型"
assert isinstance(item["teacher_count_10k"], (int, float)), "teacher_count_10k应为数值类型"
assert isinstance(item["enrollment_count_10k"], (int, float)), "enrollment_count_10k应为数值类型"
assert isinstance(item["admission_count_10k"], (int, float)), "admission_count_10k应为数值类型"
print(f"{year} 年数据验证通过,共包含 {len(data['data'])} 个教育阶段的数据")
else:
print(f"⚠️ {year} 年未返回数据")
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"❌ 请求失败: {e}") print(f"❌ 请求失败: {e}")
@@ -87,64 +116,51 @@ def test_school_inschool_chart_interface():
# 添加短暂延迟,避免请求过于频繁 # 添加短暂延迟,避免请求过于频繁
time.sleep(0.5) time.sleep(0.5)
# 测试边界值
print("\n===== 测试边界值 =====")
def test_default_parameters(): # 测试最小值年份
"""测试默认参数情况""" min_year = 2015
print("\n===== 测试默认参数 =====") url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={min_year}"
print(f"\n测试最小值年份 {min_year}:")
# 测试默认参数下的school/chart接口
url = f"{BASE_URL}/school/chart"
try: try:
response = requests.get(url) response = requests.get(url)
response.raise_for_status() response.raise_for_status()
data = response.json() print(f"响应状态码: {response.status_code}")
print(f"school/chart默认参数响应状态码: {response.status_code}") print(f"✅ 最小值年份测试通过")
print(f"school/chart默认教育阶段: {data.get('education_stage')}") except requests.exceptions.RequestException as e:
print(f"❌ 请求失败: {e}")
# 测试默认参数下的school/inschool/chart接口 # 测试最大值年份
url = f"{BASE_URL}/school/inschool/chart" max_year = 2028
response = requests.get(url) url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={max_year}"
response.raise_for_status() print(f"\n测试最大值年份 {max_year}:")
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: try:
response = requests.get(url) response = requests.get(url)
print(f"无效参数响应状态码: {response.status_code}") response.raise_for_status()
if response.status_code == 422: # FastAPI参数验证失败的状态码 print(f"响应状态码: {response.status_code}")
print("无效参数正确被拒绝") print(f"最大值年份测试通过")
except requests.exceptions.RequestException as e:
print(f"❌ 请求失败: {e}")
# 测试超出范围的年份
invalid_year = 2030
url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={invalid_year}"
print(f"\n测试超出范围的年份 {invalid_year}:")
try:
response = requests.get(url)
if response.status_code == 422: # FastAPI的验证错误状态码
print(f"响应状态码: {response.status_code}")
print(f"✅ 超出范围年份正确返回验证错误")
else: else:
data = response.json() print(f"❌ 期望状态码422实际: {response.status_code}")
print(f"无效参数处理结果: {json.dumps(data, ensure_ascii=False, indent=2)}") except requests.exceptions.RequestException as e:
print(f"❌ 请求失败: {e}")
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__": if __name__ == "__main__":
run_all_tests() # 测试原有接口
# test_school_chart_interface()
# 测试新添加的教育数据接口
test_education_data_by_year()