Skip to content

Commit

Permalink
add dummy test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Mar 8, 2020
1 parent cc08409 commit 3c4cd9b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 74 deletions.
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/maps/common/descriptor_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export type AggDescriptor = {
type: AGG_TYPE;
};

export type AbstractESAggDescriptor = AbstractESSourceDescriptor & {
export type AbstractESAggSourceDescriptor = AbstractESSourceDescriptor & {
metrics: AggDescriptor[];
};

export type ESGeoGridSourceDescriptor = AbstractESAggDescriptor & {
export type ESGeoGridSourceDescriptor = AbstractESAggSourceDescriptor & {
requestType?: RENDER_AS;
resolution?: GRID_RESOLUTION;
};
Expand All @@ -54,12 +54,12 @@ export type ESSearchSourceDescriptor = AbstractESSourceDescriptor & {
topHitsSize?: number;
};

export type ESPewPewSourceDescriptor = AbstractESAggDescriptor & {
export type ESPewPewSourceDescriptor = AbstractESAggSourceDescriptor & {
sourceGeoField: string;
destGeoField: string;
};

export type ESTermSourceDescriptor = AbstractESAggDescriptor & {
export type ESTermSourceDescriptor = AbstractESAggSourceDescriptor & {
indexPatternTitle: string;
term: string; // term field name
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { TopTermPercentageField } from './top_term_percentage_field';
export interface IESAggField extends IField {
getValueAggDsl(indexPattern: IndexPattern): unknown | null;
getBucketCount(): number;
getAggType(): AGG_TYPE;
}

export class ESAggField implements IESAggField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IESSource } from './es_source';
import { AbstractESSource } from './es_source';
import { AGG_TYPE } from '../../../common/constants';
import { IESAggField } from '../fields/es_agg_field';
import { AbstractESAggSourceDescriptor } from '../../../common/descriptor_types';

export interface IESAggSource extends IESSource {
getAggKey(aggType: AGG_TYPE, fieldName: string): string;
Expand All @@ -16,6 +17,8 @@ export interface IESAggSource extends IESSource {
}

export class AbstractESAggSource extends AbstractESSource implements IESAggSource {
constructor(sourceDescriptor: AbstractESAggSourceDescriptor, inspectorAdapters: object);

getAggKey(aggType: AGG_TYPE, fieldName: string): string;
getAggLabel(aggType: AGG_TYPE, fieldName: string): string;
getMetricFields(): IESAggField[];
Expand Down
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import { AbstractESAggSource } from '../es_agg_source';
import { ESGeoGridSourceDescriptor } from '../../../../common/descriptor_types';
import { GRID_RESOLUTION } from '../../../../common/constants';

export class ESGeoGridSource extends AbstractESAggSource {
constructor(sourceDescriptor: ESGeoGridSourceDescriptor, inspectorAdapters: unknown);
getGridResolution(): GRID_RESOLUTION;
getGeoGridPrecision(zoom: number): number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { DynamicStyleProperty } from '../../styles/vector/properties/dynamic_sty
import { StaticStyleProperty } from '../../styles/vector/properties/static_style_property';
import { DataRequestAbortError } from '../../util/data_request';

const MAX_GEOTILE_LEVEL = 29;
export const MAX_GEOTILE_LEVEL = 29;

export class ESGeoGridSource extends AbstractESAggSource {
static type = ES_GEO_GRID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ jest.mock('ui/new_platform');

import { ESGeoGridSource } from './es_geo_grid_source';
import { AGG_TYPE, ES_GEO_GRID, GRID_RESOLUTION, RENDER_AS } from '../../../../common/constants';
import { IField } from '../../fields/field';

describe('ESGeoGridSource', () => {
const metricExamples = [
Expand Down Expand Up @@ -41,39 +40,11 @@ describe('ESGeoGridSource', () => {
{}
);

const getFieldMeta = async (field: IField) => {
return {
field: field.getName(),
label: await field.getLabel(),
type: await field.getDataType(),
};
};

it('getMetricFields should remove incomplete metric aggregations.', async () => {
const fields = await geogridSource.getFields();
const fieldsMeta = await Promise.all(fields.map(getFieldMeta));
expect(
_.isEqual(fieldsMeta, [
{
type: 'number',
field: 'sum_of_myFieldGettingSummed',
label: 'my custom label',
},
{
type: 'number',
label: 'count',
field: 'doc_count',
},
])
).toEqual(true);
it('should echo gridResoltuion', () => {
expect(geogridSource.getGridResolution()).toBe(GRID_RESOLUTION.COARSE);
});

it('getFields should return getMetricFields', async () => {
const fields = await geogridSource.getFields();
const metrics = await geogridSource.getMetricFields();
const fieldsMeta = await Promise.all(fields.map(getFieldMeta));
const metricFieldsMeta = await Promise.all(metrics.map(getFieldMeta));

expect(_.isEqual(fieldsMeta, metricFieldsMeta)).toEqual(true);
it('should clamp geo-grid derived zoom to max geotile level supported by ES', () => {
expect(geogridSource.getGeoGridPrecision(29)).toBe(29);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { ESTermSource, extractPropertiesMap } from './es_term_source';
import _ from 'lodash';

jest.mock('ui/new_platform');
jest.mock('../vector_layer', () => {});
Expand All @@ -32,63 +31,30 @@ const metricExamples = [
];

describe('getMetricFields', () => {
it('should add default "count" metric when no metrics are provided', async () => {
it('should override name and label of count metric', async () => {
const source = new ESTermSource({
indexPatternTitle: indexPatternTitle,
term: termFieldName,
});
const metrics = source.getMetricFields();
expect(metrics.length).toBe(1);

expect(metrics[0].getAggType()).toEqual('count');
expect(metrics[0].getName()).toEqual('__kbnjoin__count_groupby_myIndex.myTermField');
expect(await metrics[0].getLabel()).toEqual('Count of myIndex');
});

it('should remove incomplete metric configurations', async () => {
it('should override name and label of sum metric', async () => {
const source = new ESTermSource({
indexPatternTitle: indexPatternTitle,
term: termFieldName,
metrics: 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(
'__kbnjoin__sum_of_myFieldGettingSummed_groupby_myIndex.myTermField'
);
expect(await metrics[0].getLabel()).toEqual('my custom label');

expect(metrics[1].getAggType()).toEqual('count');
expect(metrics[1].getName()).toEqual('__kbnjoin__count_groupby_myIndex.myTermField');
expect(await metrics[1].getLabel()).toEqual('Count of myIndex');
});

it('should match getFields since term_source is agg_source', async () => {
const source = new ESTermSource({
indexPatternTitle: indexPatternTitle,
term: termFieldName,
metrics: metricExamples,
});
const metrics = source.getMetricFields();
const fields = await source.getFields();

const getFieldMeta = async field => {
return {
aggType: field.getAggType(),
name: field.getName(),
label: await field.getLabel(),
esDoc: field.getRootName(),
};
};

const metricsFieldMeta = await Promise.all(metrics.map(getFieldMeta));
const fieldsFieldMeta = await Promise.all(fields.map(getFieldMeta));

expect(_.isEqual(metricsFieldMeta, fieldsFieldMeta)).toEqual(true);
});
});

describe('extractPropertiesMap', () => {
Expand Down

0 comments on commit 3c4cd9b

Please sign in to comment.