-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
432 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"git.ignoreLimitWarning": true | ||
} | ||
"git.ignoreLimitWarning": true, | ||
"cSpell.words": [ | ||
"Itis" | ||
] | ||
} |
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,93 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import qs from 'qs'; | ||
import { TaxonomyService } from '../../../services/taxonomy-service'; | ||
import { getLogger } from '../../../utils/logger'; | ||
|
||
const defaultLog = getLogger('paths/taxonomy/itis/list'); | ||
|
||
export const GET: Operation = [getSpeciesFromIds()]; | ||
|
||
GET.apiDoc = { | ||
description: 'Gets the labels of the taxonomic units identified by the provided list of ids.', | ||
tags: ['taxonomy'], | ||
parameters: [ | ||
{ | ||
description: 'Taxonomy ids.', | ||
in: 'query', | ||
name: 'ids', | ||
required: true, | ||
schema: { | ||
type: 'string' | ||
} | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'Taxonomy search response object.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
searchResponse: { | ||
type: 'array', | ||
items: { | ||
title: 'Species', | ||
type: 'object', | ||
required: ['id', 'label'], | ||
properties: { | ||
id: { | ||
type: 'string' | ||
}, | ||
label: { | ||
type: 'string' | ||
}, | ||
scientificName: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
400: { | ||
$ref: '#/components/responses/400' | ||
}, | ||
500: { | ||
$ref: '#/components/responses/500' | ||
}, | ||
default: { | ||
$ref: '#/components/responses/default' | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Get taxonomic search results. | ||
* | ||
* @returns {RequestHandler} | ||
*/ | ||
export function getSpeciesFromIds(): RequestHandler { | ||
return async (req, res) => { | ||
defaultLog.debug({ label: 'getSearchResults', message: 'request body', req_body: req.query }); | ||
|
||
const ids = Object.values(qs.parse(req.query.ids?.toString() || '')); | ||
|
||
try { | ||
const taxonomyService = new TaxonomyService(); | ||
const response = await taxonomyService.itisTsnSearch(ids as string[]); | ||
|
||
// Overwrite default cache-control header, allow caching up to 7 days | ||
res.setHeader('Cache-Control', 'max-age=604800'); | ||
|
||
res.status(200).json({ searchResponse: response }); | ||
} catch (error) { | ||
defaultLog.error({ label: 'getSearchResults', message: 'error', error }); | ||
throw error; | ||
} | ||
}; | ||
} |
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,97 @@ | ||
import Ajv from 'ajv'; | ||
import chai, { expect } from 'chai'; | ||
import { describe } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import * as db from '../../../database/db'; | ||
import { HTTPError } from '../../../errors/http-error'; | ||
import { TaxonomyService } from '../../../services/taxonomy-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
import { GET, searchSpecies } from './search'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('search', () => { | ||
describe('openapi schema', () => { | ||
const ajv = new Ajv(); | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema(GET.apiDoc as unknown as object)).to.be.true; | ||
}); | ||
}); | ||
|
||
describe('searchSpecies', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('returns an empty array if no species are found', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const getSpeciesFromIdsStub = sinon.stub(TaxonomyService.prototype, 'itisTermSearch').resolves([]); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { | ||
terms: '' | ||
}; | ||
|
||
const requestHandler = searchSpecies(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getSpeciesFromIdsStub).to.have.been.calledWith(''); | ||
|
||
expect(mockRes.statusValue).to.equal(200); | ||
expect(mockRes.jsonValue).to.eql({ searchResponse: [] }); | ||
}); | ||
|
||
it('returns an array of species', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const mock1 = { id: '1', label: 'something', scientificName: 'string' } as unknown as any; | ||
const mock2 = { id: '2', label: 'anything', scientificName: 'string' } as unknown as any; | ||
|
||
const getSpeciesFromIdsStub = sinon.stub(TaxonomyService.prototype, 'itisTermSearch').resolves([mock1, mock2]); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { | ||
terms: 't' | ||
}; | ||
|
||
const requestHandler = searchSpecies(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getSpeciesFromIdsStub).to.have.been.calledWith('t'); | ||
|
||
expect(mockRes.jsonValue).to.eql({ searchResponse: [mock1, mock2] }); | ||
expect(mockRes.statusValue).to.equal(200); | ||
}); | ||
|
||
it('catches error, and re-throws error', async () => { | ||
const dbConnectionObj = getMockDBConnection({ rollback: sinon.stub(), release: sinon.stub() }); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
sinon.stub(TaxonomyService.prototype, 'itisTermSearch').rejects(new Error('a test error')); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { | ||
ids: 'a' | ||
}; | ||
|
||
try { | ||
const requestHandler = searchSpecies(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect((actualError as HTTPError).message).to.equal('a test error'); | ||
} | ||
}); | ||
}); | ||
}); |
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,93 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { TaxonomyService } from '../../../services/taxonomy-service'; | ||
import { getLogger } from '../../../utils/logger'; | ||
|
||
const defaultLog = getLogger('paths/taxonomy/itis/search'); | ||
|
||
export const GET: Operation = [searchSpecies()]; | ||
|
||
GET.apiDoc = { | ||
description: 'Gets a list of taxonomic units.', | ||
tags: ['taxonomy'], | ||
parameters: [ | ||
{ | ||
description: 'Taxonomy search parameters.', | ||
in: 'query', | ||
name: 'terms', | ||
required: true, | ||
schema: { | ||
type: 'string' | ||
} | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'Taxonomy search response object.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
searchResponse: { | ||
type: 'array', | ||
items: { | ||
title: 'Species', | ||
type: 'object', | ||
required: ['id', 'label'], | ||
properties: { | ||
id: { | ||
type: 'string' | ||
}, | ||
label: { | ||
type: 'string' | ||
}, | ||
scientificName: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
400: { | ||
$ref: '#/components/responses/400' | ||
}, | ||
500: { | ||
$ref: '#/components/responses/500' | ||
}, | ||
default: { | ||
$ref: '#/components/responses/default' | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Get taxonomic search results from itis. | ||
* | ||
* @returns {RequestHandler} | ||
*/ | ||
export function searchSpecies(): RequestHandler { | ||
return async (req, res) => { | ||
defaultLog.debug({ label: 'getSearchResults', message: 'request params', req_params: req.query.terms }); | ||
|
||
const term = String(req.query.terms) || ''; | ||
|
||
try { | ||
const taxonomyService = new TaxonomyService(); | ||
|
||
const response = await taxonomyService.itisTermSearch(term.toLowerCase()); | ||
|
||
// Overwrite default cache-control header, allow caching up to 7 days | ||
res.setHeader('Cache-Control', 'max-age=604800'); | ||
|
||
res.status(200).json({ searchResponse: response }); | ||
} catch (error) { | ||
defaultLog.error({ label: 'getSearchResults', message: 'error', error }); | ||
throw error; | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.