Skip to content

Commit

Permalink
Fix failing test case and some clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
saarikabhasi committed Mar 27, 2023
1 parent a4c18b9 commit 6916094
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,62 @@ describe('fetchIndicesStats lib function', () => {
},
asInternalUser: {},
};
const indicesNames = ['index-name-1', 'index-name-2'];
const indicesWithStatsResponse = [
const indicesNames = ['test-index-name-1', 'test-index-name-2', 'test-index-name-3'];
const indicesStats = {
indices: {
'test-index-name-1': {
health: 'GREEN',
primaries: { docs: [{}] },
status: 'open',
total: {
docs: {
count: 200,
deleted: 0,
},
},
uuid: 'YOLLiZ_mSRiDYDk0DJ-p8B',
},
'test-index-name-2': {
health: 'YELLOW',
primaries: { docs: [{}] },
status: 'closed',
total: {
docs: {
count: 0,
deleted: 0,
},
},
uuid: 'QOLLiZ_mGRiDYD30D2-p8B',
},
'test-index-name-3': {
health: 'RED',
primaries: { docs: [{}] },
status: 'open',
total: {
docs: {
count: 150,
deleted: 0,
},
},
uuid: 'QYLLiZ_fGRiDYD3082-e7',
},
},
};
const fetchIndicesStatsResponse = [
{
name: 'index-name-1',
count: 10,
count: 200,
health: 'GREEN',
name: 'test-index-name-1',
},
{
name: 'index-name-2',
count: 0,
health: 'unknown',
health: 'YELLOW',
name: 'test-index-name-2',
},
{
count: 150,
health: 'RED',
name: 'test-index-name-3',
},
];

Expand All @@ -36,13 +81,11 @@ describe('fetchIndicesStats lib function', () => {
});

it('should return hydrated indices', async () => {
mockClient.asCurrentUser.indices.stats.mockImplementation(() => indicesWithStatsResponse);
mockClient.asCurrentUser.indices.stats.mockImplementationOnce(() => indicesStats);

await expect(
fetchIndicesStats(mockClient as unknown as IScopedClusterClient, indicesNames)
).resolves.toEqual({
indicesWithStatsResponse,
});
).resolves.toEqual(fetchIndicesStatsResponse);

expect(mockClient.asCurrentUser.indices.stats).toHaveBeenCalledWith({
index: indicesNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export const fetchIndicesStats = async (client: IScopedClusterClient, indicesNam
index: indicesNames,
metric: ['docs'],
});

const indicesWithStats = indicesNames.map((indexName: string) => {
const indiceStats = indicesStats[indexName];
const indexStats = indicesStats[indexName];
const hydratedIndex: EnterpriseSearchEngineIndex = {
count: indexStats?.total?.docs?.count ?? 0,
health: indexStats?.health ?? 'unknown',
name: indexName,
health: indiceStats?.health ?? 'unknown',
count: indiceStats?.total?.docs?.count ?? 0,
};
return hydratedIndex;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ jest.mock('../../utils/fetch_enterprise_search', () => ({
jest.mock('../../lib/engines/field_capabilities', () => ({
fetchEngineFieldCapabilities: jest.fn(),
}));

jest.mock('../../lib/engines/fetch_indices_stats', () => ({
fetchIndicesStats: jest.fn(),
}));
import { RequestHandlerContext } from '@kbn/core/server';

import { fetchIndicesStats } from '../../lib/engines/fetch_indices_stats';
import { fetchEngineFieldCapabilities } from '../../lib/engines/field_capabilities';
import { fetchEnterpriseSearch } from '../../utils/fetch_enterprise_search';

Expand Down Expand Up @@ -120,6 +123,22 @@ describe('engines routes', () => {
method: 'GET',
path: '/_application/search_application/engine-name',
});
const mock = jest.fn();

const fetchIndicesStatsResponse = [
{ count: 5, health: 'green', name: 'test-index-name-1' },
{ count: 10, health: 'yellow', name: 'test-index-name-2' },
{ count: 0, health: 'red', name: 'test-index-name-3' },
];
const engineResult = {
indices: mock(['test-index-name-1', 'test-index-name-2', 'test-index-name-3']),
name: 'test-engine-1',
updated_at_millis: 1679847286355,
};

(fetchIndicesStats as jest.Mock).mockResolvedValueOnce(fetchIndicesStatsResponse);
expect(fetchIndicesStats).toHaveBeenCalledWith(mockClient, engineResult.indices);

expect(mockRouter.response.ok).toHaveBeenCalledWith({
body: {},
});
Expand Down Expand Up @@ -171,20 +190,20 @@ describe('engines routes', () => {
}));

await mockRouter.callRoute({
body: {
indices: ['test-indices-1'],
},
params: {
engine_name: 'engine-name',
},
query: { create: true },
body: {
indices: ['test-indices-1'],
},
});
expect(mockClient.asCurrentUser.transport.request).toHaveBeenCalledWith({
method: 'PUT',
path: '/_application/search_application/engine-name',
body: {
indices: ['test-indices-1'],
},
method: 'PUT',
path: '/_application/search_application/engine-name',
querystring: { create: true },
});
const mock = jest.fn();
Expand All @@ -202,19 +221,19 @@ describe('engines routes', () => {
}));

await mockRouter.callRoute({
params: {
engine_name: 'engine-name',
},
body: {
indices: ['test-indices-1'],
},
params: {
engine_name: 'engine-name',
},
});
expect(mockClient.asCurrentUser.transport.request).toHaveBeenCalledWith({
method: 'PUT',
path: '/_application/search_application/engine-name',
body: {
indices: ['test-indices-1'],
},
method: 'PUT',
path: '/_application/search_application/engine-name',
querystring: {},
});
const mock = jest.fn();
Expand All @@ -229,10 +248,10 @@ describe('engines routes', () => {

it('validates correctly with engine_name', () => {
const request = {
params: { engine_name: 'some-engine' },
body: {
indices: ['search-unit-test'],
},
params: { engine_name: 'some-engine' },
};

mockRouter.shouldValidate(request);
Expand All @@ -246,10 +265,10 @@ describe('engines routes', () => {

it('fails validation without indices', () => {
const request = {
params: { engine_name: 'some-engine' },
body: {
name: 'some-engine',
},
params: { engine_name: 'some-engine' },
};

mockRouter.shouldThrow(request);
Expand Down Expand Up @@ -354,9 +373,9 @@ describe('engines routes', () => {
},
});
expect(mockClient.asCurrentUser.transport.request).toHaveBeenCalledWith({
body: {},
method: 'POST',
path: '/engine-name/_search',
body: {},
});
expect(mockRouter.response.ok).toHaveBeenCalledWith({
body: {
Expand All @@ -368,8 +387,8 @@ describe('engines routes', () => {
it('validates correctly with engine_name and pagination', () => {
const request = {
body: {
query: 'test-query',
fields: ['test-field-1', 'test-field-2'],
query: 'test-query',
},
params: {
engine_name: 'some-engine',
Expand All @@ -393,12 +412,12 @@ describe('engines routes', () => {

it('validation with query and without fields', () => {
const request = {
params: {
engine_name: 'my-test-engine',
},
body: {
query: 'sample-query',
fields: [],
query: 'sample-query',
},
params: {
engine_name: 'my-test-engine',
},
};
mockRouter.shouldValidate(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ export function registerEnginesRoutes({ config, log, router }: RouteDependencies
indices: schema.arrayOf(schema.string()),
name: schema.maybe(schema.string()),
}),
query: schema.object({
create: schema.maybe(schema.boolean()),
}),
params: schema.object({
engine_name: schema.string(),
}),
query: schema.object({
create: schema.maybe(schema.boolean()),
}),
},
},
elasticsearchErrorHandler(log, async (context, request, response) => {
const { client } = (await context.core).elasticsearch;
const engine =
await client.asCurrentUser.transport.request<EnterpriseSearchEngineUpsertResponse>({
body: { indices: request.body.indices },
method: 'PUT',
path: `/_application/search_application/${request.params.engine_name}`,
body: { indices: request.body.indices },
querystring: request.query,
});
return response.ok({ body: engine });
Expand Down Expand Up @@ -138,9 +138,9 @@ export function registerEnginesRoutes({ config, log, router }: RouteDependencies
elasticsearchErrorHandler(log, async (context, request, response) => {
const { client } = (await context.core).elasticsearch;
const engines = await client.asCurrentUser.transport.request<SearchResponse>({
body: {},
method: 'POST',
path: `/${request.params.engine_name}/_search`,
body: {},
});
return response.ok({ body: engines });
})
Expand Down

0 comments on commit 6916094

Please sign in to comment.