Add test case for failures

This commit is contained in:
Hongbo Wu
2023-04-17 17:20:14 +08:00
parent a1e782e39d
commit c03d59c3c9
2 changed files with 17 additions and 3 deletions

View File

@ -17,7 +17,7 @@ export const importCsv = async (ctx: ImportContext, stream: Stream) => {
// labels follows format: "[label1, label2]"
const labels = row.length > 2 ? row[2].slice(1, -1).split(',') : undefined
await ctx.urlHandler(ctx, url, state, labels)
ctx.countImported++
ctx.countImported += 1
} catch (error) {
console.log('invalid url', row, error)
ctx.countFailed += 1

View File

@ -1,10 +1,10 @@
import 'mocha'
import * as chai from 'chai'
import { expect } from 'chai'
import chaiString from 'chai-string'
import * as fs from 'fs'
import { importCsv } from '../../src/csv'
import 'mocha'
import { ArticleSavingRequestStatus, ImportContext } from '../../src'
import { importCsv } from '../../src/csv'
import { stubImportCtx } from '../util'
chai.use(chaiString)
@ -27,6 +27,20 @@ describe('Load a simple CSV file', () => {
new URL('https://google.com'),
])
})
it('increments the failed count when the URL is invalid', async () => {
const urls: URL[] = []
const stream = fs.createReadStream('./test/csv/data/simple.csv')
const stub = stubImportCtx()
stub.urlHandler = (ctx: ImportContext, url): Promise<void> => {
urls.push(url)
return Promise.reject('Failed to import url')
}
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(2)
expect(stub.countImported).to.equal(0)
})
})
describe('Load a complex CSV file', () => {