Fix mapColumn and staticColumn to work with empty tables (#33078) (#33096)

This commit is contained in:
Catherine Liu 2019-03-13 00:10:40 -07:00 committed by GitHub
parent a3574c281c
commit f600edaf9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 4 deletions

View file

@ -7,7 +7,7 @@
import expect from 'expect.js';
import { mapColumn } from '../mapColumn';
import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper';
import { testTable } from './fixtures/test_tables';
import { testTable, emptyTable } from './fixtures/test_tables';
const pricePlusTwo = datatable => Promise.resolve(datatable.rows[0].price + 2);
@ -42,6 +42,16 @@ describe('mapColumn', () => {
});
});
it('adds a column to empty tables', () => {
return fn(emptyTable, { name: 'name', expression: pricePlusTwo }).then(result => {
expect(result.type).to.be('datatable');
expect(result.columns).to.have.length(1);
expect(result.columns[0])
.to.have.property('name', 'name')
.and.to.have.property('type', 'null');
});
});
describe('expression', () => {
it('maps null values to the new column', () => {
return fn(testTable, { name: 'empty' }).then(result => {

View file

@ -7,7 +7,7 @@
import expect from 'expect.js';
import { staticColumn } from '../staticColumn';
import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper';
import { testTable } from './fixtures/test_tables';
import { testTable, emptyTable } from './fixtures/test_tables';
describe('staticColumn', () => {
const fn = functionWrapper(staticColumn);
@ -37,4 +37,12 @@ describe('staticColumn', () => {
expect(result.columns).to.eql([...testTable.columns, { name: 'empty', type: 'null' }]);
expect(result.rows.every(row => row.empty === null)).to.be(true);
});
it('adds a column to empty tables', () => {
const result = fn(emptyTable, { name: 'empty', value: 1 });
expect(result.type).to.be('datatable');
expect(result.columns).to.eql([{ name: 'empty', type: 'number' }]);
expect(result.rows.length).to.be(0);
});
});

View file

@ -47,7 +47,7 @@ export const mapColumn = () => ({
return Promise.all(rowPromises).then(rows => {
const existingColumnIndex = columns.findIndex(({ name }) => name === args.name);
const type = getType(rows[0][args.name]);
const type = rows.length ? getType(rows[0][args.name]) : 'null';
const newColumn = { name: args.name, type };
if (existingColumnIndex === -1) {
columns.push(newColumn);

View file

@ -29,7 +29,7 @@ export const staticColumn = () => ({
},
fn: (context, args) => {
const rows = context.rows.map(row => ({ ...row, [args.name]: args.value }));
const type = getType(rows[0][args.name]);
const type = getType(args.value);
const columns = [...context.columns];
const existingColumnIndex = columns.findIndex(({ name }) => name === args.name);
const newColumn = { name: args.name, type };