import { expect } from 'chai'; import { rm } from 'fs/promises'; import { handleCsv } from '../../src/handle-csv'; import { createFile, readFile } from '../../src/utils/file'; import { getTestPath, randomString } from '../utils/utils'; describe('handle-csv/index', () => { let inputFilePath: string; let successOutputFilePath: string; let errorOutputFilePath: string; beforeEach(() => { const fileName = randomString(); inputFilePath = getTestPath(`${fileName}.csv`); successOutputFilePath = getTestPath(`${fileName}-success.csv`); errorOutputFilePath = getTestPath(`${fileName}-error.csv`); }); afterEach(async () => { await rm(inputFilePath); await rm(successOutputFilePath); await rm(errorOutputFilePath); }); it('找出正确的数据', async () => { await createFile(inputFilePath, [ 'a,b,c', 'a,b",c', 'a,b,c,', 'a,b,c', 'a,b,c', 'e,f,g', ]); await handleCsv({ inputFilePath, successOutputFilePath, errorOutputFilePath, columnCount: 3, }); const data = await readFile(successOutputFilePath); expect(data).to.include.members([ 'a,b,c', 'a,b,c', 'a,b,c', 'e,f,g', ]); }); it('找出错误的数据', async () => { await createFile(inputFilePath, [ 'a,b,c', 'a,b",c', 'a,b,c,', ]); await handleCsv({ inputFilePath, successOutputFilePath, errorOutputFilePath, columnCount: 3, }); const data = await readFile(errorOutputFilePath); expect(data).to.include.members([ 'a,b",c', 'a,b,c,', ]); }); it('日期标准化 (正确并转换)', async () => { await createFile(inputFilePath, [ 'a,b,2020-11-30T16:00:55.405Z,c', 'e,f,2020-12-31,c', 'e,f,,c', ]); await handleCsv({ inputFilePath, successOutputFilePath, errorOutputFilePath, columnCount: 4, dateIndexes: [2], }); expect(await readFile(successOutputFilePath)).to.include.members([ 'a,b,2020-12-01 00:00:55,c', 'e,f,2020-12-31 00:00:00,c', ]); }); it('日期标准化 (找出错误日期格式)', async () => { await createFile(inputFilePath, [ 'a,b,2020-11-30T16:00:55.405Z,c', 'e,f,2020-12-31,c', 'e,f,,c', // 'e,f,1596336484,c', ]); await handleCsv({ inputFilePath, successOutputFilePath, errorOutputFilePath, columnCount: 4, dateIndexes: [2], }); expect(await readFile(errorOutputFilePath)).to.include.members([ 'e,f,,c', ]); }); });