From 6ab3f082d0e0ba6a843a79cc9fc629cf968c8aa6 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Mon, 30 Nov 2020 08:14:09 +0100 Subject: [PATCH] move tests --- .../operations/definitions.test.ts | 215 ++++++++++++++++++ .../operations/definitions/index.ts | 6 + .../operations/layer_helpers.test.ts | 97 -------- 3 files changed, 221 insertions(+), 97 deletions(-) create mode 100644 x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions.test.ts diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions.test.ts b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions.test.ts new file mode 100644 index 00000000000000..18aa15badec4ff --- /dev/null +++ b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions.test.ts @@ -0,0 +1,215 @@ +/* + * 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 { + sumOperation, + averageOperation, + countOperation, + counterRateOperation, + movingAverageOperation, + derivativeOperation, +} from './definitions'; +import { getFieldByNameFactory } from '../pure_helpers'; +import { documentField } from '../document_field'; +import { IndexPattern, IndexPatternLayer, IndexPatternField } from '../types'; +import { IndexPatternColumn } from '.'; + +const indexPatternFields = [ + { + name: 'timestamp', + displayName: 'timestampLabel', + type: 'date', + aggregatable: true, + searchable: true, + }, + { + name: 'start_date', + displayName: 'start_date', + type: 'date', + aggregatable: true, + searchable: true, + }, + { + name: 'bytes', + displayName: 'bytes', + type: 'number', + aggregatable: true, + searchable: true, + }, + { + name: 'memory', + displayName: 'memory', + type: 'number', + aggregatable: true, + searchable: true, + }, + { + name: 'source', + displayName: 'source', + type: 'string', + aggregatable: true, + searchable: true, + }, + { + name: 'dest', + displayName: 'dest', + type: 'string', + aggregatable: true, + searchable: true, + }, + documentField, +]; + +const indexPattern = { + id: '1', + title: 'my-fake-index-pattern', + timeFieldName: 'timestamp', + hasRestrictions: false, + fields: indexPatternFields, + getFieldByName: getFieldByNameFactory([...indexPatternFields]), +}; + +const baseColumnArgs: { + previousColumn: IndexPatternColumn; + indexPattern: IndexPattern; + layer: IndexPatternLayer; + field: IndexPatternField; +} = { + previousColumn: { + label: 'Count of records per hour', + timeScale: 'h', + dataType: 'number', + isBucketed: false, + + // Private + operationType: 'count', + sourceField: 'Records', + }, + indexPattern, + layer: { + columns: {}, + columnOrder: [], + indexPatternId: '1', + }, + field: indexPattern.fields[2], +}; + +describe('time scale transition', () => { + it('should carry over time scale and adjust label on operation from count to sum', () => { + expect( + sumOperation.buildColumn({ + ...baseColumnArgs, + }) + ).toEqual( + expect.objectContaining({ + timeScale: 'h', + label: 'Sum of bytes per hour', + }) + ); + }); + + it('should carry over time scale and adjust label on operation from count to calculation', () => { + [counterRateOperation, movingAverageOperation, derivativeOperation].forEach( + (calculationOperation) => { + const result = calculationOperation.buildColumn({ + ...baseColumnArgs, + referenceIds: [], + }); + expect(result.timeScale).toEqual('h'); + expect(result.label).toContain('per hour'); + } + ); + }); + + it('should carry over time scale and adjust label on operation from sum to count', () => { + expect( + countOperation.buildColumn({ + ...baseColumnArgs, + previousColumn: { + label: 'Sum of bytes per hour', + timeScale: 'h', + dataType: 'number', + isBucketed: false, + operationType: 'sum', + sourceField: 'bytes', + }, + }) + ).toEqual( + expect.objectContaining({ + timeScale: 'h', + label: 'Count of records per hour', + }) + ); + }); + + it('should not set time scale if it was not set previously', () => { + expect( + countOperation.buildColumn({ + ...baseColumnArgs, + previousColumn: { + label: 'Sum of bytes', + dataType: 'number', + isBucketed: false, + operationType: 'sum', + sourceField: 'bytes', + }, + }) + ).toEqual( + expect.objectContaining({ + timeScale: undefined, + label: 'Count of records', + }) + ); + }); + + it('should set time scale to default for counter rate', () => { + expect( + counterRateOperation.buildColumn({ + indexPattern, + layer: { + columns: {}, + columnOrder: [], + indexPatternId: '1', + }, + referenceIds: [], + }) + ).toEqual( + expect.objectContaining({ + timeScale: 's', + }) + ); + }); + + it('should adjust label on field change', () => { + expect( + sumOperation.onFieldChange( + { + label: 'Sum of bytes per hour', + timeScale: 'h', + dataType: 'number', + isBucketed: false, + + // Private + operationType: 'sum', + sourceField: 'bytes', + }, + indexPattern.fields[3] + ) + ).toEqual( + expect.objectContaining({ + timeScale: 'h', + label: 'Sum of memory per hour', + }) + ); + }); + + it('should not carry over time scale if target does not support time scaling', () => { + const result = averageOperation.buildColumn({ + ...baseColumnArgs, + }); + expect(result.timeScale).toBeUndefined(); + }); +}); diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/index.ts b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/index.ts index 61f02dc7a706bf..31bb332f791da6 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/index.ts +++ b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/index.ts @@ -99,6 +99,12 @@ export { filtersOperation } from './filters'; export { dateHistogramOperation } from './date_histogram'; export { minOperation, averageOperation, sumOperation, maxOperation } from './metrics'; export { countOperation } from './count'; +export { + cumulativeSumOperation, + counterRateOperation, + derivativeOperation, + movingAverageOperation, +} from './calculations'; /** * Properties passed to the operation-specific part of the popover editor diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/operations/layer_helpers.test.ts b/x-pack/plugins/lens/public/indexpattern_datasource/operations/layer_helpers.test.ts index 31316f6e080bb4..0d103a766c23a7 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/operations/layer_helpers.test.ts +++ b/x-pack/plugins/lens/public/indexpattern_datasource/operations/layer_helpers.test.ts @@ -436,8 +436,6 @@ describe('state_helpers', () => { ); }); }); - - // TODO add test for mandatory time scale operation once counter rate is implemented }); describe('replaceColumn', () => { @@ -728,101 +726,6 @@ describe('state_helpers', () => { ); }); - describe('time scale transition', () => { - it('should carry over time scale and adjust label on operation', () => { - expect( - replaceColumn({ - layer: { - indexPatternId: '1', - columnOrder: ['col1'], - columns: { - col1: { - label: 'Count of records per hour', - timeScale: 'h', - dataType: 'number', - isBucketed: false, - - // Private - operationType: 'count', - sourceField: 'Records', - }, - }, - }, - indexPattern, - columnId: 'col1', - op: 'sum', - field: indexPattern.fields[2], - }).columns.col1 - ).toEqual( - expect.objectContaining({ - timeScale: 'h', - label: 'Sum of bytes per hour', - }) - ); - }); - - it('should carry over time scale and retain custom label', () => { - expect( - replaceColumn({ - layer: { - indexPatternId: '1', - columnOrder: ['col1'], - columns: { - col1: { - label: 'Custom label', - customLabel: true, - timeScale: 'h', - dataType: 'number', - isBucketed: false, - - // Private - operationType: 'count', - sourceField: 'Records', - }, - }, - }, - indexPattern, - columnId: 'col1', - op: 'sum', - field: indexPattern.fields[2], - }).columns.col1 - ).toEqual( - expect.objectContaining({ - timeScale: 'h', - label: 'Custom label', - }) - ); - }); - - it('should not carry over time scale if target does not support time scaling', () => { - const result = replaceColumn({ - layer: { - indexPatternId: '1', - columnOrder: ['col1'], - columns: { - col1: { - label: '', - timeScale: 'h', - dataType: 'number', - isBucketed: false, - - // Private - operationType: 'count', - sourceField: 'Records', - }, - }, - }, - indexPattern, - columnId: 'col1', - op: 'avg', - field: indexPattern.fields[2], - }).columns.col1; - expect(result.timeScale).toBeUndefined(); - }); - - // TODO add test for mandatory time scale operation once counter rate is implemented - }); - it('should execute adjustments for other columns', () => { const termsColumn: TermsIndexPatternColumn = { label: 'Top values of source',