2020-06-19 09:24:51 +03:00
|
|
|
const path = require('path');
|
2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
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
|
|
|
|
2020-07-08 10:39:37 +03:00
|
|
|
describe('parse', function () {
|
2022-10-18 08:05:31 +03:00
|
|
|
const DEFAULT_HEADER_MAPPING = {
|
|
|
|
email: 'email',
|
|
|
|
name: 'name'
|
|
|
|
};
|
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('empty file', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'empty.csv';
|
2022-10-18 08:05:31 +03:00
|
|
|
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
|
2020-07-08 10:39:37 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 0);
|
2020-07-08 10:39:37 +03:00
|
|
|
});
|
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('one column', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'single-column-with-header.csv';
|
2022-10-18 08:05:31 +03:00
|
|
|
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|
|
|
|
|
2022-10-13 11:34:59 +03:00
|
|
|
it('one column without header mapping returns empty result', async function () {
|
|
|
|
const filePath = csvPath + 'single-column-with-header.csv';
|
|
|
|
const result = await parse(filePath);
|
|
|
|
|
2023-06-23 09:31:16 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('one column with bom', async function () {
|
|
|
|
const filePath = csvPath + 'single-column-with-header-bom.csv';
|
|
|
|
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
|
|
|
|
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('one column with bom and without header mapping returns empty result', async function () {
|
|
|
|
const filePath = csvPath + 'single-column-with-header-bom.csv';
|
|
|
|
const result = await parse(filePath);
|
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 0);
|
2022-10-13 11:34:59 +03:00
|
|
|
});
|
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('two columns, 1 filter', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'two-columns-with-header.csv';
|
2022-10-18 08:05:31 +03:00
|
|
|
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('two columns, 2 filters', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'two-columns-obscure-header.csv';
|
|
|
|
const mapping = {
|
2022-10-18 08:05:31 +03:00
|
|
|
id: 'id',
|
2022-10-13 11:34:59 +03:00
|
|
|
'Email Address': 'email'
|
|
|
|
};
|
|
|
|
const result = await parse(filePath, mapping);
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[0].id, '1');
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
assert.equal(result[1].id, '2');
|
2020-06-30 16:27:11 +03:00
|
|
|
});
|
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('two columns with mapping', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'two-columns-mapping-header.csv';
|
|
|
|
const mapping = {
|
2022-10-18 08:05:31 +03:00
|
|
|
id: 'id',
|
2022-10-13 11:34:59 +03:00
|
|
|
correo_electronico: 'email',
|
|
|
|
nombre: 'name'
|
|
|
|
};
|
|
|
|
const result = await parse(filePath, mapping);
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[0].name, 'joe');
|
|
|
|
assert.equal(result[0].id, '1');
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
assert.equal(result[1].name, 'test');
|
|
|
|
assert.equal(result[1].id, '2');
|
2020-06-30 16:27:11 +03:00
|
|
|
});
|
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('two columns with partial mapping', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'two-columns-mapping-header.csv';
|
|
|
|
const mapping = {
|
2022-10-18 08:05:31 +03:00
|
|
|
id: 'id',
|
2022-10-13 11:34:59 +03:00
|
|
|
correo_electronico: 'email'
|
|
|
|
};
|
|
|
|
const result = await parse(filePath, mapping);
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[0].id, '1');
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
assert.equal(result[1].id, '2');
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|
2020-08-17 08:57:49 +03:00
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('transforms empty values to nulls', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'multiple-records-with-empty-values.csv';
|
2022-10-18 08:05:31 +03:00
|
|
|
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
|
2020-08-17 08:57:49 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
|
|
|
assert.equal(result[0].name, 'Bob');
|
2020-08-17 08:57:49 +03:00
|
|
|
|
2022-10-18 08:13:31 +03:00
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
|
|
|
assert.equal(result[1].name, null);
|
2020-08-17 08:57:49 +03:00
|
|
|
});
|
2022-10-13 07:10:44 +03:00
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it(' transforms "subscribed_to_emails" column to "subscribed" property when the mapping is passed in', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'subscribed-to-emails-header.csv';
|
2022-10-13 07:39:57 +03:00
|
|
|
const mapping = {
|
2022-10-18 08:05:31 +03:00
|
|
|
email: 'email',
|
2022-10-13 07:39:57 +03:00
|
|
|
subscribed_to_emails: 'subscribed'
|
|
|
|
};
|
2022-10-13 11:34:59 +03:00
|
|
|
const result = await parse(filePath, 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
|
|
|
|
2022-10-13 11:17:13 +03:00
|
|
|
it('DOES NOT transforms "subscribed_to_emails" column to "subscribed" property when the WITHOUT mapping', async function () {
|
2022-10-13 11:34:59 +03:00
|
|
|
const filePath = csvPath + 'subscribed-to-emails-header.csv';
|
2022-10-18 08:05:31 +03:00
|
|
|
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
|
2022-10-13 07:39:57 +03:00
|
|
|
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.length, 2);
|
|
|
|
assert.equal(result[0].email, 'jbloggs@example.com');
|
2022-10-18 08:05:31 +03:00
|
|
|
assert.equal(result[0].subscribed_to_emails, undefined, 'property not present in the mapping should not be defined');
|
2022-10-13 07:39:57 +03:00
|
|
|
|
|
|
|
assert.equal(result[1].email, 'test@example.com');
|
2022-10-18 08:05:31 +03:00
|
|
|
assert.equal(result[1].subscribed_to_emails, undefined, 'property not present in the mapping should not be defined');
|
2022-10-13 07:39:57 +03:00
|
|
|
});
|
2020-06-19 09:24:51 +03:00
|
|
|
});
|