Files
yunhaifinance/backend/test-logistics-companies.js
T

242 lines
8.7 KiB
JavaScript

/**
* 测试跨境物流公司管理功能
* 遵循设计方案:采购-付款-物流-退库一体化流程设计方案.md
* 章节:四、跨境物流公司管理
*
* 测试内容:
* 1. 创建物流公司
* 2. 添加联系人
* 3. 添加收款信息
* 4. 验证业务台账统计
*/
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const dbPath = path.join(__dirname, 'company_finance.db');
const db = new sqlite3.Database(dbPath, (err) => {
if (err) {
console.error('数据库连接失败:', err.message);
process.exit(1);
}
console.log('SQLite数据库连接成功:', dbPath);
});
const runSQL = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.run(sql, params, function(err) {
if (err) {
reject(err);
} else {
resolve({ lastID: this.lastID, changes: this.changes });
}
});
});
};
const runAllSQL = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.all(sql, params, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
};
async function test() {
let passed = 0;
let failed = 0;
const errors = [];
let testCompanyId = null;
let testContactId = null;
let testPaymentId = null;
try {
console.log('\n========================================');
console.log('测试跨境物流公司管理功能');
console.log('========================================\n');
console.log('--- 测试1:验证数据表结构 ---\n');
const tables = ['logistics_companies', 'logistics_company_contacts', 'logistics_company_payment_infos'];
for (const table of tables) {
const tableInfo = await runAllSQL(`PRAGMA table_info(${table})`);
if (tableInfo.length > 0) {
console.log(` ✅ 表 ${table} 存在,共 ${tableInfo.length} 个字段`);
passed++;
} else {
console.log(` ❌ 表 ${table} 不存在`);
failed++;
errors.push(`表 ${table} 不存在`);
}
}
console.log('\n--- 测试2:创建物流公司 ---\n');
const companyCode = 'TEST-LC-' + Date.now();
const createResult = await runSQL(`
INSERT INTO logistics_companies
(code, name, address, phone, email, quotation_description, status, remark, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, 'active', ?, datetime('now'), datetime('now'))
`, [companyCode, '测试物流公司', '中国云南省', '13800138000', 'test@logistics.com', '中国-老挝陆运,时效5-7天', '测试备注']);
if (createResult.lastID) {
testCompanyId = createResult.lastID;
console.log(` ✅ 创建物流公司成功,ID: ${testCompanyId},编码: ${companyCode}`);
passed++;
} else {
console.log(` ❌ 创建物流公司失败`);
failed++;
errors.push('创建物流公司失败');
}
console.log('\n--- 测试3:添加联系人 ---\n');
const contactResult = await runSQL(`
INSERT INTO logistics_company_contacts
(logistics_company_id, name, phone, email, position, is_primary, created_at)
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))
`, [testCompanyId, '张三', '13900139000', 'zhangsan@logistics.com', '业务经理', 1]);
if (contactResult.lastID) {
testContactId = contactResult.lastID;
console.log(` ✅ 添加联系人成功,ID: ${testContactId}`);
passed++;
} else {
console.log(` ❌ 添加联系人失败`);
failed++;
errors.push('添加联系人失败');
}
const contact2Result = await runSQL(`
INSERT INTO logistics_company_contacts
(logistics_company_id, name, phone, email, position, is_primary, created_at)
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))
`, [testCompanyId, '李四', '13900139001', 'lisi@logistics.com', '财务', 0]);
if (contact2Result.lastID) {
console.log(` ✅ 添加第二个联系人成功`);
passed++;
}
console.log('\n--- 测试4:添加收款信息 ---\n');
const paymentResult = await runSQL(`
INSERT INTO logistics_company_payment_infos
(logistics_company_id, account_name, account_number, bank_name, qr_code, is_default, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
`, [testCompanyId, '测试物流公司', '6222021234567890123', '中国工商银行', '', 1]);
if (paymentResult.lastID) {
testPaymentId = paymentResult.lastID;
console.log(` ✅ 添加收款信息成功,ID: ${testPaymentId}`);
passed++;
} else {
console.log(` ❌ 添加收款信息失败`);
failed++;
errors.push('添加收款信息失败');
}
console.log('\n--- 测试5:验证统一合作伙伴界面规范 ---\n');
const companyData = await runAllSQL('SELECT * FROM logistics_companies WHERE id = ?', [testCompanyId]);
const contactsData = await runAllSQL('SELECT * FROM logistics_company_contacts WHERE logistics_company_id = ?', [testCompanyId]);
const paymentData = await runAllSQL('SELECT * FROM logistics_company_payment_infos WHERE logistics_company_id = ?', [testCompanyId]);
if (companyData.length > 0) {
console.log(` ✅ 基本信息TAB: 公司名 ${companyData[0].name}`);
passed++;
}
if (contactsData.length === 2) {
console.log(` ✅ 联系人TAB: ${contactsData.length} 个联系人`);
passed++;
} else {
console.log(` ❌ 联系人TAB: 数量错误 ${contactsData.length}`);
failed++;
}
if (paymentData.length === 1) {
console.log(` ✅ 收款信息TAB: ${paymentData.length} 个收款账户`);
passed++;
} else {
console.log(` ❌ 收款信息TAB: 数量错误 ${paymentData.length}`);
failed++;
}
console.log('\n--- 测试6:验证主联系人/默认账户标记 ---\n');
const primaryContact = contactsData.find(c => c.is_primary === 1);
if (primaryContact && primaryContact.name === '张三') {
console.log(` ✅ 主联系人标记正确: ${primaryContact.name}`);
passed++;
} else {
console.log(` ❌ 主联系人标记错误`);
failed++;
}
const defaultPayment = paymentData.find(p => p.is_default === 1);
if (defaultPayment) {
console.log(` ✅ 默认账户标记正确: ${defaultPayment.account_name}`);
passed++;
} else {
console.log(` ❌ 默认账户标记错误`);
failed++;
}
console.log('\n--- 测试7:验证业务台账统计 ---\n');
const statsResult = await runAllSQL(`
SELECT
COUNT(*) as order_count,
COALESCE(SUM(primary_freight), 0) as total_primary,
COALESCE(SUM(secondary_freight), 0) as total_secondary
FROM logistics_records
WHERE logistics_company_id = ?
`, [testCompanyId]);
console.log(` 业务台账统计:`);
console.log(` 订单数量: ${statsResult[0]?.order_count || 0}`);
console.log(` 一次运费总额: ${statsResult[0]?.total_primary || 0}`);
console.log(` 二次运费总额: ${statsResult[0]?.total_secondary || 0}`);
passed++;
console.log('\n--- 清理测试数据 ---\n');
await runSQL('DELETE FROM logistics_company_payment_infos WHERE logistics_company_id = ?', [testCompanyId]);
await runSQL('DELETE FROM logistics_company_contacts WHERE logistics_company_id = ?', [testCompanyId]);
await runSQL('DELETE FROM logistics_companies WHERE id = ?', [testCompanyId]);
console.log(' 测试数据已清理');
console.log('\n========================================');
console.log('测试结果汇总');
console.log('========================================\n');
console.log(`通过: ${passed}`);
console.log(`失败: ${failed}`);
if (errors.length > 0) {
console.log('\n错误详情:');
errors.forEach(err => console.log(` - ${err}`));
}
console.log('\n========================================');
if (failed === 0) {
console.log('✅ 所有测试通过!跨境物流公司管理功能符合设计方案。');
} else {
console.log('❌ 部分测试失败,请检查错误详情。');
}
console.log('========================================\n');
db.close();
process.exit(failed > 0 ? 1 : 0);
} catch (error) {
console.error('测试失败:', error);
if (testCompanyId) {
try {
await runSQL('DELETE FROM logistics_company_payment_infos WHERE logistics_company_id = ?', [testCompanyId]);
await runSQL('DELETE FROM logistics_company_contacts WHERE logistics_company_id = ?', [testCompanyId]);
await runSQL('DELETE FROM logistics_companies WHERE id = ?', [testCompanyId]);
} catch (e) {}
}
db.close();
process.exit(1);
}
}
test();