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

[Backport 2.x] [discover] undefined datasource fix for index patterns for PPL and SQL #8028

Merged
merged 1 commit into from
Sep 5, 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
119 changes: 119 additions & 0 deletions src/plugins/query_enhancements/server/utils/facet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { Logger } from 'opensearch-dashboards/server';
import { Facet, FacetProps } from './facet';

describe('Facet', () => {
let facet: Facet;
let mockClient: jest.Mock;
let mockLogger: jest.Mocked<Logger>;
let mockContext: any;
let mockRequest: any;

beforeEach(() => {
mockClient = jest.fn();
mockLogger = ({
error: jest.fn(),
} as unknown) as jest.Mocked<Logger>;

const props: FacetProps = {
client: { asScoped: jest.fn().mockReturnValue({ callAsCurrentUser: mockClient }) },
logger: mockLogger,
endpoint: 'test-endpoint',
};

facet = new Facet(props);

mockContext = {
dataSource: {
opensearch: {
legacy: {
getClient: jest.fn().mockReturnValue({ callAPI: mockClient }),
},
},
},
};

mockRequest = {
body: {
query: {
query: 'test query',
dataset: {
dataSource: {
id: 'test-id',
meta: {
name: 'test-name',
sessionId: 'test-session',
},
},
},
},
format: 'jdbc',
lang: 'sql',
},
};
});

describe('describeQuery', () => {
it('should handle request with complete dataset information', async () => {
mockClient.mockResolvedValue({ result: 'success' });

const result = await facet.describeQuery(mockContext, mockRequest);

expect(result).toEqual({ success: true, data: { result: 'success' } });
expect(mockClient).toHaveBeenCalledWith('test-endpoint', {
body: {
query: 'test query',
datasource: 'test-name',
sessionId: 'test-session',
lang: 'sql',
},
});
});

it('should handle request with missing dataSource', async () => {
mockRequest.body.query.dataset.dataSource = undefined;
mockClient.mockResolvedValue({ result: 'success' });

const result = await facet.describeQuery(mockContext, mockRequest);

expect(result).toEqual({ success: true, data: { result: 'success' } });
expect(mockClient).toHaveBeenCalledWith('test-endpoint', {
body: {
query: 'test query',
lang: 'sql',
},
});
});

it('should handle request with missing dataset', async () => {
mockRequest.body.query.dataset = undefined;
mockClient.mockResolvedValue({ result: 'success' });

const result = await facet.describeQuery(mockContext, mockRequest);

expect(result).toEqual({ success: true, data: { result: 'success' } });
expect(mockClient).toHaveBeenCalledWith('test-endpoint', {
body: {
query: 'test query',
lang: 'sql',
},
});
});

it('should handle errors', async () => {
const error = new Error('Test error');
mockClient.mockRejectedValue(error);

const result = await facet.describeQuery(mockContext, mockRequest);

expect(result).toEqual({ success: false, data: error });
expect(mockLogger.error).toHaveBeenCalledWith(
'Facet fetch: test-endpoint: Error: Test error'
);
});
});
});
4 changes: 2 additions & 2 deletions src/plugins/query_enhancements/server/utils/facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class Facet {
): Promise<FacetResponse> => {
try {
const query: Query = request.body.query;
const { dataSource } = query.dataset!;
const { meta } = dataSource!;
const dataSource = query.dataset?.dataSource;
const meta = dataSource?.meta;
const { format, lang } = request.body;
const params = {
body: {
Expand Down
Loading