Files
ccdi/doc/test-data/test-data/purchase_transaction/test-date-query.js
2026-02-09 14:28:25 +08:00

279 lines
9.3 KiB
JavaScript

/**
* 采购交易申请日期查询功能测试脚本
*
* 测试目的: 验证申请日期查询条件修复后能正常工作
* 问题描述: 之前申请日期查询条件未生效,原因是 Mapper XML 中存在两套参数名导致混乱
* 修复方案: 统一使用 applyDateStart 和 applyDateEnd 作为日期查询参数
*/
const axios = require('axios');
const fs = require('fs');
const BASE_URL = 'http://localhost:8080';
// 测试配置
const TEST_CONFIG = {
// 使用固定的测试账号
username: 'admin',
password: 'admin123',
};
/**
* 登录获取 token
*/
async function login() {
try {
console.log('📝 正在登录...');
const response = await axios.post(`${BASE_URL}/login/test`, {
username: TEST_CONFIG.username,
password: TEST_CONFIG.password
});
if (response.data.code === 200) {
const token = response.data.data.token;
console.log('✅ 登录成功!');
console.log(` Token: ${token.substring(0, 20)}...`);
return token;
} else {
throw new Error(`登录失败: ${response.data.msg}`);
}
} catch (error) {
console.error('❌ 登录失败:', error.message);
throw error;
}
}
/**
* 测试申请日期查询功能
*/
async function testDateQuery(token) {
const testResults = [];
const config = {
headers: {
'Authorization': `Bearer ${token}`
}
};
try {
console.log('\n📊 开始测试申请日期查询功能...\n');
// 测试1: 不带日期查询条件(获取所有数据)
console.log('测试1: 不带日期查询条件');
const response1 = await axios.get(`${BASE_URL}/ccdi/purchaseTransaction/list`, {
...config,
params: {
pageNum: 1,
pageSize: 10
}
});
const totalRecords = response1.data.total;
console.log(` 总记录数: ${totalRecords}`);
testResults.push({
test: '无日期条件查询',
status: response1.data.code === 200 ? '✅ 通过' : '❌ 失败',
total: totalRecords
});
if (totalRecords === 0) {
console.log('⚠️ 数据库中没有数据,无法继续测试日期查询功能');
return testResults;
}
// 测试2: 查询2024年的申请日期
console.log('\n测试2: 查询2024-01-01到2024-12-31的申请日期');
const response2 = await axios.get(`${BASE_URL}/ccdi/purchaseTransaction/list`, {
...config,
params: {
pageNum: 1,
pageSize: 10,
applyDateStart: '2024-01-01',
applyDateEnd: '2024-12-31'
}
});
const records2024 = response2.data.total;
console.log(` 2024年记录数: ${records2024}`);
testResults.push({
test: '2024年日期查询',
status: response2.data.code === 200 ? '✅ 通过' : '❌ 失败',
total: records2024,
params: 'applyDateStart=2024-01-01, applyDateEnd=2024-12-31'
});
// 测试3: 查询2025年的申请日期
console.log('\n测试3: 查询2025-01-01到2025-12-31的申请日期');
const response3 = await axios.get(`${BASE_URL}/ccdi/purchaseTransaction/list`, {
...config,
params: {
pageNum: 1,
pageSize: 10,
applyDateStart: '2025-01-01',
applyDateEnd: '2025-12-31'
}
});
const records2025 = response3.data.total;
console.log(` 2025年记录数: ${records2025}`);
testResults.push({
test: '2025年日期查询',
status: response3.data.code === 200 ? '✅ 通过' : '❌ 失败',
total: records2025,
params: 'applyDateStart=2025-01-01, applyDateEnd=2025-12-31'
});
// 测试4: 查询2026年2月的申请日期
console.log('\n测试4: 查询2026-02-01到2026-02-28的申请日期');
const response4 = await axios.get(`${BASE_URL}/ccdi/purchaseTransaction/list`, {
...config,
params: {
pageNum: 1,
pageSize: 10,
applyDateStart: '2026-02-01',
applyDateEnd: '2026-02-28'
}
});
const recordsFeb2026 = response4.data.total;
console.log(` 2026年2月记录数: ${recordsFeb2026}`);
testResults.push({
test: '2026年2月日期查询',
status: response4.data.code === 200 ? '✅ 通过' : '❌ 失败',
total: recordsFeb2026,
params: 'applyDateStart=2026-02-01, applyDateEnd=2026-02-28'
});
// 测试5: 只传入开始日期
console.log('\n测试5: 只传入开始日期(2024-01-01)');
const response5 = await axios.get(`${BASE_URL}/ccdi/purchaseTransaction/list`, {
...config,
params: {
pageNum: 1,
pageSize: 10,
applyDateStart: '2024-01-01'
}
});
const recordsFrom2024 = response5.data.total;
console.log(` 2024-01-01之后记录数: ${recordsFrom2024}`);
testResults.push({
test: '只有开始日期查询',
status: response5.data.code === 200 ? '✅ 通过' : '❌ 失败',
total: recordsFrom2024,
params: 'applyDateStart=2024-01-01'
});
// 测试6: 只传入结束日期
console.log('\n测试6: 只传入结束日期(2024-12-31)');
const response6 = await axios.get(`${BASE_URL}/ccdi/purchaseTransaction/list`, {
...config,
params: {
pageNum: 1,
pageSize: 10,
applyDateEnd: '2024-12-31'
}
});
const recordsUntil2024 = response6.data.total;
console.log(` 2024-12-31之前记录数: ${recordsUntil2024}`);
testResults.push({
test: '只有结束日期查询',
status: response6.data.code === 200 ? '✅ 通过' : '❌ 失败',
total: recordsUntil2024,
params: 'applyDateEnd=2024-12-31'
});
// 验证: 日期查询是否生效
console.log('\n🔍 验证结果:');
console.log(` 总记录数: ${totalRecords}`);
console.log(` 2024年: ${records2024}`);
console.log(` 2025年: ${records2025}`);
console.log(` 2026年2月: ${recordsFeb2026}`);
const dateQueryWorks = (records2024 !== totalRecords) ||
(records2025 !== totalRecords) ||
(recordsFeb2026 !== totalRecords);
if (dateQueryWorks) {
console.log(' ✅ 日期查询功能正常!不同日期范围返回不同的记录数');
} else {
console.log(' ⚠️ 日期查询可能未生效,所有日期范围返回相同记录数');
console.log(' 提示: 如果数据库中所有记录的申请日期都在同一个范围内,这是正常现象');
}
} catch (error) {
console.error('❌ 测试失败:', error.message);
if (error.response) {
console.error(' 响应数据:', error.response.data);
}
testResults.push({
test: '异常',
status: '❌ 失败',
error: error.message
});
}
return testResults;
}
/**
* 生成测试报告
*/
function generateReport(testResults, testResultsPath) {
const report = {
testDate: new Date().toISOString(),
description: '采购交易申请日期查询功能测试报告',
issue: '申请日期查询条件未生效',
fix: '统一使用 applyDateStart 和 applyDateEnd 作为日期查询参数',
results: testResults
};
fs.writeFileSync(testResultsPath, JSON.stringify(report, null, 2));
console.log(`\n📄 测试报告已保存: ${testResultsPath}`);
}
/**
* 主函数
*/
async function main() {
console.log('=================================');
console.log('采购交易申请日期查询功能测试');
console.log('=================================\n');
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
const testResultsPath = `doc/test-results/purchase-transaction-date-query-${timestamp}.json`;
// 确保测试结果目录存在
const testResultsDir = 'doc/test-results';
if (!fs.existsSync(testResultsDir)) {
fs.mkdirSync(testResultsDir, { recursive: true });
}
try {
// 登录获取token
const token = await login();
// 测试日期查询功能
const testResults = await testDateQuery(token);
// 生成测试报告
generateReport(testResults, testResultsPath);
console.log('\n=================================');
console.log('✅ 测试完成!');
console.log('=================================\n');
// 显示汇总
const passedTests = testResults.filter(r => r.status.includes('通过')).length;
const totalTests = testResults.length;
console.log(`测试结果: ${passedTests}/${totalTests} 通过`);
} catch (error) {
console.error('\n❌ 测试失败:', error.message);
process.exit(1);
}
}
// 运行测试
main();