2020-06-19 09:24:51 +03:00
|
|
|
const should = require('should');
|
|
|
|
const path = require('path');
|
2020-06-19 10:49:39 +03:00
|
|
|
const {readCSV} = require('../lib/parse');
|
2020-06-19 10:23:52 +03:00
|
|
|
const csvPath = path.join(__dirname, '/fixtures/');
|
2020-06-19 09:24:51 +03:00
|
|
|
|
2020-06-19 10:23:52 +03:00
|
|
|
describe('read csv', function () {
|
2020-06-19 09:24:51 +03:00
|
|
|
it('read csv: one column', function (done) {
|
2020-06-19 10:23:52 +03:00
|
|
|
readCSV({
|
2020-06-19 09:24:51 +03:00
|
|
|
path: csvPath + 'single-column-with-header.csv',
|
|
|
|
columnsToExtract: [{name: 'email', lookup: /email/i}]
|
|
|
|
}).then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[1].email.should.eql('test@example.com');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('read csv: two columns, 1 filter', function (done) {
|
2020-06-19 10:23:52 +03:00
|
|
|
readCSV({
|
2020-06-19 09:24:51 +03:00
|
|
|
path: csvPath + 'two-columns-with-header.csv',
|
|
|
|
columnsToExtract: [{name: 'email', lookup: /email/i}]
|
|
|
|
}).then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[1].email.should.eql('test@example.com');
|
|
|
|
should.not.exist(result[0].id);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('read csv: two columns, 2 filters', function (done) {
|
2020-06-19 10:23:52 +03:00
|
|
|
readCSV({
|
2020-06-19 09:24:51 +03:00
|
|
|
path: csvPath + 'two-columns-obscure-header.csv',
|
|
|
|
columnsToExtract: [
|
|
|
|
{name: 'email', lookup: /email/i},
|
|
|
|
{name: 'id', lookup: /id/i}
|
|
|
|
]
|
|
|
|
}).then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[0].id.should.eql('1');
|
|
|
|
result[1].email.should.eql('test@example.com');
|
|
|
|
result[1].id.should.eql('2');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|