Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Oct 15, 2020
1 parent 08256ae commit c7a1243
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import _, { each, reject } from 'lodash';
import { SavedObjectsClientCommon } from '../..';
import { DuplicateField } from '../../../../kibana_utils/common';

import {
Expand All @@ -37,10 +36,9 @@ import { SerializedFieldFormat } from '../../../../expressions/common';

interface IndexPatternDeps {
spec?: IndexPatternSpec;
savedObjectsClient: SavedObjectsClientCommon;
fieldFormats: FieldFormatsStartCommon;
shortDotsEnable: boolean;
metaFields: string[];
shortDotsEnable?: boolean;
metaFields?: string[];
}

interface SavedObjectBody {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/plugins/data/common/search/tabify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { tabifyDocs } from './tabify_docs';

export const tabify = (searchSource: SearchSource, esResponse: any, opts: any) => {
return !esResponse.aggregations
? tabifyDocs(searchSource, esResponse, opts)
? tabifyDocs(esResponse, searchSource.getField('index'), opts)
: tabifyAggResponse(searchSource.getField('aggs'), esResponse, opts);
};

Expand Down
77 changes: 77 additions & 0 deletions src/plugins/data/common/search/tabify/tabify_docs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { tabifyDocs } from './tabify_docs';
import { IndexPattern } from '../../index_patterns/index_patterns';
import { SearchResponse } from 'elasticsearch';

describe('tabifyDocs', () => {
const fieldFormats = {
getInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getDefaultInstance: (id: string) => ({ toJSON: () => ({ id }) }),
};

const index = new IndexPattern({
spec: {
id: 'test-index',
fields: {
sourceTest: { name: 'sourceTest', type: 'number', searchable: true, aggregatable: true },
fieldTest: { name: 'fieldTest', type: 'number', searchable: true, aggregatable: true },
'nested.field': {
name: 'nested.field',
type: 'number',
searchable: true,
aggregatable: true,
},
},
},
fieldFormats: fieldFormats as any,
});

const response = {
hits: {
hits: [
{
_source: { sourceTest: 123 },
fields: { fieldTest: 123, invalidMapping: 345, nested: [{ field: 123 }] },
},
],
},
} as SearchResponse<unknown>;

it('converts fields by default', () => {
const table = tabifyDocs(response, index);
expect(table).toMatchSnapshot('converts fields by default');
});

it('converts source if option is set', () => {
const table = tabifyDocs(response, index, { source: true });
expect(table).toMatchSnapshot('converts source if option is set');
});

it('skips nested fields if option is set', () => {
const table = tabifyDocs(response, index, { shallow: true });
expect(table).toMatchSnapshot('skips nested fields if option is set');
});

it('works without provided index pattern', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot('works without provided index pattern');
});
});
22 changes: 10 additions & 12 deletions src/plugins/data/common/search/tabify/tabify_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import { SearchResponse } from 'elasticsearch';
import _ from 'lodash';
import { SearchSource } from '../search_source';
import { IndexPattern } from '../../index_patterns/index_patterns';
import { DatatableColumn } from '../../../../expressions/common/expression_types/specs';

export function flattenHit(
hit: Record<string, any>,
indexPattern?: IndexPattern,
deep: boolean = false
shallow: boolean = false
) {
const flat = {} as Record<string, any>;

Expand All @@ -36,7 +35,7 @@ export function flattenHit(

const field = indexPattern?.fields.getByName(key);

if (deep) {
if (!shallow) {
const isNestedField = field?.type === 'nested';
const isArrayOfObjects = Array.isArray(val) && _.isPlainObject(_.first(val));
if (isArrayOfObjects && !isNestedField) {
Expand Down Expand Up @@ -70,36 +69,35 @@ export function flattenHit(
}

export interface TabifyDocsOptions {
deep: boolean;
source: boolean;
shallow?: boolean;
source?: boolean;
}

export const tabifyDocs = (
searchSource: SearchSource,
esResponse: SearchResponse<unknown>,
{ deep, source }: TabifyDocsOptions
index?: IndexPattern,
params: TabifyDocsOptions = {}
) => {
const index = searchSource.getField('index');

const columns: DatatableColumn[] = [];

const rows = esResponse.hits.hits
.map((hit) => {
const toConvert = source ? hit._source : hit.fields;
const flat = flattenHit(toConvert, index, deep);
const toConvert = params.source ? hit._source : hit.fields;
const flat = flattenHit(toConvert, index, params.shallow);
for (const [key, value] of Object.entries(flat)) {
const field = index?.fields.getByName(key);
if (!columns.find((c) => c.id === (field?.name || key))) {
const fieldName = field?.name || key;
const fieldType = (field?.type as any) || typeof value;
const formatter = field && index?.getFormatterForField(field);
columns.push({
id: fieldName,
name: fieldName,
meta: {
type: fieldType,
field: fieldName,
index: index?.id,
params: index?.getFormatterForField(field!).toJSON(),
params: formatter ? formatter.toJSON() : undefined,
},
});
}
Expand Down

0 comments on commit c7a1243

Please sign in to comment.