-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc08409
commit 3c4cd9b
Showing
8 changed files
with
104 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 { AbstractESAggSource } from './es_agg_source'; | ||
import { IField } from '../fields/field'; | ||
import { IESAggField } from '../fields/es_agg_field'; | ||
import _ from 'lodash'; | ||
import { AGG_TYPE } from '../../../common/constants'; | ||
import { AggDescriptor } from '../../../common/descriptor_types'; | ||
|
||
jest.mock('ui/new_platform'); | ||
|
||
const sumFieldName = 'myFieldGettingSummed'; | ||
const metricExamples = [ | ||
{ | ||
type: AGG_TYPE.SUM, | ||
field: sumFieldName, | ||
label: 'my custom label', | ||
}, | ||
{ | ||
// metric config is invalid beause field is missing | ||
type: AGG_TYPE.MAX, | ||
}, | ||
{ | ||
// metric config is valid because "count" metric does not need to provide field | ||
type: AGG_TYPE.COUNT, | ||
label: '', // should ignore empty label fields | ||
}, | ||
]; | ||
|
||
class TestESAggSource extends AbstractESAggSource { | ||
constructor(metrics: AggDescriptor[]) { | ||
super({ type: 'test', id: 'foobar', indexPatternId: 'foobarid', metrics }, []); | ||
} | ||
} | ||
|
||
describe('getMetricFields', () => { | ||
it('should add default "count" metric when no metrics are provided', async () => { | ||
const source = new TestESAggSource([]); | ||
const metrics = source.getMetricFields(); | ||
expect(metrics.length).toBe(1); | ||
|
||
expect(metrics[0].getAggType()).toEqual('count'); | ||
expect(metrics[0].getName()).toEqual('doc_count'); | ||
expect(await metrics[0].getLabel()).toEqual('count'); | ||
}); | ||
|
||
it('should remove incomplete metric configurations', async () => { | ||
const source = new TestESAggSource(metricExamples); | ||
const metrics = source.getMetricFields(); | ||
expect(metrics.length).toBe(2); | ||
|
||
expect(metrics[0].getAggType()).toEqual('sum'); | ||
expect(metrics[0].getRootName()).toEqual(sumFieldName); | ||
expect(metrics[0].getName()).toEqual('sum_of_myFieldGettingSummed'); | ||
expect(await metrics[0].getLabel()).toEqual('my custom label'); | ||
|
||
expect(metrics[1].getAggType()).toEqual('count'); | ||
expect(metrics[1].getName()).toEqual('doc_count'); | ||
expect(await metrics[1].getLabel()).toEqual('count'); | ||
}); | ||
|
||
it('getMetrics should be identical to getFields', async () => { | ||
const source = new TestESAggSource(metricExamples); | ||
const metrics = source.getMetricFields(); | ||
const fields = await source.getFields(); | ||
|
||
const getFieldMeta = async (field: IField) => { | ||
const esAggField = field as IESAggField; | ||
return { | ||
aggType: esAggField.getAggType(), | ||
name: esAggField.getName(), | ||
label: await esAggField.getLabel(), | ||
esDoc: esAggField.getRootName(), | ||
}; | ||
}; | ||
|
||
const metricsFieldMeta = await Promise.all(metrics.map(getFieldMeta)); | ||
const fieldsFieldMeta = await Promise.all(fields.map(getFieldMeta)); | ||
|
||
expect(_.isEqual(metricsFieldMeta, fieldsFieldMeta)).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters