Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
demjened committed Oct 5, 2022
1 parent 2f28e1c commit d7b98f5
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { ElasticsearchClient } from '@kbn/core/server';

import { getMlInferenceErrors } from './get_inference_errors';

describe('getMlInferenceErrors', () => {
const indexName = 'my-index';

const mockClient = {
search: jest.fn(),
}

beforeEach(() => {
jest.clearAllMocks();
});

it('should fetch aggregations and transform them', async () => {
mockClient.search.mockImplementation(() => Promise.resolve({
aggregations: {
errors: {
buckets: [
{
key: 'Error message 1',
doc_count: 100,
max_error_timestamp: {
value: 1664977836100,
value_as_string: '2022-10-05T13:50:36.100Z'
}
},
{
key: 'Error message 2',
doc_count: 200,
max_error_timestamp: {
value: 1664977836200,
value_as_string: '2022-10-05T13:50:36.200Z'
}
},
]
}
}
}));

const actualResult = await getMlInferenceErrors(indexName, mockClient as unknown as ElasticsearchClient);

expect(actualResult).toEqual([
{
message: 'Error message 1',
doc_count: 100,
timestamp: '2022-10-05T13:50:36.100Z',
},
{
message: 'Error message 2',
doc_count: 200,
timestamp: '2022-10-05T13:50:36.200Z',
}
]);
expect(mockClient.search).toHaveBeenCalledTimes(1);
});

it('should return an empty array if there are no aggregates', async () => {
mockClient.search.mockImplementation(() => Promise.resolve({
aggregations: {
errors: []
}
}));

const actualResult = await getMlInferenceErrors(indexName, mockClient as unknown as ElasticsearchClient);

expect(actualResult).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ jest.mock('../../lib/indices/delete_ml_inference_pipeline', () => ({
jest.mock('../../lib/indices/exists_index', () => ({
indexOrAliasExists: jest.fn(),
}));
jest.mock('../../lib/ml_inference_pipeline/get_inference_errors', () => ({
getMlInferenceErrors: jest.fn(),
}));

import { deleteMlInferencePipeline } from '../../lib/indices/delete_ml_inference_pipeline';
import { indexOrAliasExists } from '../../lib/indices/exists_index';
import { fetchMlInferencePipelineProcessors } from '../../lib/indices/fetch_ml_inference_pipeline_processors';
import { createAndReferenceMlInferencePipeline } from '../../utils/create_ml_inference_pipeline';
import { getMlInferenceErrors } from '../../lib/ml_inference_pipeline/get_inference_errors';
import { ElasticsearchResponseError } from '../../utils/identify_exceptions';

import { registerIndexRoutes } from './indices';
Expand All @@ -40,13 +44,75 @@ describe('Enterprise Search Managed Indices', () => {
putPipeline: jest.fn(),
simulate: jest.fn(),
},
search: jest.fn(),
},
};

const mockCore = {
elasticsearch: { client: mockClient },
};

describe('GET /internal/enterprise_search/indices/{indexName}/ml_inference/errors', () => {
beforeEach(() => {
const context = {
core: Promise.resolve(mockCore),
} as unknown as jest.Mocked<RequestHandlerContext>;

mockRouter = new MockRouter({
context,
method: 'get',
path: '/internal/enterprise_search/indices/{indexName}/ml_inference/errors',
});

registerIndexRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('fails validation without index_name', () => {
const request = {
params: {},
};
mockRouter.shouldThrow(request);
});

it('fetches ML inference errors', async () => {
const errorsResult = [
{
message: 'Error message 1',
doc_count: 100,
timestamp: '2022-10-05T13:50:36.100Z',
},
{
message: 'Error message 2',
doc_count: 200,
timestamp: '2022-10-05T13:50:36.200Z',
},
];

(getMlInferenceErrors as jest.Mock).mockImplementationOnce(() => {
return Promise.resolve(errorsResult);
});

await mockRouter.callRoute({
params: { indexName: 'my-index-name' }
});

expect(getMlInferenceErrors).toHaveBeenCalledWith(
'my-index-name',
mockClient.asCurrentUser
);

expect(mockRouter.response.ok).toHaveBeenCalledWith({
body: {
errors: errorsResult,
},
headers: { 'content-type': 'application/json' },
});
});
});

describe('GET /internal/enterprise_search/indices/{indexName}/ml_inference/pipeline_processors', () => {
beforeEach(() => {
const context = {
Expand Down

0 comments on commit d7b98f5

Please sign in to comment.