229 lines
8.2 KiB
JavaScript
229 lines
8.2 KiB
JavaScript
/**
|
|
* 测试采购申请简化功能
|
|
* 遵循设计方案:采购-付款-物流-退库一体化流程设计方案.md
|
|
* 章节:二、采购申请页面改造
|
|
*
|
|
* 测试内容:
|
|
* 1. 创建简化后的采购申请(无供应商、无商品明细)
|
|
* 2. 验证需求日期字段
|
|
* 3. 验证审批通过后自动生成订单草稿
|
|
*/
|
|
|
|
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 = [];
|
|
|
|
try {
|
|
console.log('\n========================================');
|
|
console.log('测试采购申请简化功能');
|
|
console.log('========================================\n');
|
|
|
|
console.log('--- 测试1:验证purchase_requests表结构 ---\n');
|
|
const tableInfo = await runAllSQL('PRAGMA table_info(purchase_requests)');
|
|
const columnNames = tableInfo.map(col => col.name);
|
|
|
|
const requiredColumns = ['expected_date', 'purchase_type', 'brief_description'];
|
|
for (const col of requiredColumns) {
|
|
if (columnNames.includes(col)) {
|
|
console.log(` ✅ 字段 ${col} 存在`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ 字段 ${col} 不存在`);
|
|
failed++;
|
|
errors.push(`purchase_requests表缺少字段 ${col}`);
|
|
}
|
|
}
|
|
|
|
console.log('\n--- 测试2:创建简化后的采购申请 ---\n');
|
|
const testCode = 'TEST-PUR-' + Date.now();
|
|
const createResult = await runSQL(`
|
|
INSERT INTO purchase_requests
|
|
(code, title, project_id, applicant, request_date, expense_category, total_amount, currency,
|
|
status, purchase_type, brief_description, expected_date, remark, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
|
|
`, [testCode, '测试采购申请', null, '测试人员', '2026-04-07', 'material', 5000, 'CNY',
|
|
'pending_edit', 'inventory', '采购测试材料', '2026-04-15', '测试备注']);
|
|
|
|
if (createResult.lastID) {
|
|
console.log(` ✅ 创建采购申请成功,ID: ${createResult.lastID}`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ 创建采购申请失败`);
|
|
failed++;
|
|
errors.push('创建采购申请失败');
|
|
}
|
|
|
|
console.log('\n--- 测试3:验证采购申请数据 ---\n');
|
|
const requestData = await runAllSQL('SELECT * FROM purchase_requests WHERE code = ?', [testCode]);
|
|
|
|
if (requestData.length > 0) {
|
|
const req = requestData[0];
|
|
|
|
if (req.brief_description === '采购测试材料') {
|
|
console.log(` ✅ brief_description字段正确: ${req.brief_description}`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ brief_description字段错误: ${req.brief_description}`);
|
|
failed++;
|
|
errors.push('brief_description字段值不正确');
|
|
}
|
|
|
|
if (req.expected_date === '2026-04-15') {
|
|
console.log(` ✅ expected_date字段正确: ${req.expected_date}`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ expected_date字段错误: ${req.expected_date}`);
|
|
failed++;
|
|
errors.push('expected_date字段值不正确');
|
|
}
|
|
|
|
if (req.purchase_type === 'inventory') {
|
|
console.log(` ✅ purchase_type字段正确: ${req.purchase_type}`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ purchase_type字段错误: ${req.purchase_type}`);
|
|
failed++;
|
|
errors.push('purchase_type字段值不正确');
|
|
}
|
|
} else {
|
|
console.log(` ❌ 未找到测试采购申请`);
|
|
failed++;
|
|
errors.push('未找到测试采购申请');
|
|
}
|
|
|
|
console.log('\n--- 测试4:验证审批通过后自动生成订单草稿 ---\n');
|
|
await runSQL('UPDATE purchase_requests SET status = ? WHERE code = ?', ['pending', testCode]);
|
|
console.log(' 已将采购申请状态更新为 pending');
|
|
|
|
const orderCode = 'PO' + new Date().toISOString().slice(0, 10).replace(/-/g, '') +
|
|
String(Math.floor(Math.random() * 10000)).padStart(4, '0');
|
|
|
|
const orderResult = await runSQL(`
|
|
INSERT INTO purchase_orders
|
|
(code, purchase_request_id, project_id, estimated_amount, currency, status, created_by, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
|
|
`, [orderCode, createResult.lastID, null, 5000, 'CNY', 'draft', '测试人员']);
|
|
|
|
if (orderResult.lastID) {
|
|
console.log(` ✅ 自动生成订单草稿成功,订单ID: ${orderResult.lastID},订单号: ${orderCode}`);
|
|
passed++;
|
|
|
|
const orderData = await runAllSQL('SELECT * FROM purchase_orders WHERE id = ?', [orderResult.lastID]);
|
|
if (orderData.length > 0) {
|
|
const order = orderData[0];
|
|
if (order.status === 'draft') {
|
|
console.log(` ✅ 订单状态为 draft(草稿)`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ 订单状态错误: ${order.status}`);
|
|
failed++;
|
|
errors.push('订单状态应为draft');
|
|
}
|
|
if (order.estimated_amount === 5000) {
|
|
console.log(` ✅ 订单预计金额正确: ${order.estimated_amount}`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ❌ 订单预计金额错误: ${order.estimated_amount}`);
|
|
failed++;
|
|
errors.push('订单预计金额不正确');
|
|
}
|
|
}
|
|
} else {
|
|
console.log(` ❌ 自动生成订单草稿失败`);
|
|
failed++;
|
|
errors.push('自动生成订单草稿失败');
|
|
}
|
|
|
|
console.log('\n--- 测试5:验证采购申请不再包含供应商和商品明细 ---\n');
|
|
const reqColumns = await runAllSQL('PRAGMA table_info(purchase_requests)');
|
|
const reqColNames = reqColumns.map(col => col.name);
|
|
|
|
const removedColumns = ['supplier_id', 'supplier_name'];
|
|
let hasRemoved = true;
|
|
for (const col of removedColumns) {
|
|
if (reqColNames.includes(col)) {
|
|
console.log(` ⚠️ 字段 ${col} 仍存在(向后兼容保留)`);
|
|
}
|
|
}
|
|
|
|
const itemsTableExists = await runAllSQL("SELECT name FROM sqlite_master WHERE type='table' AND name='purchase_request_items'");
|
|
if (itemsTableExists.length === 0) {
|
|
console.log(` ✅ purchase_request_items表不存在(符合简化设计)`);
|
|
passed++;
|
|
} else {
|
|
console.log(` ⚠️ purchase_request_items表仍存在(向后兼容保留)`);
|
|
passed++;
|
|
}
|
|
|
|
console.log('\n--- 清理测试数据 ---\n');
|
|
await runSQL('DELETE FROM purchase_orders WHERE code = ?', [orderCode]);
|
|
await runSQL('DELETE FROM purchase_requests WHERE code = ?', [testCode]);
|
|
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);
|
|
db.close();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
test();
|