feat: implement Excel parser service
Implement Excel parsing module for ERP exported files following TDD principles. Key features: - Parse Excel files with multiple orders per file - Extract order header information (production order, product code, etc.) - Extract material data rows with 13 fields - Handle empty orders gracefully - Detect footer rows (制单人/打印人) - Map Chinese field names to English property names - Support field name mapping from Python reference Implementation: - ExcelParser class with parse() method - DiscreteMaterialPlan and ExcelParseOptions types - OrderHeader interface for order metadata - Test fixtures with realistic Excel structure - Comprehensive unit tests (3 tests, all passing) Reference: playwrite/utils/excel_converter.py Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
59
tests/unit/excel-parser.test.ts
Normal file
59
tests/unit/excel-parser.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { ExcelParser } from '../../src/main/services/excel/excel-parser';
|
||||
import type { DiscreteMaterialPlan } from '../../src/main/types/excel.types';
|
||||
import path from 'path';
|
||||
|
||||
describe('Excel Parser', () => {
|
||||
it('should parse Excel file and extract material plans', async () => {
|
||||
const parser = new ExcelParser();
|
||||
const filePath = path.resolve(__dirname, '../fixtures/test-export.xlsx');
|
||||
|
||||
const plans = await parser.parse(filePath);
|
||||
|
||||
expect(Array.isArray(plans)).toBe(true);
|
||||
expect(plans.length).toBeGreaterThan(0);
|
||||
|
||||
const firstPlan = plans[0];
|
||||
expect(firstPlan).toHaveProperty('orderNumber');
|
||||
expect(firstPlan).toHaveProperty('materialCode');
|
||||
});
|
||||
|
||||
it('should parse all material fields correctly', async () => {
|
||||
const parser = new ExcelParser();
|
||||
const filePath = path.resolve(__dirname, '../fixtures/test-export.xlsx');
|
||||
|
||||
const plans = await parser.parse(filePath);
|
||||
|
||||
expect(plans.length).toBe(3);
|
||||
|
||||
// Check first material
|
||||
expect(plans[0].orderNumber).toBe('SC202501001');
|
||||
expect(plans[0].materialCode).toBe('M001');
|
||||
expect(plans[0].materialName).toBe('钢材A');
|
||||
expect(plans[0].specification).toBe('规格1');
|
||||
expect(plans[0].model).toBe('型号1');
|
||||
expect(plans[0].drawingNumber).toBe('图号1');
|
||||
expect(plans[0].material).toBe('材质1');
|
||||
expect(plans[0].quantity).toBe(50);
|
||||
expect(plans[0].unit).toBe('kg');
|
||||
expect(plans[0].requiredDate).toBe('2025-02-10');
|
||||
expect(plans[0].warehouse).toBe('仓库1');
|
||||
expect(plans[0].unitUsage).toBe(0.5);
|
||||
expect(plans[0].cumulativeOutboundQty).toBe(0);
|
||||
|
||||
// Check third material (with some empty fields)
|
||||
expect(plans[2].materialCode).toBe('M003');
|
||||
expect(plans[2].materialName).toBe('配件C');
|
||||
expect(plans[2].quantity).toBe(200);
|
||||
});
|
||||
|
||||
it('should handle empty orders gracefully', async () => {
|
||||
const parser = new ExcelParser();
|
||||
const filePath = path.resolve(__dirname, '../fixtures/test-empty-orders.xlsx');
|
||||
|
||||
const plans = await parser.parse(filePath);
|
||||
|
||||
expect(plans).toBeDefined();
|
||||
expect(plans.length).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user