247 lines
7.5 KiB
JavaScript
247 lines
7.5 KiB
JavaScript
|
|
/**
|
||
|
|
* 采购交易导入失败记录接口测试脚本
|
||
|
|
*
|
||
|
|
* 测试目标: 验证修复后的 /importFailures/{taskId} 接口返回正确的分页数据
|
||
|
|
*
|
||
|
|
* 使用方法:
|
||
|
|
* 1. 确保后端服务已启动
|
||
|
|
* 2. 先执行一次导入操作(包含失败数据)
|
||
|
|
* 3. 获取返回的taskId
|
||
|
|
* 4. 运行此脚本: node test-purchase-import-failures-api.js <taskId>
|
||
|
|
*/
|
||
|
|
|
||
|
|
const http = require('http');
|
||
|
|
|
||
|
|
const BASE_URL = 'localhost';
|
||
|
|
const PORT = 8080;
|
||
|
|
const USERNAME = 'admin';
|
||
|
|
const PASSWORD = 'admin123';
|
||
|
|
|
||
|
|
let authToken = null;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 登录获取token
|
||
|
|
*/
|
||
|
|
async function login() {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const postData = JSON.stringify({
|
||
|
|
username: USERNAME,
|
||
|
|
password: PASSWORD
|
||
|
|
});
|
||
|
|
|
||
|
|
const options = {
|
||
|
|
hostname: BASE_URL,
|
||
|
|
port: PORT,
|
||
|
|
path: '/login/test',
|
||
|
|
method: 'POST',
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Content-Length': Buffer.byteLength(postData)
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const req = http.request(options, (res) => {
|
||
|
|
let data = '';
|
||
|
|
|
||
|
|
res.on('data', (chunk) => {
|
||
|
|
data += chunk;
|
||
|
|
});
|
||
|
|
|
||
|
|
res.on('end', () => {
|
||
|
|
try {
|
||
|
|
const response = JSON.parse(data);
|
||
|
|
if (response.code === 200 && response.token) {
|
||
|
|
authToken = response.token;
|
||
|
|
console.log('✅ 登录成功,获取到token');
|
||
|
|
resolve();
|
||
|
|
} else {
|
||
|
|
reject(new Error('登录失败:' + JSON.stringify(response)));
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
reject(new Error('解析响应失败:' + error.message));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
req.on('error', (error) => {
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
req.write(postData);
|
||
|
|
req.end();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 测试导入失败记录接口
|
||
|
|
*/
|
||
|
|
async function testImportFailuresAPI(taskId) {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const path = `/ccdi/purchaseTransaction/importFailures/${taskId}?pageNum=1&pageSize=10`;
|
||
|
|
|
||
|
|
const options = {
|
||
|
|
hostname: BASE_URL,
|
||
|
|
port: PORT,
|
||
|
|
path: path,
|
||
|
|
method: 'GET',
|
||
|
|
headers: {
|
||
|
|
'Authorization': `Bearer ${authToken}`
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log(`\n📡 测试接口: GET ${path}`);
|
||
|
|
|
||
|
|
const req = http.request(options, (res) => {
|
||
|
|
let data = '';
|
||
|
|
|
||
|
|
res.on('data', (chunk) => {
|
||
|
|
data += chunk;
|
||
|
|
});
|
||
|
|
|
||
|
|
res.on('end', () => {
|
||
|
|
try {
|
||
|
|
const response = JSON.parse(data);
|
||
|
|
console.log('\n📥 响应状态码:', res.statusCode);
|
||
|
|
console.log('📦 响应数据:', JSON.stringify(response, null, 2));
|
||
|
|
|
||
|
|
// 验证响应结构
|
||
|
|
console.log('\n🔍 验证响应结构:');
|
||
|
|
|
||
|
|
if (response.code === 200) {
|
||
|
|
console.log(' ✅ code 字段正确: 200');
|
||
|
|
} else {
|
||
|
|
console.log(' ❌ code 字段错误:', response.code);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (response.rows !== undefined) {
|
||
|
|
console.log(' ✅ rows 字段存在, 类型:', Array.isArray(response.rows) ? 'Array' : typeof response.rows);
|
||
|
|
console.log(' ✅ rows 长度:', response.rows ? response.rows.length : 0);
|
||
|
|
|
||
|
|
if (response.rows && response.rows.length > 0) {
|
||
|
|
console.log('\n📄 第一条失败记录示例:');
|
||
|
|
console.log(JSON.stringify(response.rows[0], null, 2));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log(' ❌ rows 字段缺失');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (response.total !== undefined) {
|
||
|
|
console.log(' ✅ total 字段存在:', response.total);
|
||
|
|
} else {
|
||
|
|
console.log(' ❌ total 字段缺失');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试分页参数
|
||
|
|
console.log('\n📄 测试不同分页参数:');
|
||
|
|
testPagination(taskId, 1, 5).then(() => resolve(response));
|
||
|
|
} catch (error) {
|
||
|
|
reject(new Error('解析响应失败:' + error.message));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
req.on('error', (error) => {
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
req.end();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 测试分页功能
|
||
|
|
*/
|
||
|
|
async function testPagination(taskId, pageNum, pageSize) {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const path = `/ccdi/purchaseTransaction/importFailures/${taskId}?pageNum=${pageNum}&pageSize=${pageSize}`;
|
||
|
|
|
||
|
|
const options = {
|
||
|
|
hostname: BASE_URL,
|
||
|
|
port: PORT,
|
||
|
|
path: path,
|
||
|
|
method: 'GET',
|
||
|
|
headers: {
|
||
|
|
'Authorization': `Bearer ${authToken}`
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const req = http.request(options, (res) => {
|
||
|
|
let data = '';
|
||
|
|
|
||
|
|
res.on('data', (chunk) => {
|
||
|
|
data += chunk;
|
||
|
|
});
|
||
|
|
|
||
|
|
res.on('end', () => {
|
||
|
|
try {
|
||
|
|
const response = JSON.parse(data);
|
||
|
|
console.log(`\n 📌 分页测试 (pageNum=${pageNum}, pageSize=${pageSize}):`);
|
||
|
|
console.log(` 返回记录数: ${response.rows ? response.rows.length : 0}`);
|
||
|
|
console.log(` 总记录数: ${response.total || 0}`);
|
||
|
|
|
||
|
|
if (response.rows && response.rows.length <= pageSize) {
|
||
|
|
console.log(` ✅ 分页大小正确`);
|
||
|
|
} else {
|
||
|
|
console.log(` ❌ 分页大小错误,期望最多${pageSize}条`);
|
||
|
|
}
|
||
|
|
|
||
|
|
resolve();
|
||
|
|
} catch (error) {
|
||
|
|
reject(new Error('解析响应失败:' + error.message));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
req.on('error', (error) => {
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
req.end();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 主测试函数
|
||
|
|
*/
|
||
|
|
async function main() {
|
||
|
|
console.log('========================================');
|
||
|
|
console.log('采购交易导入失败记录接口测试');
|
||
|
|
console.log('========================================');
|
||
|
|
|
||
|
|
// 获取命令行参数
|
||
|
|
const taskId = process.argv[2];
|
||
|
|
|
||
|
|
if (!taskId) {
|
||
|
|
console.error('\n❌ 错误: 请提供任务ID');
|
||
|
|
console.error('\n使用方法: node test-purchase-import-failures-api.js <taskId>');
|
||
|
|
console.error('示例: node test-purchase-import-failures-api.js 1234567890\n');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`\n🎯 测试任务ID: ${taskId}`);
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 登录
|
||
|
|
await login();
|
||
|
|
|
||
|
|
// 测试接口
|
||
|
|
const result = await testImportFailuresAPI(taskId);
|
||
|
|
|
||
|
|
console.log('\n========================================');
|
||
|
|
console.log('✅ 测试完成!');
|
||
|
|
console.log('========================================\n');
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('\n❌ 测试失败:', error.message);
|
||
|
|
console.error('\n请检查:');
|
||
|
|
console.error('1. 后端服务是否已启动');
|
||
|
|
console.error('2. 任务ID是否正确');
|
||
|
|
console.error('3. 是否已执行过导入操作(包含失败数据)');
|
||
|
|
console.error('');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行测试
|
||
|
|
main();
|