From 3c4cd9b1d897bb3f68e7bf50b99193b3aa51bbf9 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Sat, 7 Mar 2020 19:30:19 -0500 Subject: [PATCH] add dummy test --- .../plugins/maps/common/descriptor_types.d.ts | 8 +- .../maps/public/layers/fields/es_agg_field.ts | 1 + .../public/layers/sources/es_agg_source.d.ts | 3 + .../layers/sources/es_agg_source.test.ts | 86 +++++++++++++++++++ .../es_geo_grid_source.d.ts | 3 + .../es_geo_grid_source/es_geo_grid_source.js | 2 +- .../es_geo_grid_source.test.ts | 37 +------- .../layers/sources/es_term_source.test.js | 38 +------- 8 files changed, 104 insertions(+), 74 deletions(-) create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.test.ts diff --git a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts index f03f828200bbd..ce0743ba2baed 100644 --- a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts +++ b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts @@ -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; }; @@ -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 }; diff --git a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts index 9f08200442fea..00c6b4d26d2f2 100644 --- a/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts +++ b/x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts @@ -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 { diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts index b662cd42ee34d..99ee1ec652b54 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.d.ts @@ -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; @@ -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[]; diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.test.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.test.ts new file mode 100644 index 0000000000000..6dbc19f179a7a --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_agg_source.test.ts @@ -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); + }); +}); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts index 652409b61fd72..48e90b6c41d51 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.d.ts @@ -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; } diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js index 4987d052b8ab7..3b3e8004ded05 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.js @@ -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; diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.test.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.test.ts index fe1484f0428d9..2fcd64f292914 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.test.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/es_geo_grid_source.test.ts @@ -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 = [ @@ -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); }); }); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js index 549176dd7aa48..890b1e3aaac1f 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js @@ -5,7 +5,6 @@ */ import { ESTermSource, extractPropertiesMap } from './es_term_source'; -import _ from 'lodash'; jest.mock('ui/new_platform'); jest.mock('../vector_layer', () => {}); @@ -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', () => {