'commit'
This commit is contained in:
BIN
Controller/__pycache__/EducationDataController.cpython-310.pyc
Normal file
BIN
Controller/__pycache__/EducationDataController.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
@@ -12,8 +12,10 @@ class EducationDataModel:
|
||||
'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')
|
||||
TEACHER_COUNT_FILE = os.path.join(DATA_DIR, 'TeacherCount.json')
|
||||
ENROLLMENT_COUNT_FILE = os.path.join(DATA_DIR, 'ZaiXiaoShengCount.json')
|
||||
|
BIN
Model/__pycache__/EducationDataModel.cpython-310.pyc
Normal file
BIN
Model/__pycache__/EducationDataModel.cpython-310.pyc
Normal file
Binary file not shown.
@@ -4,6 +4,7 @@ import time
|
||||
|
||||
# API基础URL
|
||||
BASE_URL = "http://localhost:8100/RuYuanZaiYuan"
|
||||
EDUCATION_DATA_BASE_URL = "http://localhost:8100/EducationData"
|
||||
|
||||
# 支持的教育阶段
|
||||
test_education_stages = [
|
||||
@@ -13,6 +14,9 @@ test_education_stages = [
|
||||
("senior", "高中")
|
||||
]
|
||||
|
||||
# 测试年份
|
||||
test_years = [2023, 2022, 2024]
|
||||
|
||||
|
||||
def test_school_chart_interface():
|
||||
"""测试获取教育阶段图表数据接口"""
|
||||
@@ -51,13 +55,23 @@ def test_school_chart_interface():
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
def test_school_inschool_chart_interface():
|
||||
"""测试获取在校人数图表数据接口"""
|
||||
print("\n===== 测试 /school/inschool/chart 接口 =====")
|
||||
# @router.get("/school/inschool/chart")
|
||||
# async def get_in_school_chart_config(
|
||||
# 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)
|
||||
#
|
||||
|
||||
|
||||
def test_education_data_by_year():
|
||||
"""测试获取指定年份教育数据接口"""
|
||||
print("\n===== 测试 /EducationData/byYear 接口 =====")
|
||||
|
||||
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}) 数据:")
|
||||
# 测试不同年份的数据
|
||||
for year in test_years:
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={year}"
|
||||
print(f"\n测试 {year} 年数据:")
|
||||
|
||||
try:
|
||||
response = requests.get(url)
|
||||
@@ -68,17 +82,32 @@ def test_school_inschool_chart_interface():
|
||||
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} 数据验证通过")
|
||||
assert "code" in data, "返回数据缺少code字段"
|
||||
assert "message" in data, "返回数据缺少message字段"
|
||||
assert "data" in data, "返回数据缺少data字段"
|
||||
assert data["code"] == 200, f"状态码不匹配,期望: 200,实际: {data['code']}"
|
||||
assert isinstance(data["data"], list), "data字段应为列表类型"
|
||||
|
||||
# 验证数据内容
|
||||
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:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
except AssertionError as e:
|
||||
@@ -87,64 +116,51 @@ def test_school_inschool_chart_interface():
|
||||
# 添加短暂延迟,避免请求过于频繁
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
def test_default_parameters():
|
||||
"""测试默认参数情况"""
|
||||
print("\n===== 测试默认参数 =====")
|
||||
# 测试边界值
|
||||
print("\n===== 测试边界值 =====")
|
||||
|
||||
# 测试默认参数下的school/chart接口
|
||||
url = f"{BASE_URL}/school/chart"
|
||||
# 测试最小值年份
|
||||
min_year = 2015
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={min_year}"
|
||||
print(f"\n测试最小值年份 {min_year}:")
|
||||
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===== 测试无效参数 =====")
|
||||
print(f"响应状态码: {response.status_code}")
|
||||
print(f"✅ 最小值年份测试通过")
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
|
||||
# 测试无效的教育阶段参数
|
||||
invalid_stage = "invalid_stage"
|
||||
url = f"{BASE_URL}/school/chart?education_stage={invalid_stage}"
|
||||
# 测试最大值年份
|
||||
max_year = 2028
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/byYear?year={max_year}"
|
||||
print(f"\n测试最大值年份 {max_year}:")
|
||||
try:
|
||||
response = requests.get(url)
|
||||
print(f"无效参数响应状态码: {response.status_code}")
|
||||
if response.status_code == 422: # FastAPI参数验证失败的状态码
|
||||
print("✅ 无效参数正确被拒绝")
|
||||
response.raise_for_status()
|
||||
print(f"响应状态码: {response.status_code}")
|
||||
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:
|
||||
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所有测试完成!")
|
||||
print(f"❌ 期望状态码422,实际: {response.status_code}")
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_all_tests()
|
||||
# 测试原有接口
|
||||
# test_school_chart_interface()
|
||||
|
||||
# 测试新添加的教育数据接口
|
||||
test_education_data_by_year()
|
Reference in New Issue
Block a user