forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
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
More unit tests #13
Merged
stacey-gammon
merged 1 commit into
stacey-gammon:2019-09-25-np-search-api
from
lukasolson:np-search-api
Oct 4, 2019
Merged
More unit tests #13
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/plugins/data/server/search/es_search/es_search_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { coreMock } from '../../../../../core/server/mocks'; | ||
import { EsSearchService } from './es_search_service'; | ||
import { PluginInitializerContext } from '../../../../../core/server'; | ||
import { searchSetupMock } from '../mocks'; | ||
|
||
describe('ES search strategy service', () => { | ||
let service: EsSearchService; | ||
|
||
const mockCoreSetup = coreMock.createSetup(); | ||
const opaqueId = Symbol(); | ||
const context: PluginInitializerContext = { | ||
opaqueId, | ||
config: { | ||
createIfExists: jest.fn(), | ||
create: jest.fn(), | ||
}, | ||
env: { | ||
mode: { | ||
dev: false, | ||
name: 'development', | ||
prod: false, | ||
}, | ||
}, | ||
logger: { | ||
get: jest.fn(), | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
service = new EsSearchService(context); | ||
}); | ||
|
||
describe('setup()', () => { | ||
it('registers the ES search strategy', async () => { | ||
service.setup(mockCoreSetup, { | ||
search: searchSetupMock, | ||
}); | ||
expect(searchSetupMock.registerSearchStrategyProvider).toBeCalled(); | ||
}); | ||
}); | ||
}); |
102 changes: 102 additions & 0 deletions
102
src/plugins/data/server/search/es_search/es_search_strategy.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { coreMock } from '../../../../../core/server/mocks'; | ||
import { esSearchStrategyProvider } from './es_search_strategy'; | ||
|
||
describe('ES search strategy', () => { | ||
const mockCoreSetup = coreMock.createSetup(); | ||
const mockApiCaller = jest.fn().mockResolvedValue({ | ||
_shards: { | ||
total: 10, | ||
failed: 1, | ||
skipped: 2, | ||
successful: 7, | ||
}, | ||
}); | ||
const mockSearch = jest.fn(); | ||
|
||
beforeEach(() => { | ||
mockApiCaller.mockClear(); | ||
mockSearch.mockClear(); | ||
}); | ||
|
||
it('returns a strategy with `search`', () => { | ||
const esSearch = esSearchStrategyProvider( | ||
{ | ||
core: mockCoreSetup, | ||
}, | ||
mockApiCaller, | ||
mockSearch | ||
); | ||
|
||
expect(typeof esSearch.search).toBe('function'); | ||
}); | ||
|
||
it('logs the response if `debug` is set to `true`', () => { | ||
const spy = jest.spyOn(console, 'log'); | ||
const esSearch = esSearchStrategyProvider( | ||
{ | ||
core: mockCoreSetup, | ||
}, | ||
mockApiCaller, | ||
mockSearch | ||
); | ||
|
||
expect(spy).not.toBeCalled(); | ||
|
||
esSearch.search({ params: {}, debug: true }); | ||
|
||
expect(spy).toBeCalled(); | ||
}); | ||
|
||
it('calls the API caller with the params', () => { | ||
const params = { index: 'logstash-*' }; | ||
const esSearch = esSearchStrategyProvider( | ||
{ | ||
core: mockCoreSetup, | ||
}, | ||
mockApiCaller, | ||
mockSearch | ||
); | ||
|
||
esSearch.search({ params }); | ||
|
||
expect(mockApiCaller).toBeCalled(); | ||
expect(mockApiCaller.mock.calls[0][0]).toBe('search'); | ||
expect(mockApiCaller.mock.calls[0][1]).toEqual(params); | ||
}); | ||
|
||
it('returns total, loaded, and raw response', async () => { | ||
const params = { index: 'logstash-*' }; | ||
const esSearch = esSearchStrategyProvider( | ||
{ | ||
core: mockCoreSetup, | ||
}, | ||
mockApiCaller, | ||
mockSearch | ||
); | ||
|
||
const response = await esSearch.search({ params }); | ||
|
||
expect(response).toHaveProperty('total'); | ||
expect(response).toHaveProperty('loaded'); | ||
expect(response).toHaveProperty('rawResponse'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { httpServiceMock, httpServerMock } from '../../../../../src/core/server/mocks'; | ||
import { registerSearchRoute } from './routes'; | ||
import { IRouter, ScopedClusterClient } from 'kibana/server'; | ||
|
||
describe('Search service', () => { | ||
let routerMock: jest.Mocked<IRouter>; | ||
|
||
beforeEach(() => { | ||
routerMock = httpServiceMock.createRouter(); | ||
}); | ||
|
||
it('registers a post route', async () => { | ||
registerSearchRoute(routerMock); | ||
expect(routerMock.post).toBeCalled(); | ||
}); | ||
|
||
it('handler calls context.search.search with the given request and strategy', async () => { | ||
const mockSearch = jest.fn().mockResolvedValue('yay'); | ||
const mockContext = { | ||
core: { | ||
elasticsearch: { | ||
dataClient: {} as ScopedClusterClient, | ||
adminClient: {} as ScopedClusterClient, | ||
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also not a fan of this, but also didn't see a way to get a mock for this. |
||
}, | ||
}, | ||
search: { | ||
search: mockSearch, | ||
}, | ||
}; | ||
const mockBody = { params: {} }; | ||
const mockParams = { strategy: 'foo' }; | ||
const mockRequest = httpServerMock.createKibanaRequest({ | ||
body: mockBody, | ||
params: mockParams, | ||
}); | ||
const mockResponse = httpServerMock.createResponseFactory(); | ||
|
||
registerSearchRoute(routerMock); | ||
const handler = routerMock.post.mock.calls[0][1]; | ||
await handler(mockContext, mockRequest, mockResponse); | ||
|
||
expect(mockSearch).toBeCalled(); | ||
expect(mockSearch.mock.calls[0][0]).toStrictEqual(mockBody); | ||
expect(mockSearch.mock.calls[0][1]).toBe(mockParams.strategy); | ||
expect(mockResponse.ok).toBeCalled(); | ||
expect(mockResponse.ok.mock.calls[0][0]).toEqual({ body: 'yay' }); | ||
}); | ||
|
||
it('handler throws internal error if the search throws an error', async () => { | ||
const mockSearch = jest.fn().mockRejectedValue('oh no'); | ||
const mockContext = { | ||
core: { | ||
elasticsearch: { | ||
dataClient: {} as ScopedClusterClient, | ||
adminClient: {} as ScopedClusterClient, | ||
}, | ||
}, | ||
search: { | ||
search: mockSearch, | ||
}, | ||
}; | ||
const mockBody = { params: {} }; | ||
const mockParams = { strategy: 'foo' }; | ||
const mockRequest = httpServerMock.createKibanaRequest({ | ||
body: mockBody, | ||
params: mockParams, | ||
}); | ||
const mockResponse = httpServerMock.createResponseFactory(); | ||
|
||
registerSearchRoute(routerMock); | ||
const handler = routerMock.post.mock.calls[0][1]; | ||
await handler(mockContext, mockRequest, mockResponse); | ||
|
||
expect(mockSearch).toBeCalled(); | ||
expect(mockSearch.mock.calls[0][0]).toStrictEqual(mockBody); | ||
expect(mockSearch.mock.calls[0][1]).toBe(mockParams.strategy); | ||
expect(mockResponse.internalError).toBeCalled(); | ||
expect(mockResponse.internalError.mock.calls[0][0]).toEqual({ body: 'oh no' }); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of this, but I didn't see a way to get a mock for this (I might not have been looking in the right places though).