Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multiple Datasource] Fix data source filter bug and add tests #6152

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG][Multiple Datasource] Fix missing customApiRegistryPromise param for test connection ([#5944](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5944))
- [BUG][Multiple Datasource] Add a migration function for datasource to add migrationVersion field ([#6025](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6025))
- [BUG][MD]Expose picker using function in data source management plugin setup([#6030](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6030))
- [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152))

### 🚞 Infrastructure

Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('DataSourceSelector: check dataSource options', () => {
disabled={false}
hideLocalCluster={false}
fullWidth={false}
filterFn={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);

Expand All @@ -152,4 +152,22 @@ describe('DataSourceSelector: check dataSource options', () => {
expect(component).toMatchSnapshot();
expect(toasts.addWarning).toBeCalledTimes(0);
});

it('should return empty options if filter out all options and hide local cluster', async () => {
component = shallow(
<DataSourceSelector
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSource={jest.fn()}
disabled={false}
hideLocalCluster={true}
fullWidth={false}
dataSourceFilter={(ds) => ds.attributes.auth.type === 'random'}
/>
);
component.instance().componentDidMount!();
await nextTick();
expect(component).toMatchSnapshot();
expect(toasts.addWarning).toBeCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import React from 'react';
import { i18n } from '@osd/i18n';
import { EuiComboBox } from '@elastic/eui';
import { SavedObjectsClientContract, ToastsStart } from 'opensearch-dashboards/public';
import { SavedObjectsClientContract, ToastsStart, SavedObject } from 'opensearch-dashboards/public';
import { getDataSourcesWithFields } from '../utils';
import { DataSourceAttributes } from '../../types';

export const LocalCluster: DataSourceOption = {
label: i18n.translate('dataSource.localCluster', {
Expand All @@ -26,7 +27,7 @@
defaultOption?: DataSourceOption[];
placeholderText?: string;
removePrepend?: boolean;
filterFn?: (dataSource: any) => boolean;
dataSourceFilter?: (dataSource: SavedObject<DataSourceAttributes>) => boolean;
compressed?: boolean;
}

Expand Down Expand Up @@ -73,14 +74,13 @@
getDataSourcesWithFields(this.props.savedObjectsClient, ['id', 'title', 'auth.type'])
.then((fetchedDataSources) => {
if (fetchedDataSources?.length) {
let filteredDataSources = [];
if (this.props.filterFn) {
filteredDataSources = fetchedDataSources.filter((ds) => this.props.filterFn!(ds));
let filteredDataSources = fetchedDataSources;

Check warning on line 77 in src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx#L77

Added line #L77 was not covered by tests
if (this.props.dataSourceFilter) {
filteredDataSources = fetchedDataSources.filter((ds) =>
this.props.dataSourceFilter!(ds)

Check warning on line 80 in src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx#L79-L80

Added lines #L79 - L80 were not covered by tests
);
}

if (filteredDataSources.length === 0) {
filteredDataSources = fetchedDataSources;
}
const dataSourceOptions = filteredDataSources.map((dataSource) => ({
id: dataSource.id,
label: dataSource.attributes?.title || '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { HttpStart, SavedObjectsClientContract } from 'src/core/public';
import { HttpStart, SavedObjectsClientContract, SavedObject } from 'src/core/public';
import {
DataSourceAttributes,
DataSourceTableItem,
Expand Down Expand Up @@ -39,8 +39,8 @@
export async function getDataSourcesWithFields(
savedObjectsClient: SavedObjectsClientContract,
fields: string[]
) {
const response = await savedObjectsClient.find({
): Promise<Array<SavedObject<DataSourceAttributes>>> {
const response = await savedObjectsClient.find<DataSourceAttributes>({

Check warning on line 43 in src/plugins/data_source_management/public/components/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/utils.ts#L43

Added line #L43 was not covered by tests
type: 'data-source',
fields,
perPage: 10000,
Expand Down
Loading