269 lines
9.5 KiB
Python
269 lines
9.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
学生信息管理SQL修复测试脚本
|
|
功能:验证SQL重复子句错误修复是否成功
|
|
时间:2025-01-14
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
import time
|
|
from datetime import datetime
|
|
|
|
class StudentInfoSQLFixTest:
|
|
def __init__(self):
|
|
self.base_url = "http://localhost:8080"
|
|
self.headers = {
|
|
'Content-Type': 'application/json',
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
}
|
|
self.token = None
|
|
|
|
def login(self):
|
|
"""登录获取token"""
|
|
print("=== 登录测试 ===")
|
|
login_url = f"{self.base_url}/login"
|
|
|
|
login_data = {
|
|
"username": "admin",
|
|
"password": "admin123"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(login_url, json=login_data, headers=self.headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get('code') == 200:
|
|
self.token = result.get('data', {}).get('access_token')
|
|
self.headers['Authorization'] = f'Bearer {self.token}'
|
|
print("✅ 登录成功")
|
|
return True
|
|
else:
|
|
print(f"❌ 登录失败: {result.get('msg')}")
|
|
return False
|
|
else:
|
|
print(f"❌ 登录请求失败: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 登录异常: {str(e)}")
|
|
return False
|
|
|
|
def test_basic_query(self):
|
|
"""测试基础查询功能"""
|
|
print("\n=== 基础查询测试 ===")
|
|
|
|
url = f"{self.base_url}/xg/studentInfo/list"
|
|
params = {
|
|
'pageNum': 1,
|
|
'pageSize': 10
|
|
}
|
|
|
|
try:
|
|
response = requests.get(url, params=params, headers=self.headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get('code') == 200:
|
|
total = result.get('total', 0)
|
|
rows = result.get('rows', [])
|
|
print(f"✅ 基础查询成功,总数: {total}, 当前页: {len(rows)}")
|
|
return True
|
|
else:
|
|
print(f"❌ 基础查询失败: {result.get('msg')}")
|
|
return False
|
|
else:
|
|
print(f"❌ 基础查询请求失败: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 基础查询异常: {str(e)}")
|
|
return False
|
|
|
|
def test_gender_query(self):
|
|
"""测试性别查询(之前报错的查询)"""
|
|
print("\n=== 性别查询测试 ===")
|
|
|
|
url = f"{self.base_url}/xg/studentInfo/list"
|
|
params = {
|
|
'pageNum': 1,
|
|
'pageSize': 10,
|
|
'gender': 'M' # 男性
|
|
}
|
|
|
|
try:
|
|
response = requests.get(url, params=params, headers=self.headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get('code') == 200:
|
|
total = result.get('total', 0)
|
|
rows = result.get('rows', [])
|
|
print(f"✅ 性别查询成功,总数: {total}, 当前页: {len(rows)}")
|
|
|
|
# 验证查询结果
|
|
if rows:
|
|
for row in rows:
|
|
if row.get('gender') != 'M':
|
|
print(f"⚠️ 查询结果不匹配: {row.get('name')} - {row.get('gender')}")
|
|
return False
|
|
print("✅ 查询结果验证通过")
|
|
|
|
return True
|
|
else:
|
|
print(f"❌ 性别查询失败: {result.get('msg')}")
|
|
return False
|
|
else:
|
|
print(f"❌ 性别查询请求失败: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 性别查询异常: {str(e)}")
|
|
return False
|
|
|
|
def test_multi_condition_query(self):
|
|
"""测试多条件查询"""
|
|
print("\n=== 多条件查询测试 ===")
|
|
|
|
url = f"{self.base_url}/xg/studentInfo/list"
|
|
params = {
|
|
'pageNum': 1,
|
|
'pageSize': 10,
|
|
'gender': 'F', # 女性
|
|
'grade': '2024' # 2024级
|
|
}
|
|
|
|
try:
|
|
response = requests.get(url, params=params, headers=self.headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get('code') == 200:
|
|
total = result.get('total', 0)
|
|
rows = result.get('rows', [])
|
|
print(f"✅ 多条件查询成功,总数: {total}, 当前页: {len(rows)}")
|
|
return True
|
|
else:
|
|
print(f"❌ 多条件查询失败: {result.get('msg')}")
|
|
return False
|
|
else:
|
|
print(f"❌ 多条件查询请求失败: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 多条件查询异常: {str(e)}")
|
|
return False
|
|
|
|
def test_name_query(self):
|
|
"""测试姓名模糊查询"""
|
|
print("\n=== 姓名模糊查询测试 ===")
|
|
|
|
url = f"{self.base_url}/xg/studentInfo/list"
|
|
params = {
|
|
'pageNum': 1,
|
|
'pageSize': 10,
|
|
'name': '张' # 姓张的学生
|
|
}
|
|
|
|
try:
|
|
response = requests.get(url, params=params, headers=self.headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get('code') == 200:
|
|
total = result.get('total', 0)
|
|
rows = result.get('rows', [])
|
|
print(f"✅ 姓名模糊查询成功,总数: {total}, 当前页: {len(rows)}")
|
|
return True
|
|
else:
|
|
print(f"❌ 姓名模糊查询失败: {result.get('msg')}")
|
|
return False
|
|
else:
|
|
print(f"❌ 姓名模糊查询请求失败: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 姓名模糊查询异常: {str(e)}")
|
|
return False
|
|
|
|
def test_pagination(self):
|
|
"""测试分页功能"""
|
|
print("\n=== 分页功能测试 ===")
|
|
|
|
url = f"{self.base_url}/xg/studentInfo/list"
|
|
|
|
# 测试第一页
|
|
params1 = {'pageNum': 1, 'pageSize': 5}
|
|
# 测试第二页
|
|
params2 = {'pageNum': 2, 'pageSize': 5}
|
|
|
|
try:
|
|
response1 = requests.get(url, params=params1, headers=self.headers)
|
|
response2 = requests.get(url, params=params2, headers=self.headers)
|
|
|
|
if response1.status_code == 200 and response2.status_code == 200:
|
|
result1 = response1.json()
|
|
result2 = response2.json()
|
|
|
|
if result1.get('code') == 200 and result2.get('code') == 200:
|
|
rows1 = result1.get('rows', [])
|
|
rows2 = result2.get('rows', [])
|
|
|
|
print(f"✅ 分页功能测试成功")
|
|
print(f" 第一页记录数: {len(rows1)}")
|
|
print(f" 第二页记录数: {len(rows2)}")
|
|
return True
|
|
else:
|
|
print(f"❌ 分页功能测试失败")
|
|
return False
|
|
else:
|
|
print(f"❌ 分页功能请求失败")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 分页功能异常: {str(e)}")
|
|
return False
|
|
|
|
def run_all_tests(self):
|
|
"""运行所有测试"""
|
|
print("=" * 50)
|
|
print("学生信息管理SQL修复测试")
|
|
print(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
print("=" * 50)
|
|
|
|
# 登录
|
|
if not self.login():
|
|
print("❌ 登录失败,测试终止")
|
|
return False
|
|
|
|
# 等待服务启动
|
|
print("\n⏳ 等待服务完全启动...")
|
|
time.sleep(5)
|
|
|
|
# 运行测试
|
|
tests = [
|
|
("基础查询", self.test_basic_query),
|
|
("性别查询", self.test_gender_query),
|
|
("多条件查询", self.test_multi_condition_query),
|
|
("姓名模糊查询", self.test_name_query),
|
|
("分页功能", self.test_pagination),
|
|
]
|
|
|
|
passed = 0
|
|
total = len(tests)
|
|
|
|
for test_name, test_func in tests:
|
|
if test_func():
|
|
passed += 1
|
|
time.sleep(1) # 测试间隔
|
|
|
|
print("\n" + "=" * 50)
|
|
print("测试结果总结")
|
|
print("=" * 50)
|
|
print(f"总测试数: {total}")
|
|
print(f"通过测试: {passed}")
|
|
print(f"失败测试: {total - passed}")
|
|
print(f"通过率: {passed/total*100:.1f}%")
|
|
|
|
if passed == total:
|
|
print("🎉 所有测试通过!SQL修复成功!")
|
|
return True
|
|
else:
|
|
print("❌ 部分测试失败,请检查修复")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
tester = StudentInfoSQLFixTest()
|
|
tester.run_all_tests() |