omnivore/packages/import-handler/test/csv/csv.test.ts

155 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-02-11 17:24:33 +00:00
import * as chai from 'chai'
import { expect } from 'chai'
import chaiString from 'chai-string'
import * as fs from 'fs'
2023-04-17 09:20:14 +00:00
import 'mocha'
2023-03-03 01:27:38 +00:00
import { ArticleSavingRequestStatus, ImportContext } from '../../src'
2023-04-17 09:20:14 +00:00
import { importCsv } from '../../src/csv'
import { stubImportCtx } from '../util'
2022-02-11 17:24:33 +00:00
chai.use(chaiString)
2023-05-25 08:14:40 +00:00
describe('Test csv importer', () => {
let stub: ImportContext
2024-03-23 08:40:01 +00:00
beforeEach(() => {
stub = stubImportCtx()
2023-05-25 08:14:40 +00:00
})
2023-05-25 08:08:43 +00:00
2023-05-25 08:14:40 +00:00
afterEach(async () => {
2023-05-25 08:08:43 +00:00
await stub.redisClient.quit()
2022-02-11 17:24:33 +00:00
})
2023-04-17 09:20:14 +00:00
2023-05-25 08:14:40 +00:00
describe('Load a simple CSV file', () => {
it('should call the handler for each URL', async () => {
const urls: URL[] = []
const stream = fs.createReadStream('./test/csv/data/simple.csv')
stub.urlHandler = (ctx: ImportContext, url): Promise<void> => {
urls.push(url)
return Promise.resolve()
}
2023-04-17 09:20:14 +00:00
2023-05-25 08:14:40 +00:00
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(0)
expect(stub.countImported).to.equal(2)
expect(urls).to.eql([
new URL('https://omnivore.app'),
new URL('https://google.com'),
])
})
2023-05-25 08:08:43 +00:00
2023-05-25 08:14:40 +00:00
it('increments the failed count when the URL is invalid', async () => {
const stream = fs.createReadStream('./test/csv/data/simple.csv')
stub.urlHandler = (ctx: ImportContext, url): Promise<void> => {
return Promise.reject('Failed to import url')
}
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(2)
expect(stub.countImported).to.equal(0)
})
2023-04-17 09:20:14 +00:00
})
2023-05-25 08:14:40 +00:00
describe('Load a complex CSV file', () => {
it('should call the handler for each URL, state and labels', async () => {
const results: {
url: URL
state?: ArticleSavingRequestStatus
labels?: string[]
savedAt?: Date
publishedAt?: Date
2023-05-25 08:14:40 +00:00
}[] = []
const stream = fs.createReadStream('./test/csv/data/complex.csv')
stub.urlHandler = (
ctx: ImportContext,
url,
state,
labels,
savedAt,
publishedAt
2023-05-25 08:14:40 +00:00
): Promise<void> => {
results.push({
url,
state,
labels,
savedAt,
publishedAt,
2023-05-25 08:14:40 +00:00
})
return Promise.resolve()
}
2023-05-25 08:08:43 +00:00
2023-05-25 08:14:40 +00:00
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(0)
expect(stub.countImported).to.equal(3)
expect(results).to.eql([
{
url: new URL('https://omnivore.app'),
state: 'ARCHIVED',
labels: ['test'],
savedAt: undefined,
publishedAt: undefined,
2023-05-25 08:14:40 +00:00
},
{
url: new URL('https://google.com'),
labels: ['test', 'development'],
state: undefined,
savedAt: undefined,
publishedAt: undefined,
2023-05-25 08:14:40 +00:00
},
{
url: new URL('https://test.com'),
state: 'SUCCEEDED',
labels: ['test', 'development'],
savedAt: new Date(1692093633000),
publishedAt: new Date(1692093633000),
2023-05-25 08:14:40 +00:00
},
])
})
})
2023-05-25 08:14:40 +00:00
describe('A file with no status set', () => {
it('should not try to set status', async () => {
const states: (ArticleSavingRequestStatus | undefined)[] = []
const stream = fs.createReadStream('./test/csv/data/unset-status.csv')
stub.urlHandler = (
ctx: ImportContext,
url,
state?: ArticleSavingRequestStatus
): Promise<void> => {
states.push(state)
return Promise.resolve()
}
2023-05-25 08:14:40 +00:00
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(0)
expect(stub.countImported).to.equal(2)
expect(states).to.eql([undefined, ArticleSavingRequestStatus.Archived])
})
})
2023-05-24 10:17:29 +00:00
2023-05-25 08:14:40 +00:00
describe('A file with some labels', () => {
it('gets the labels, handles empty, and trims extra whitespace', async () => {
const importedLabels: (string[] | undefined)[] = []
const stream = fs.createReadStream('./test/csv/data/labels.csv')
stub.urlHandler = (
ctx: ImportContext,
url,
state?: ArticleSavingRequestStatus,
labels?: string[]
): Promise<void> => {
importedLabels.push(labels)
return Promise.resolve()
}
2023-05-24 10:17:29 +00:00
2023-05-25 08:14:40 +00:00
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(0)
expect(stub.countImported).to.equal(3)
expect(importedLabels).to.eql([
['Label1', 'Label2', 'Label 3', 'Label 4'],
[],
2023-08-14 08:38:24 +00:00
undefined,
2023-05-25 08:14:40 +00:00
])
})
2023-05-24 10:17:29 +00:00
})
})