'commit'
This commit is contained in:
52
Test/read_population_data.py
Normal file
52
Test/read_population_data.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
# 获取脚本所在目录
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
# 构建数据文件的绝对路径
|
||||
file_path = os.path.join(script_dir, '../Data/RenKou.json')
|
||||
|
||||
# 读取JSON数据
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
population_data = json.load(f)
|
||||
|
||||
# 查找云南省的数据
|
||||
for area in population_data:
|
||||
if area.get('area_name') == '云南省':
|
||||
yunnan_data = area
|
||||
break
|
||||
|
||||
# 提取2023年的人口数据
|
||||
year = '2023'
|
||||
|
||||
# 总人口(万人)
|
||||
total_population = yunnan_data.get('total_population', {}).get(year, 0)
|
||||
|
||||
# 新生人口(万人,注意原数据是整数,需要转换为万人)
|
||||
birth_population = yunnan_data.get('birth_population', {}).get(year, 0)
|
||||
# 转换为万人(如果是整数)
|
||||
if isinstance(birth_population, int):
|
||||
birth_population = round(birth_population / 10000, 1)
|
||||
|
||||
# 城镇人口(万人)
|
||||
urban_population = yunnan_data.get('urban_population', {}).get(year, 0)
|
||||
|
||||
# 乡村人口(万人)
|
||||
rural_population = yunnan_data.get('rural_population', {}).get(year, 0)
|
||||
|
||||
# 城镇化率(%)
|
||||
urbanization_rate = yunnan_data.get('urbanization_rate', {}).get(year, 0)
|
||||
|
||||
# 打印结果
|
||||
print("云南省 2023年人口概览")
|
||||
print("-------------------")
|
||||
print(f"总人口 {total_population:,} 万人")
|
||||
print(f"新生人口 {birth_population} 万人")
|
||||
print(f"城镇人口 {urban_population:,} 万人")
|
||||
print(f"乡村人口 {rural_population:,} 万人")
|
||||
print(f"城镇化率 {urbanization_rate:.2f} %")
|
||||
|
||||
# 验证数据一致性
|
||||
if abs(total_population - (urban_population + rural_population)) > 0.01:
|
||||
print("\n注意:总人口与城镇人口+乡村人口存在差异")
|
||||
print(f"差异值: {abs(total_population - (urban_population + rural_population)):.2f} 万人")
|
124
Test/test_population_data_interface.py
Normal file
124
Test/test_population_data_interface.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
|
||||
# API基础URL
|
||||
EDUCATION_DATA_BASE_URL = "http://localhost:8100/EducationData"
|
||||
|
||||
# 测试年份
|
||||
test_years = [2023, 2022, 2024]
|
||||
|
||||
|
||||
def test_population_data_by_year():
|
||||
"""测试获取指定年份人口数据接口"""
|
||||
print("\n===== 测试 /EducationData/populationByYear 接口 =====")
|
||||
|
||||
# 测试不同年份的数据
|
||||
for year in test_years:
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/populationByYear?year={year}"
|
||||
print(f"\n测试 {year} 年人口数据:")
|
||||
|
||||
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 "code" in data, "返回数据缺少code字段"
|
||||
assert "message" in data, "返回数据缺少message字段"
|
||||
assert "data" in data, "返回数据缺少data字段"
|
||||
assert data["code"] == 200, f"状态码不匹配,期望: 200,实际: {data['code']}"
|
||||
|
||||
# 验证数据内容
|
||||
if data["data"]:
|
||||
# 检查数据项的结构
|
||||
population_data = data["data"]
|
||||
assert "year" in population_data, "数据缺少year字段"
|
||||
assert "total_population" in population_data, "数据缺少total_population字段"
|
||||
assert "birth_population" in population_data, "数据缺少birth_population字段"
|
||||
assert "urban_population" in population_data, "数据缺少urban_population字段"
|
||||
assert "rural_population" in population_data, "数据缺少rural_population字段"
|
||||
assert "urbanization_rate" in population_data, "数据缺少urbanization_rate字段"
|
||||
|
||||
# 检查数值类型
|
||||
assert isinstance(population_data["year"], int), "year应为整数类型"
|
||||
assert isinstance(population_data["total_population"], (int, float)), "total_population应为数值类型"
|
||||
assert isinstance(population_data["birth_population"], (int, float)), "birth_population应为数值类型"
|
||||
assert isinstance(population_data["urban_population"], (int, float)), "urban_population应为数值类型"
|
||||
assert isinstance(population_data["rural_population"], (int, float)), "rural_population应为数值类型"
|
||||
assert isinstance(population_data["urbanization_rate"], (int, float)), "urbanization_rate应为数值类型"
|
||||
|
||||
# 检查年份是否匹配
|
||||
assert population_data["year"] == year, f"年份不匹配,期望: {year},实际: {population_data['year']}"
|
||||
|
||||
print(f"✅ {year} 年人口数据验证通过")
|
||||
else:
|
||||
print(f"⚠️ {year} 年未返回人口数据")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
except AssertionError as e:
|
||||
print(f"❌ 数据验证失败: {e}")
|
||||
|
||||
# 添加短暂延迟,避免请求过于频繁
|
||||
time.sleep(0.5)
|
||||
|
||||
# 测试边界值
|
||||
print("\n===== 测试边界值 =====")
|
||||
|
||||
# 测试最小值年份
|
||||
min_year = 2015
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/populationByYear?year={min_year}"
|
||||
print(f"\n测试最小值年份 {min_year}:")
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
print(f"响应状态码: {response.status_code}")
|
||||
if data["code"] == 200 or data["code"] == 404:
|
||||
print(f"✅ 最小值年份测试通过")
|
||||
else:
|
||||
print(f"❌ 状态码异常: {data['code']}")
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
|
||||
# 测试最大值年份
|
||||
max_year = 2028
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/populationByYear?year={max_year}"
|
||||
print(f"\n测试最大值年份 {max_year}:")
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
print(f"响应状态码: {response.status_code}")
|
||||
if data["code"] == 200 or data["code"] == 404:
|
||||
print(f"✅ 最大值年份测试通过")
|
||||
else:
|
||||
print(f"❌ 状态码异常: {data['code']}")
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
|
||||
# 测试超出范围的年份
|
||||
invalid_year = 2030
|
||||
url = f"{EDUCATION_DATA_BASE_URL}/populationByYear?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"响应状态码: {response.status_code}")
|
||||
print(f"响应数据: {json.dumps(data, ensure_ascii=False, indent=2)}")
|
||||
print(f"❌ 期望状态码422,实际: {response.status_code}")
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ 请求失败: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 测试新添加的人口数据接口
|
||||
test_population_data_by_year()
|
Reference in New Issue
Block a user