2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { renameColumns } from './rename_columns';
|
2020-10-16 18:46:51 +02:00
|
|
|
import { Datatable } from '../../../../../src/plugins/expressions/public';
|
2020-04-15 12:22:37 +02:00
|
|
|
import { createMockExecutionContext } from '../../../../../src/plugins/expressions/common/mocks';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
describe('rename_columns', () => {
|
|
|
|
it('should rename columns of a given datatable', () => {
|
2020-10-16 18:46:51 +02:00
|
|
|
const input: Datatable = {
|
|
|
|
type: 'datatable',
|
2019-11-13 17:00:02 +01:00
|
|
|
columns: [
|
2020-10-16 18:46:51 +02:00
|
|
|
{ id: 'a', name: 'A', meta: { type: 'number' } },
|
|
|
|
{ id: 'b', name: 'B', meta: { type: 'number' } },
|
2019-11-13 17:00:02 +01:00
|
|
|
],
|
|
|
|
rows: [
|
|
|
|
{ a: 1, b: 2 },
|
|
|
|
{ a: 3, b: 4 },
|
|
|
|
{ a: 5, b: 6 },
|
|
|
|
{ a: 7, b: 8 },
|
|
|
|
],
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const idMap = {
|
2019-10-16 05:02:18 +02:00
|
|
|
a: {
|
|
|
|
id: 'b',
|
|
|
|
label: 'Austrailia',
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
id: 'c',
|
|
|
|
label: 'Boomerang',
|
|
|
|
},
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
|
2020-02-11 19:47:36 +01:00
|
|
|
const result = renameColumns.fn(
|
|
|
|
input,
|
|
|
|
{ idMap: JSON.stringify(idMap) },
|
|
|
|
createMockExecutionContext()
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result).toMatchInlineSnapshot(`
|
2019-10-16 05:02:18 +02:00
|
|
|
Object {
|
|
|
|
"columns": Array [
|
|
|
|
Object {
|
|
|
|
"id": "b",
|
2020-10-16 18:46:51 +02:00
|
|
|
"meta": Object {
|
|
|
|
"type": "number",
|
|
|
|
},
|
2019-10-16 05:02:18 +02:00
|
|
|
"name": "Austrailia",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": "c",
|
2020-10-16 18:46:51 +02:00
|
|
|
"meta": Object {
|
|
|
|
"type": "number",
|
|
|
|
},
|
2019-10-16 05:02:18 +02:00
|
|
|
"name": "Boomerang",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"rows": Array [
|
|
|
|
Object {
|
|
|
|
"b": 1,
|
|
|
|
"c": 2,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"b": 3,
|
|
|
|
"c": 4,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"b": 5,
|
|
|
|
"c": 6,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"b": 7,
|
|
|
|
"c": 8,
|
|
|
|
},
|
|
|
|
],
|
2020-10-16 18:46:51 +02:00
|
|
|
"type": "datatable",
|
2019-10-16 05:02:18 +02:00
|
|
|
}
|
|
|
|
`);
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
|
2019-10-23 21:17:33 +02:00
|
|
|
it('should replace "" with a visible value', () => {
|
2020-10-16 18:46:51 +02:00
|
|
|
const input: Datatable = {
|
|
|
|
type: 'datatable',
|
|
|
|
columns: [{ id: 'a', name: 'A', meta: { type: 'string' } }],
|
2019-10-23 21:17:33 +02:00
|
|
|
rows: [{ a: '' }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const idMap = {
|
|
|
|
a: {
|
|
|
|
id: 'a',
|
|
|
|
label: 'Austrailia',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-02-11 19:47:36 +01:00
|
|
|
const result = renameColumns.fn(
|
|
|
|
input,
|
|
|
|
{ idMap: JSON.stringify(idMap) },
|
|
|
|
createMockExecutionContext()
|
2019-10-23 21:17:33 +02:00
|
|
|
);
|
2020-02-11 19:47:36 +01:00
|
|
|
|
|
|
|
expect(result.rows[0].a).toEqual('(empty)');
|
2019-10-23 21:17:33 +02:00
|
|
|
});
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
it('should keep columns which are not mapped', () => {
|
2020-10-16 18:46:51 +02:00
|
|
|
const input: Datatable = {
|
|
|
|
type: 'datatable',
|
2019-11-13 17:00:02 +01:00
|
|
|
columns: [
|
2020-10-16 18:46:51 +02:00
|
|
|
{ id: 'a', name: 'A', meta: { type: 'number' } },
|
|
|
|
{ id: 'b', name: 'B', meta: { type: 'number' } },
|
2019-11-13 17:00:02 +01:00
|
|
|
],
|
|
|
|
rows: [
|
|
|
|
{ a: 1, b: 2 },
|
|
|
|
{ a: 3, b: 4 },
|
|
|
|
{ a: 5, b: 6 },
|
|
|
|
{ a: 7, b: 8 },
|
|
|
|
],
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const idMap = {
|
2019-10-16 05:02:18 +02:00
|
|
|
b: { id: 'c', label: 'Catamaran' },
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
|
2020-02-11 19:47:36 +01:00
|
|
|
const result = renameColumns.fn(
|
|
|
|
input,
|
|
|
|
{ idMap: JSON.stringify(idMap) },
|
|
|
|
createMockExecutionContext()
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result).toMatchInlineSnapshot(`
|
2019-10-16 05:02:18 +02:00
|
|
|
Object {
|
|
|
|
"columns": Array [
|
|
|
|
Object {
|
|
|
|
"id": "a",
|
2020-10-16 18:46:51 +02:00
|
|
|
"meta": Object {
|
|
|
|
"type": "number",
|
|
|
|
},
|
2019-10-16 05:02:18 +02:00
|
|
|
"name": "A",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": "c",
|
2020-10-16 18:46:51 +02:00
|
|
|
"meta": Object {
|
|
|
|
"type": "number",
|
|
|
|
},
|
2019-10-16 05:02:18 +02:00
|
|
|
"name": "Catamaran",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"rows": Array [
|
|
|
|
Object {
|
|
|
|
"a": 1,
|
|
|
|
"c": 2,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"a": 3,
|
|
|
|
"c": 4,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"a": 5,
|
|
|
|
"c": 6,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"a": 7,
|
|
|
|
"c": 8,
|
|
|
|
},
|
|
|
|
],
|
2020-10-16 18:46:51 +02:00
|
|
|
"type": "datatable",
|
2019-10-16 05:02:18 +02:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should rename date histograms', () => {
|
2020-10-16 18:46:51 +02:00
|
|
|
const input: Datatable = {
|
|
|
|
type: 'datatable',
|
2019-11-13 17:00:02 +01:00
|
|
|
columns: [
|
2020-10-16 18:46:51 +02:00
|
|
|
{ id: 'a', name: 'A', meta: { type: 'number' } },
|
|
|
|
{ id: 'b', name: 'banana per 30 seconds', meta: { type: 'number' } },
|
2019-11-13 17:00:02 +01:00
|
|
|
],
|
|
|
|
rows: [
|
|
|
|
{ a: 1, b: 2 },
|
|
|
|
{ a: 3, b: 4 },
|
|
|
|
{ a: 5, b: 6 },
|
|
|
|
{ a: 7, b: 8 },
|
|
|
|
],
|
2019-10-16 05:02:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const idMap = {
|
|
|
|
b: { id: 'c', label: 'Apple', operationType: 'date_histogram', sourceField: 'banana' },
|
|
|
|
};
|
|
|
|
|
2020-02-11 19:47:36 +01:00
|
|
|
const result = renameColumns.fn(
|
|
|
|
input,
|
|
|
|
{ idMap: JSON.stringify(idMap) },
|
|
|
|
createMockExecutionContext()
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result).toMatchInlineSnapshot(`
|
2019-10-16 05:02:18 +02:00
|
|
|
Object {
|
|
|
|
"columns": Array [
|
|
|
|
Object {
|
|
|
|
"id": "a",
|
2020-10-16 18:46:51 +02:00
|
|
|
"meta": Object {
|
|
|
|
"type": "number",
|
|
|
|
},
|
2019-10-16 05:02:18 +02:00
|
|
|
"name": "A",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": "c",
|
2020-10-16 18:46:51 +02:00
|
|
|
"meta": Object {
|
|
|
|
"type": "number",
|
|
|
|
},
|
2019-10-16 05:02:18 +02:00
|
|
|
"name": "Apple per 30 seconds",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"rows": Array [
|
|
|
|
Object {
|
|
|
|
"a": 1,
|
|
|
|
"c": 2,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"a": 3,
|
|
|
|
"c": 4,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"a": 5,
|
|
|
|
"c": 6,
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"a": 7,
|
|
|
|
"c": 8,
|
|
|
|
},
|
|
|
|
],
|
2020-10-16 18:46:51 +02:00
|
|
|
"type": "datatable",
|
2019-10-16 05:02:18 +02:00
|
|
|
}
|
|
|
|
`);
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
});
|