Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Nov 30, 2020
1 parent 41200dd commit 6ab3f08
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,6 @@ describe('state_helpers', () => {
);
});
});

// TODO add test for mandatory time scale operation once counter rate is implemented
});

describe('replaceColumn', () => {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 6ab3f08

Please sign in to comment.