This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VisBuilder] Adds field unit tests (opensearch-project#3211)
* 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
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ins/vis_builder/public/application/components/data_tab/utils/get_available_fields.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
...lugins/vis_builder/public/application/components/data_tab/utils/get_field_details.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); |