Ghost/ghost/members-csv/test/parse.test.js
Nazar Gargol b8c1aeee35 Added empty string '' to null transform when parsing CSVs
no issue

- When items are parsed from CSV empty values were interpreted as empty strings - ''. Empty strings are always transformed into 'null' values in Ghost's model layer and are much more problematic to validate comparing to plain `null`. Specifically validation was imossible for 'format: date-time' with JSON schema validation through ajv when the value of date property was an empty string
- This behavior resemples one present in Ghost's model layer  - 95880dddeb
- When testing performance overhead for this change did not spot any statistically significant change in performance (tested set was 50K rows)
2020-08-17 17:57:49 +12:00

130 lines
4.2 KiB
JavaScript

const should = require('should');
const path = require('path');
const {readCSV} = require('../lib/parse');
const csvPath = path.join(__dirname, '/fixtures/');
describe('parse', function () {
it('read csv: empty file', async function () {
const result = await readCSV({
path: csvPath + 'empty.csv',
columnsToExtract: [{name: 'email', lookup: /email/i}]
});
should.exist(result);
result.length.should.eql(0);
});
it('read csv: one column', async function () {
const result = await readCSV({
path: csvPath + 'single-column-with-header.csv',
columnsToExtract: [{name: 'email', lookup: /email/i}]
});
should.exist(result);
result.length.should.eql(2);
result[0].email.should.eql('jbloggs@example.com');
result[1].email.should.eql('test@example.com');
});
it('read csv: two columns, 1 filter', async function () {
const result = await readCSV({
path: csvPath + 'two-columns-with-header.csv',
columnsToExtract: [{name: 'email', lookup: /email/i}]
});
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);
});
it('read csv: two columns, 2 filters', async function () {
const result = await readCSV({
path: csvPath + 'two-columns-obscure-header.csv',
columnsToExtract: [
{name: 'email', lookup: /email/i},
{name: 'id', lookup: /id/i}
]
});
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({
path: csvPath + 'two-columns-mapping-header.csv',
mapping: {
email: 'correo_electronico',
name: 'nombre',
id: 'id'
}
});
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({
path: csvPath + 'two-columns-mapping-header.csv',
mapping: {
email: 'correo_electronico'
}
});
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');
});
it('read csv: two columns with empty mapping', async function () {
const result = await readCSV({
path: csvPath + 'two-columns-mapping-header.csv',
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');
});
it('read csv: transforms empty values to nulls', async function () {
const result = await readCSV({
path: csvPath + 'multiple-records-with-empty-values.csv'
});
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);
});
});