Skip to content

Commit

Permalink
[VisBuilder] Adds field unit tests (#3211) (#3222)
Browse files Browse the repository at this point in the history
* Adds field tests

Signed-off-by: Ashwin P Chandran <[email protected]>

* Adds changelog

Signed-off-by: Ashwin P Chandran <[email protected]>

Signed-off-by: Ashwin P Chandran <[email protected]>
(cherry picked from commit 8bc2a6b)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent dd28a5b commit c485e9b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { FieldSpec } from '../../../../../../data/common';
import { IndexPatternField, OSD_FIELD_TYPES } from '../../../../../../data/public';
import { getAvailableFields } from './get_available_fields';

describe('getAvailableFields', () => {
const createIndexFields = (fields: Array<Partial<FieldSpec>>) =>
fields.map(
(field) =>
new IndexPatternField(
{
aggregatable: true,
name: 'field 1',
searchable: false,
type: OSD_FIELD_TYPES.STRING,
...field,
},
field.name || 'field'
)
);

test('should return only aggregateable fields by default', () => {
const fields = createIndexFields([
{
name: 'field 1',
},
{
aggregatable: false,
name: 'field 2',
},
{
scripted: true,
name: 'field 3',
},
{
name: 'field 4',
subType: {
nested: { path: 'something' },
},
},
]);

expect(getAvailableFields(fields).length).toBe(1);
});

test('should return all fields if filterFieldTypes was not specified', () => {
const fields = createIndexFields([
{
name: 'field 1',
},
{
name: 'field 2',
},
]);

expect(getAvailableFields(fields).length).toBe(2);
});

test('should filterFieldTypes', () => {
const fields = createIndexFields([
{
name: 'field 1',
},
{
name: 'field 2',
type: OSD_FIELD_TYPES.BOOLEAN,
},
{
name: 'field 3',
type: OSD_FIELD_TYPES.BOOLEAN,
},
]);

expect(getAvailableFields(fields, OSD_FIELD_TYPES.BOOLEAN).length).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// @ts-ignore
import realHits from 'fixtures/real_hits.js';
// @ts-ignore
import stubbedLogstashFields from 'fixtures/logstash_fields';
import { coreMock } from '../../../../../../../core/public/mocks';
import { getStubIndexPattern } from '../../../../../../data/public/test_utils';
import { getFieldDetails } from './get_field_details';
import _ from 'lodash';

describe('getFieldDetails', () => {
const indexPattern = getStubIndexPattern(
'logstash-*',
(cfg: any) => cfg,
'time',
stubbedLogstashFields(),
coreMock.createSetup()
);

test('should have error if index pattern is missing', () => {
const details = getFieldDetails(indexPattern.fields[0], []);

expect(details.total).toBe(0);
expect(details.error).toMatchInlineSnapshot(`"Index pattern not specified."`);
});

test('should have error if there are no hits', () => {
const details = getFieldDetails(indexPattern.fields[0], [], indexPattern);

expect(details.total).toBe(0);
expect(details.error).toMatchInlineSnapshot(
`"No documents match the selected query and filters. Try increasing time range or removing filters."`
);
});

test('should show details if hits are available for the index pattern field', () => {
const details = getFieldDetails(
indexPattern.fields[0],
_.each(_.cloneDeep(realHits), (hit) => indexPattern.flattenHit(hit)),
indexPattern
);

expect(details.exists).toBe(20);
expect(details.total).toBe(20);
expect(details.buckets.length).toBe(5);
expect(details.error).toBe('');
});
});

0 comments on commit c485e9b

Please sign in to comment.