|
|
@@ -1,4 +1,6 @@
|
|
|
-import { parseCSV, toTableData } from './processTableData';
|
|
|
+import { parseCSV, toTableData, guessColumnTypes, guessColumnTypeFromValue } from './processTableData';
|
|
|
+import { ColumnType } from '../types/data';
|
|
|
+import moment from 'moment';
|
|
|
|
|
|
describe('processTableData', () => {
|
|
|
describe('basic processing', () => {
|
|
|
@@ -20,23 +22,23 @@ describe('processTableData', () => {
|
|
|
});
|
|
|
|
|
|
describe('toTableData', () => {
|
|
|
- it('converts timeseries to table skipping nulls', () => {
|
|
|
+ it('converts timeseries to table ', () => {
|
|
|
const input1 = {
|
|
|
target: 'Field Name',
|
|
|
datapoints: [[100, 1], [200, 2]],
|
|
|
};
|
|
|
+ let table = toTableData(input1);
|
|
|
+ expect(table.columns[0].text).toBe(input1.target);
|
|
|
+ expect(table.rows).toBe(input1.datapoints);
|
|
|
+
|
|
|
+ // Should fill a default name if target is empty
|
|
|
const input2 = {
|
|
|
// without target
|
|
|
target: '',
|
|
|
datapoints: [[100, 1], [200, 2]],
|
|
|
};
|
|
|
- const data = toTableData([null, input1, input2, null, null]);
|
|
|
- expect(data.length).toBe(2);
|
|
|
- expect(data[0].columns[0].text).toBe(input1.target);
|
|
|
- expect(data[0].rows).toBe(input1.datapoints);
|
|
|
-
|
|
|
- // Default name
|
|
|
- expect(data[1].columns[0].text).toEqual('Value');
|
|
|
+ table = toTableData(input2);
|
|
|
+ expect(table.columns[0].text).toEqual('Value');
|
|
|
});
|
|
|
|
|
|
it('keeps tableData unchanged', () => {
|
|
|
@@ -44,15 +46,42 @@ describe('toTableData', () => {
|
|
|
columns: [{ text: 'A' }, { text: 'B' }, { text: 'C' }],
|
|
|
rows: [[100, 'A', 1], [200, 'B', 2], [300, 'C', 3]],
|
|
|
};
|
|
|
- const data = toTableData([null, input, null, null]);
|
|
|
- expect(data.length).toBe(1);
|
|
|
- expect(data[0]).toBe(input);
|
|
|
+ const table = toTableData(input);
|
|
|
+ expect(table).toBe(input);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Guess Colum Types from value', () => {
|
|
|
+ expect(guessColumnTypeFromValue(1)).toBe(ColumnType.number);
|
|
|
+ expect(guessColumnTypeFromValue(1.234)).toBe(ColumnType.number);
|
|
|
+ expect(guessColumnTypeFromValue(3.125e7)).toBe(ColumnType.number);
|
|
|
+ expect(guessColumnTypeFromValue('1')).toBe(ColumnType.string);
|
|
|
+ expect(guessColumnTypeFromValue('1.234')).toBe(ColumnType.string);
|
|
|
+ expect(guessColumnTypeFromValue('3.125e7')).toBe(ColumnType.string);
|
|
|
+ expect(guessColumnTypeFromValue(true)).toBe(ColumnType.boolean);
|
|
|
+ expect(guessColumnTypeFromValue(false)).toBe(ColumnType.boolean);
|
|
|
+ expect(guessColumnTypeFromValue(new Date())).toBe(ColumnType.time);
|
|
|
+ expect(guessColumnTypeFromValue(moment())).toBe(ColumnType.time);
|
|
|
});
|
|
|
|
|
|
- it('supports null values OK', () => {
|
|
|
- expect(toTableData([null, null, null, null])).toEqual([]);
|
|
|
- expect(toTableData(undefined)).toEqual([]);
|
|
|
- expect(toTableData((null as unknown) as any[])).toEqual([]);
|
|
|
- expect(toTableData([])).toEqual([]);
|
|
|
+ it('Guess Colum Types from strings', () => {
|
|
|
+ expect(guessColumnTypeFromValue('1', true)).toBe(ColumnType.number);
|
|
|
+ expect(guessColumnTypeFromValue('1.234', true)).toBe(ColumnType.number);
|
|
|
+ expect(guessColumnTypeFromValue('3.125e7', true)).toBe(ColumnType.number);
|
|
|
+ expect(guessColumnTypeFromValue('True', true)).toBe(ColumnType.boolean);
|
|
|
+ expect(guessColumnTypeFromValue('FALSE', true)).toBe(ColumnType.boolean);
|
|
|
+ expect(guessColumnTypeFromValue('true', true)).toBe(ColumnType.boolean);
|
|
|
+ expect(guessColumnTypeFromValue('xxxx', true)).toBe(ColumnType.string);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Guess Colum Types from table', () => {
|
|
|
+ const table = {
|
|
|
+ columns: [{ text: 'A (number)' }, { text: 'B (strings)' }, { text: 'C (nulls)' }, { text: 'Time' }],
|
|
|
+ rows: [[123, null, null, '2000'], [null, '123', null, 'XXX']],
|
|
|
+ };
|
|
|
+ const norm = guessColumnTypes(table);
|
|
|
+ expect(norm.columns[0].type).toBe(ColumnType.number);
|
|
|
+ expect(norm.columns[1].type).toBe(ColumnType.string);
|
|
|
+ expect(norm.columns[2].type).toBeUndefined();
|
|
|
+ expect(norm.columns[3].type).toBe(ColumnType.time); // based on name
|
|
|
});
|
|
|
});
|