Merge pull request #2245 from omnivore-app/fix/handle-empty-state-in-csv

Handle empty state columns in the import CSV files
This commit is contained in:
Jackson Harper
2023-05-25 11:08:47 +08:00
committed by GitHub
4 changed files with 53 additions and 1 deletions

View File

@ -13,7 +13,7 @@ export const importCsv = async (ctx: ImportContext, stream: Stream) => {
for await (const row of parser) {
try {
const url = new URL(row[0])
const state = row.length > 1 ? row[1] : undefined
const state = row.length > 1 && row[1] ? row[1] : undefined
// labels follows format: "[label1,label2]"
const labels =
row.length > 2

View File

@ -88,3 +88,50 @@ describe('Load a complex CSV file', () => {
])
})
})
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')
const stub = stubImportCtx()
stub.urlHandler = (
ctx: ImportContext,
url,
state?: ArticleSavingRequestStatus
): Promise<void> => {
states.push(state)
return Promise.resolve()
}
await importCsv(stub, stream)
expect(stub.countFailed).to.equal(0)
expect(stub.countImported).to.equal(2)
expect(states).to.eql([undefined, ArticleSavingRequestStatus.Archived])
})
})
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')
const stub = stubImportCtx()
stub.urlHandler = (
ctx: ImportContext,
url,
state?: ArticleSavingRequestStatus,
labels?: string[]
): Promise<void> => {
importedLabels.push(labels)
return Promise.resolve()
}
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'],
[],
[],
])
})
})

View File

@ -0,0 +1,3 @@
"https://test.url/path01",,"[Label1,Label2, Label 3, Label 4 ]"
"https://test.url/path02",,"[]"
"https://test.url/path03",,
1 https://test.url/path01 [Label1,Label2, Label 3, Label 4 ]
2 https://test.url/path02 []
3 https://test.url/path03

View File

@ -0,0 +1,2 @@
"https://test.url/path01",,"[Random]"
"https://test.url/path02",ARCHIVED,"[Random]"
1 https://test.url/path01 [Random]
2 https://test.url/path02 ARCHIVED [Random]