Skip to content

Commit

Permalink
[UT] Update unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Xinrui Bai <[email protected]>
  • Loading branch information
xinruiba committed Feb 26, 2024
1 parent 4075f1e commit 21ad2d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ describe('OpenSearch search strategy', () => {
},
dataSourceEnabled: jest.fn(() => true),
registerCredentialProvider: jest.fn(),
registerCustomApiSchema(schema: any): void {
throw new Error('Function not implemented.');
},
};
const mockDataSourcePluginSetupWithDataSourceDisabled: DataSourcePluginSetup = {
createDataSourceError(err: any): DataSourceError {
return new DataSourceError({});
},
dataSourceEnabled: jest.fn(() => false),
registerCredentialProvider: jest.fn(),
registerCustomApiSchema(schema: any): void {
throw new Error('Function not implemented.');
},
};
const body = {
body: {
Expand All @@ -69,6 +82,7 @@ describe('OpenSearch search strategy', () => {
};
const mockOpenSearchApiCaller = jest.fn().mockResolvedValue(body);
const mockDataSourceApiCaller = jest.fn().mockResolvedValue(body);
const mockOpenSearchApiCallerWithLongNumeralsSupport = jest.fn().mockResolvedValue(body);
const dataSourceId = 'test-data-source-id';
const mockDataSourceContext = {
dataSource: {
Expand All @@ -86,7 +100,14 @@ describe('OpenSearch search strategy', () => {
get: () => {},
},
},
opensearch: { client: { asCurrentUser: { search: mockOpenSearchApiCaller } } },
opensearch: {
client: {
asCurrentUser: { search: mockOpenSearchApiCaller },
asCurrentUserWithLongNumeralsSupport: {
search: mockOpenSearchApiCallerWithLongNumeralsSupport,
},
},
},
},
};
const mockDataSourceEnabledContext = {
Expand Down Expand Up @@ -215,10 +236,10 @@ describe('OpenSearch search strategy', () => {
const hostsTobeTested = [undefined, []];
const dataSourceIdToBeTested = [undefined, '', dataSourceId];

hostsTobeTested.forEach(() => {
hostsTobeTested.forEach((host) => {
const mockOpenSearchServiceSetup = opensearchServiceMock.createSetup();

if (hostsTobeTested !== undefined) {
if (host !== undefined) {
mockOpenSearchServiceSetup.legacy.client = {
callAsInternalUser: jest.fn(),
asScoped: jest.fn(),
Expand Down Expand Up @@ -276,4 +297,27 @@ describe('OpenSearch search strategy', () => {
expect(mockDataSourceApiCaller).not.toBeCalled();
}
});

it('dataSource disabled and longNumeralsSupported, send request without dataSourceId should get longNumeralsSupport client', async () => {
const mockOpenSearchServiceSetup = opensearchServiceMock.createSetup();
const opensearchSearch = await opensearchSearchStrategyProvider(
mockConfig$,
mockLogger,
mockSearchUsage,
mockDataSourcePluginSetupWithDataSourceDisabled,
mockOpenSearchServiceSetup,
true
);

const dataSourceIdToBeTested = [undefined, ''];

for (const testDataSourceId of dataSourceIdToBeTested) {
await opensearchSearch.search((mockContext as unknown) as RequestHandlerContext, {
dataSourceId: testDataSourceId,
});
expect(mockOpenSearchApiCallerWithLongNumeralsSupport).toBeCalled();
expect(mockOpenSearchApiCaller).not.toBeCalled();
expect(mockDataSourceApiCaller).not.toBeCalled();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const opensearchSearchStrategyProvider = (

try {
const isOpenSearchHostsEmpty =
openSearchServiceSetup?.legacy.client.config.hosts.length === 0;
openSearchServiceSetup?.legacy?.client?.config?.hosts?.length === 0;

if (dataSource?.dataSourceEnabled() && isOpenSearchHostsEmpty && !request.dataSourceId) {
throw new Error(`Data source id is required when no openseach hosts config provided`);
Expand Down

0 comments on commit 21ad2d8

Please sign in to comment.