Skip to content

Commit

Permalink
Merge branch 'master' into 111309-monitoring-react
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Oct 14, 2021
2 parents 2918dcb + cdce98c commit 3549ee3
Show file tree
Hide file tree
Showing 38 changed files with 1,039 additions and 387 deletions.

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 @@ -6,6 +6,6 @@
* Side Public License, v 1.
*/

export { tabifyDocs } from './tabify_docs';
export { tabifyDocs, flattenHit } from './tabify_docs';
export { tabifyAggResponse } from './tabify';
export { tabifyGetColumns } from './get_columns';
170 changes: 118 additions & 52 deletions src/plugins/data/common/search/tabify/tabify_docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,137 @@
* Side Public License, v 1.
*/

import { tabifyDocs } from './tabify_docs';
import { IndexPattern } from '../..';
import { tabifyDocs, flattenHit } from './tabify_docs';
import { IndexPattern, DataView } from '../..';
import type { estypes } from '@elastic/elasticsearch';

describe('tabifyDocs', () => {
const fieldFormats = {
getInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getDefaultInstance: (id: string) => ({ toJSON: () => ({ id }) }),
};
import { fieldFormatsMock } from '../../../../field_formats/common/mocks';
import { stubbedSavedObjectIndexPattern } from '../../../../data_views/common/data_view.stub';

const index = new IndexPattern({
class MockFieldFormatter {}

fieldFormatsMock.getInstance = jest.fn().mockImplementation(() => new MockFieldFormatter()) as any;

// helper function to create index patterns
function create(id: string) {
const {
type,
version,
attributes: { timeFieldName, fields, title },
} = stubbedSavedObjectIndexPattern(id);

return new DataView({
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,
},
},
id,
type,
version,
timeFieldName,
fields: JSON.parse(fields),
title,
runtimeFieldMap: {},
},
fieldFormats: fieldFormats as any,
fieldFormats: fieldFormatsMock,
shortDotsEnable: false,
metaFields: ['_id', '_type', '_score', '_routing'],
});
}

// @ts-expect-error not full inteface
const response = {
hits: {
hits: [
describe('tabify_docs', () => {
describe('flattenHit', () => {
let indexPattern: DataView;

// create an indexPattern instance for each test
beforeEach(() => {
indexPattern = create('test-pattern');
});

it('returns sorted object keys that combine _source, fields and metaFields in a defined order', () => {
const response = flattenHit(
{
_id: 'hit-id-value',
_index: 'hit-index-value',
_type: 'hit-type-value',
_score: 77,
_source: { sourceTest: 123 },
fields: { fieldTest: 123, invalidMapping: 345, nested: [{ field: 123 }] },
_index: 'foobar',
_id: 'a',
_source: {
name: 'first',
},
fields: {
date: ['1'],
zzz: ['z'],
_abc: ['a'],
},
},
],
},
} as estypes.SearchResponse<unknown>;

it('converts fields by default', () => {
const table = tabifyDocs(response, index);
expect(table).toMatchSnapshot();
indexPattern
);
const expectedOrder = ['_abc', 'date', 'name', 'zzz', '_id', '_routing', '_score', '_type'];
expect(Object.keys(response)).toEqual(expectedOrder);
expect(Object.entries(response).map(([key]) => key)).toEqual(expectedOrder);
});
});

it('converts source if option is set', () => {
const table = tabifyDocs(response, index, { source: true });
expect(table).toMatchSnapshot();
});
describe('tabifyDocs', () => {
const fieldFormats = {
getInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getDefaultInstance: (id: string) => ({ toJSON: () => ({ id }) }),
};

it('skips nested fields if option is set', () => {
const table = tabifyDocs(response, index, { shallow: true });
expect(table).toMatchSnapshot();
});
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,
},
},
},
metaFields: ['_id', '_index', '_score', '_type'],
fieldFormats: fieldFormats as any,
});

it('combines meta fields if meta option is set', () => {
const table = tabifyDocs(response, index, { meta: true });
expect(table).toMatchSnapshot();
});
// @ts-expect-error not full inteface
const response = {
hits: {
hits: [
{
_id: 'hit-id-value',
_index: 'hit-index-value',
_type: 'hit-type-value',
_score: 77,
_source: { sourceTest: 123 },
fields: { fieldTest: 123, invalidMapping: 345, nested: [{ field: 123 }] },
},
],
},
} as estypes.SearchResponse<unknown>;

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

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

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

it('combines meta fields from index pattern', () => {
const table = tabifyDocs(response, index);
expect(table.columns.map((col) => col.id)).toEqual(
expect.arrayContaining(['_id', '_index', '_score', '_type'])
);
});

it('works without provided index pattern', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot();
it('works without provided index pattern', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot();
});
});
});
Loading

0 comments on commit 3549ee3

Please sign in to comment.