Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

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

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>

* Adds changelog

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>
Signed-off-by: David Sinclair <david@sinclair.tech>
  • Loading branch information
ashwin-pc authored and sikhote committed Apr 24, 2023
1 parent 7754824 commit 0be2c98
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Make tests covering plugin installation on cluster snapshots work across platforms ([#2994](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2994))
- Correct the linting logic for `no-restricted-path` to ignore trailing slashes ([#3020](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3020))
- [Tests] Bumps `chromedriver` to v107 ([#3017](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3017))
- [Vis Builder] Adds field unit tests ([#3211](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3211))

## [2.x]

Expand Down
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 0be2c98

Please sign in to comment.