Skip to content

Commit

Permalink
[Canvas] Fix column object shape in datatable created by CSV function (
Browse files Browse the repository at this point in the history
…#98561)

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
cqliu1 and kibanamachine authored May 17, 2021
1 parent e30c7a4 commit 49b1ccd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
* 2.0.
*/

// @ts-expect-error untyped lib
import { functionWrapper } from '../../../test_helpers/function_wrapper';
import { getFunctionErrors } from '../../../i18n';
import { csv } from './csv';
import { Datatable } from 'src/plugins/expressions';

const errors = getFunctionErrors().csv;

describe('csv', () => {
const fn = functionWrapper(csv);
const expected = {
const expected: Datatable = {
type: 'datatable',
columns: [
{ name: 'name', type: 'string' },
{ name: 'number', type: 'string' },
{ id: 'name', name: 'name', meta: { type: 'string' } },
{ id: 'number', name: 'number', meta: { type: 'string' } },
],
rows: [
{ name: 'one', number: '1' },
Expand Down Expand Up @@ -69,43 +71,47 @@ fourty two%SPLIT%42`,
});

it('should trim column names', () => {
expect(
fn(null, {
data: `foo," bar ", baz, " buz "
1,2,3,4`,
})
).toEqual({
const expectedResult: Datatable = {
type: 'datatable',
columns: [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'string' },
{ name: 'baz', type: 'string' },
{ name: 'buz', type: 'string' },
{ id: 'foo', name: 'foo', meta: { type: 'string' } },
{ id: 'bar', name: 'bar', meta: { type: 'string' } },
{ id: 'baz', name: 'baz', meta: { type: 'string' } },
{ id: 'buz', name: 'buz', meta: { type: 'string' } },
],
rows: [{ foo: '1', bar: '2', baz: '3', buz: '4' }],
});
});
};

it('should handle odd spaces correctly', () => {
expect(
fn(null, {
data: `foo," bar ", baz, " buz "
1," best ",3, " ok"
" good", bad, better , " worst " `,
1,2,3,4`,
})
).toEqual({
).toEqual(expectedResult);
});

it('should handle odd spaces correctly', () => {
const expectedResult: Datatable = {
type: 'datatable',
columns: [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'string' },
{ name: 'baz', type: 'string' },
{ name: 'buz', type: 'string' },
{ id: 'foo', name: 'foo', meta: { type: 'string' } },
{ id: 'bar', name: 'bar', meta: { type: 'string' } },
{ id: 'baz', name: 'baz', meta: { type: 'string' } },
{ id: 'buz', name: 'buz', meta: { type: 'string' } },
],
rows: [
{ foo: '1', bar: ' best ', baz: '3', buz: ' ok' },
{ foo: ' good', bar: ' bad', baz: ' better ', buz: ' worst ' },
],
});
};

expect(
fn(null, {
data: `foo," bar ", baz, " buz "
1," best ",3, " ok"
" good", bad, better , " worst " `,
})
).toEqual(expectedResult);
});

it('throws when given invalid csv', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export function csv(): ExpressionFunctionDefinition<'csv', null, Arguments, Data
if (i === 0) {
// first row, assume header values
row.forEach((colName: string) =>
acc.columns.push({ name: colName.trim(), type: 'string' })
acc.columns.push({
id: colName.trim(),
name: colName.trim(),
meta: { type: 'string' },
})
);
} else {
// any other row is a data row
Expand Down

0 comments on commit 49b1ccd

Please sign in to comment.