2020-06-19 09:24:51 +03:00
|
|
|
const should = require('should');
|
|
|
|
const path = require('path');
|
2022-10-13 07:10:44 +03:00
|
|
|
const assert = require('assert');
|
2022-10-13 06:37:54 +03:00
|
|
|
const {parse} = require('../index');
|
2020-06-19 10:23:52 +03:00
|
|
|
const csvPath = path.join(__dirname, '/fixtures/');
|
2020-06-19 09:24:51 +03:00
|
|
|
|
2022-10-12 08:51:53 +03:00
|
|
|
const readCSV = ({filePath, mapping, defaultLabels}) => parse(filePath, mapping, defaultLabels);
|
|
|
|
|
2020-07-08 10:39:37 +03:00
|
|
|
describe('parse', function () {
|
|
|
|
it('read csv: empty file', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'empty.csv'
|
2020-07-08 10:39:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(0);
|
|
|
|
});
|
|
|
|
|
2020-06-30 16:27:11 +03:00
|
|
|
it('read csv: one column', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'single-column-with-header.csv'
|
2020-06-30 16:27:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[1].email.should.eql('test@example.com');
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|
|
|
|
|
2020-06-30 16:27:11 +03:00
|
|
|
it('read csv: two columns, 1 filter', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'two-columns-with-header.csv'
|
2020-06-30 16:27:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[1].email.should.eql('test@example.com');
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|
|
|
|
|
2020-06-30 16:27:11 +03:00
|
|
|
it('read csv: two columns, 2 filters', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'two-columns-obscure-header.csv',
|
2020-10-27 13:44:06 +03:00
|
|
|
mapping: {
|
|
|
|
'Email Address': 'email'
|
|
|
|
}
|
2020-06-30 16:27:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('read csv: two columns with mapping', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'two-columns-mapping-header.csv',
|
2020-06-30 16:27:11 +03:00
|
|
|
mapping: {
|
2020-10-27 13:44:06 +03:00
|
|
|
correo_electronico: 'email',
|
|
|
|
nombre: 'name'
|
2020-06-30 16:27:11 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[0].name.should.eql('joe');
|
|
|
|
result[0].id.should.eql('1');
|
|
|
|
|
|
|
|
result[1].email.should.eql('test@example.com');
|
|
|
|
result[1].name.should.eql('test');
|
|
|
|
result[1].id.should.eql('2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('read csv: two columns with partial mapping', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'two-columns-mapping-header.csv',
|
2020-06-30 16:27:11 +03:00
|
|
|
mapping: {
|
2020-10-27 13:44:06 +03:00
|
|
|
correo_electronico: 'email'
|
2020-06-30 16:27:11 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[0].nombre.should.eql('joe');
|
|
|
|
result[0].id.should.eql('1');
|
|
|
|
|
|
|
|
result[1].email.should.eql('test@example.com');
|
|
|
|
result[1].nombre.should.eql('test');
|
|
|
|
result[1].id.should.eql('2');
|
|
|
|
});
|
2020-07-08 10:39:37 +03:00
|
|
|
|
2020-06-30 16:27:11 +03:00
|
|
|
it('read csv: two columns with empty mapping', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'two-columns-mapping-header.csv',
|
2020-06-30 16:27:11 +03:00
|
|
|
mapping: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].correo_electronico.should.eql('jbloggs@example.com');
|
|
|
|
result[0].nombre.should.eql('joe');
|
|
|
|
result[0].id.should.eql('1');
|
|
|
|
|
|
|
|
result[1].correo_electronico.should.eql('test@example.com');
|
|
|
|
result[1].nombre.should.eql('test');
|
|
|
|
result[1].id.should.eql('2');
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|
2020-08-17 08:57:49 +03:00
|
|
|
|
|
|
|
it('read csv: transforms empty values to nulls', async function () {
|
|
|
|
const result = await readCSV({
|
2022-10-12 08:51:53 +03:00
|
|
|
filePath: csvPath + 'multiple-records-with-empty-values.csv'
|
2020-08-17 08:57:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(result);
|
|
|
|
result.length.should.eql(2);
|
|
|
|
result[0].email.should.eql('jbloggs@example.com');
|
|
|
|
result[0].name.should.eql('Bob');
|
|
|
|
|
|
|
|
result[1].email.should.eql('test@example.com');
|
|
|
|
should.equal(result[1].name, null);
|
|
|
|
});
|
2022-10-13 07:10:44 +03:00
|
|
|
|
2022-10-13 07:39:57 +03:00
|
|
|
it('read csv: transforms "subscribed_to_emails" column to "subscribed" property when the mapping is passed in', async function () {
|
|
|
|
const mapping = {
|
|
|
|
subscribed_to_emails: 'subscribed'
|
|
|
|
};
|
2022-10-13 07:10:44 +03:00
|
|
|
const result = await readCSV({
|
2022-10-13 07:39:57 +03:00
|
|
|
filePath: csvPath + 'subscribed-to-emails-header.csv',
|
|
|
|
mapping
|
2022-10-13 07:10:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.ok(result[0].subscribed);
|
|
|
|
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
assert.equal(result[1].subscribed, false);
|
|
|
|
});
|
2022-10-13 07:39:57 +03:00
|
|
|
|
|
|
|
it('read csv: DOES NOT transforms "subscribed_to_emails" column to "subscribed" property when the WITHOUT mapping', async function () {
|
|
|
|
const result = await readCSV({
|
|
|
|
filePath: csvPath + 'subscribed-to-emails-header.csv'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.ok(result[0].subscribed_to_emails);
|
|
|
|
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
assert.equal(result[1].subscribed_to_emails, false);
|
|
|
|
});
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|