195 lines
6.6 KiB
Python
195 lines
6.6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
SQL修复验证测试
|
|
直接测试学生信息管理SQL语句是否修复了重复WHERE子句问题
|
|
"""
|
|
|
|
import time
|
|
import requests
|
|
import json
|
|
from datetime import datetime
|
|
|
|
# 配置
|
|
BASE_URL = "http://localhost:8080"
|
|
HEADERS = {
|
|
'Content-Type': 'application/json',
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
|
|
}
|
|
|
|
def test_service_health():
|
|
"""测试服务健康状态"""
|
|
print("=== 测试服务健康状态 ===")
|
|
|
|
try:
|
|
response = requests.get(f"{BASE_URL}/", headers=HEADERS, timeout=5)
|
|
print(f"服务状态码: {response.status_code}")
|
|
print(f"服务响应: {response.text[:100]}...")
|
|
|
|
if response.status_code == 200:
|
|
print("✅ 服务正常运行")
|
|
return True
|
|
else:
|
|
print(f"⚠️ 服务状态异常: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 服务连接失败: {e}")
|
|
return False
|
|
|
|
def test_sql_error_pattern():
|
|
"""测试SQL错误模式"""
|
|
print("\n=== 测试SQL错误模式 ===")
|
|
|
|
# 测试会触发SQL错误的请求
|
|
search_params = {
|
|
'pageNum': 1,
|
|
'pageSize': 10,
|
|
'gender': '男', # 这个参数会触发QueryWrapper生成WHERE子句
|
|
}
|
|
|
|
try:
|
|
response = requests.get(f"{BASE_URL}/xg/studentInfo/list",
|
|
params=search_params,
|
|
headers=HEADERS,
|
|
timeout=10)
|
|
print(f"请求状态码: {response.status_code}")
|
|
print(f"响应内容: {response.text[:400]}...")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
if data.get('code') == 401:
|
|
print("✅ 正常返回401认证错误(预期行为)")
|
|
return True
|
|
elif data.get('code') == 500:
|
|
error_msg = data.get('msg', '')
|
|
if "WHERE deleted = 0 AND tenant_id = '000000' WHERE" in error_msg:
|
|
print("❌ 仍然存在重复WHERE子句错误")
|
|
print("🔧 XML映射文件修复可能没有生效")
|
|
return False
|
|
else:
|
|
print("❌ 其他500错误")
|
|
print(f"错误信息: {error_msg}")
|
|
return False
|
|
else:
|
|
print(f"⚠️ 意外的响应码: {data.get('code')}")
|
|
return False
|
|
elif response.status_code == 500:
|
|
# 直接500错误
|
|
response_text = response.text
|
|
if "WHERE deleted = 0 AND tenant_id = '000000' WHERE" in response_text:
|
|
print("❌ 仍然存在重复WHERE子句错误")
|
|
print("🔧 XML映射文件修复可能没有生效")
|
|
return False
|
|
else:
|
|
print("❌ 其他500错误")
|
|
print(f"错误详情: {response_text[:200]}...")
|
|
return False
|
|
else:
|
|
print(f"⚠️ 预期200状态码,实际: {response.status_code}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 请求异常: {e}")
|
|
return False
|
|
|
|
def test_xml_file_content():
|
|
"""验证XML文件内容"""
|
|
print("\n=== 验证XML文件内容 ===")
|
|
|
|
xml_file_path = "../backend/ruoyi-modules/ruoyi-xg-xx/src/main/resources/mapper/xg/XgStudentInfoMapper.xml"
|
|
|
|
try:
|
|
with open(xml_file_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# 检查是否包含正确的SQL结构
|
|
if "WHERE deleted = 0 AND tenant_id = '000000'\n ${ew.customSqlSegment}" in content:
|
|
print("✅ XML文件包含正确的SQL结构")
|
|
return True
|
|
elif "<where>" in content and "${ew.customSqlSegment}" in content:
|
|
print("❌ XML文件仍然使用<where>标签包裹customSqlSegment")
|
|
print("🔧 需要手动修复XML文件")
|
|
return False
|
|
else:
|
|
print("⚠️ XML文件结构不明确")
|
|
print("🔍 让我检查具体内容...")
|
|
|
|
# 查找selectVoPage方法
|
|
if "selectVoPage" in content:
|
|
lines = content.split('\n')
|
|
for i, line in enumerate(lines):
|
|
if "selectVoPage" in line:
|
|
print(f"找到selectVoPage方法,周围内容:")
|
|
start = max(0, i-2)
|
|
end = min(len(lines), i+10)
|
|
for j in range(start, end):
|
|
print(f"{j+1:3d}: {lines[j]}")
|
|
break
|
|
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 读取XML文件失败: {e}")
|
|
return False
|
|
|
|
def show_correct_vs_incorrect():
|
|
"""显示正确与错误的SQL结构对比"""
|
|
print("\n=== SQL结构对比 ===")
|
|
|
|
print("✅ 正确的XML结构:")
|
|
print("""
|
|
<select id="selectVoPage" resultMap="XgStudentInfoResult">
|
|
SELECT ... FROM xg_student_info
|
|
WHERE deleted = 0 AND tenant_id = '000000'
|
|
${ew.customSqlSegment}
|
|
</select>
|
|
""")
|
|
|
|
print("❌ 错误的XML结构:")
|
|
print("""
|
|
<select id="selectVoPage" resultMap="XgStudentInfoResult">
|
|
SELECT ... FROM xg_student_info
|
|
<where>
|
|
deleted = 0 AND tenant_id = '000000'
|
|
${ew.customSqlSegment}
|
|
</where>
|
|
</select>
|
|
""")
|
|
|
|
print("🔍 错误原因:")
|
|
print("- MyBatis-Plus的${ew.customSqlSegment}可能已经包含WHERE关键字")
|
|
print("- 使用<where>标签包裹会导致重复的WHERE子句")
|
|
print("- 正确做法是直接在SQL中写WHERE条件,然后跟${ew.customSqlSegment}")
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("🚀 SQL修复验证测试")
|
|
print("=" * 50)
|
|
|
|
# 测试服务健康状态
|
|
if not test_service_health():
|
|
print("❌ 服务未正常运行,无法进行测试")
|
|
return
|
|
|
|
# 验证XML文件内容
|
|
xml_ok = test_xml_file_content()
|
|
|
|
# 测试SQL错误模式
|
|
sql_ok = test_sql_error_pattern()
|
|
|
|
# 显示结构对比
|
|
show_correct_vs_incorrect()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("📊 测试结果总结:")
|
|
print(f"XML文件结构: {'✅ 正确' if xml_ok else '❌ 错误'}")
|
|
print(f"SQL执行结果: {'✅ 正常' if sql_ok else '❌ 异常'}")
|
|
|
|
if xml_ok and sql_ok:
|
|
print("🎉 SQL修复验证通过!")
|
|
elif xml_ok and not sql_ok:
|
|
print("⚠️ XML文件已修复,但可能需要重启服务")
|
|
else:
|
|
print("❌ 需要检查XML文件修复")
|
|
|
|
if __name__ == "__main__":
|
|
main() |