-
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.
Endpoint for retrieving hierarchy information for multiple TSNs (#250)
* Add taxon hierarchy endpoint
- Loading branch information
1 parent
75f63fd
commit 3fb2a87
Showing
4 changed files
with
410 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Ajv from 'ajv'; | ||
import chai, { expect } from 'chai'; | ||
import { describe } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { GET } from '.'; | ||
import * as db from '../../../../database/db'; | ||
import { ItisService } from '../../../../services/itis-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../__mocks__/db'; | ||
import { getHierarchyForTSNs } from './hierarchy'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('taxonomy/taxon/tsn/hierarchy', () => { | ||
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('getHierarchyForTSNs', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('returns an empty array if no species are found', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const getHierarchyForTSNsStub = sinon.stub(ItisService.prototype, 'getHierarchyForTSNs').resolves([]); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { | ||
tsn: ['1', '2'] | ||
}; | ||
|
||
const requestHandler = getHierarchyForTSNs(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getHierarchyForTSNsStub).to.have.been.calledWith([1, 2]); | ||
|
||
expect(mockRes.statusValue).to.equal(200); | ||
expect(mockRes.jsonValue).to.eql([]); | ||
}); | ||
|
||
it('returns an array of species', async () => { | ||
const dbConnectionObj = getMockDBConnection(); | ||
|
||
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); | ||
|
||
const mock1 = { | ||
tsn: 1, | ||
commonNames: ['something'], | ||
scientificName: 'string', | ||
hierarchy: [3, 2, 1] | ||
} as unknown as any; | ||
const mock2 = { tsn: '2', commonNames: [], scientificName: 'string', hierarchy: [3, 2] } as unknown as any; | ||
|
||
const getHierarchyForTSNsStub = sinon.stub(ItisService.prototype, 'getHierarchyForTSNs').resolves([mock1, mock2]); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { | ||
tsn: ['1', '2'] | ||
}; | ||
|
||
const requestHandler = getHierarchyForTSNs(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(getHierarchyForTSNsStub).to.have.been.calledWith([1, 2]); | ||
|
||
expect(mockRes.jsonValue).to.eql([mock1, mock2]); | ||
expect(mockRes.statusValue).to.equal(200); | ||
}); | ||
}); | ||
}); |
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,108 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { getAPIUserDBConnection } from '../../../../database/db'; | ||
import { ItisService } from '../../../../services/itis-service'; | ||
import { getLogger } from '../../../../utils/logger'; | ||
|
||
const defaultLog = getLogger('paths/taxonomy/taxon/tsn/hierarchy'); | ||
|
||
export const GET: Operation = [getHierarchyForTSNs()]; | ||
|
||
GET.apiDoc = { | ||
description: 'Get taxon hierarchy information by TSN ids.', | ||
tags: ['taxon_id'], | ||
security: [], | ||
parameters: [ | ||
{ | ||
description: 'Taxon TSN ids.', | ||
in: 'query', | ||
name: 'tsn', | ||
schema: { | ||
type: 'array', | ||
description: 'One or more Taxon TSN ids.', | ||
items: { | ||
type: 'integer', | ||
minimum: 0, | ||
minItems: 1, | ||
maxItems: 100 | ||
} | ||
}, | ||
required: true | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'Taxonomy response.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: { | ||
title: 'Species', | ||
type: 'object', | ||
description: 'Taxon hierarchy response object with an array of parent TSNs', | ||
required: ['tsn', 'hierarchy'], | ||
properties: { | ||
tsn: { | ||
type: 'integer' | ||
}, | ||
hierarchy: { | ||
type: 'array', | ||
description: | ||
'Array of parent TSNs in descending order, where the highest-ranking parent is first and the TSN for which the hierarchy was requested is last.', | ||
items: { type: 'integer' } | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
400: { | ||
$ref: '#/components/responses/400' | ||
}, | ||
401: { | ||
$ref: '#/components/responses/401' | ||
}, | ||
500: { | ||
$ref: '#/components/responses/500' | ||
}, | ||
default: { | ||
$ref: '#/components/responses/default' | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Get taxon by ITIS TSN. | ||
* | ||
* @returns {RequestHandler} | ||
*/ | ||
export function getHierarchyForTSNs(): RequestHandler { | ||
return async (req, res) => { | ||
defaultLog.debug({ label: 'getHierarchyForTSNs', message: 'query params', query: req.query }); | ||
|
||
const connection = getAPIUserDBConnection(); | ||
|
||
const tsnIds: number[] = (req.query.tsn as string[]).map(Number); | ||
|
||
try { | ||
await connection.open(); | ||
|
||
const itisService = new ItisService(); | ||
|
||
const response = await itisService.getHierarchyForTSNs(tsnIds); | ||
|
||
connection.commit(); | ||
|
||
res.status(200).json(response); | ||
} catch (error) { | ||
defaultLog.error({ label: 'getHierarchyForTSNs', message: 'error', error }); | ||
connection.rollback(); | ||
throw error; | ||
} finally { | ||
connection.release(); | ||
} | ||
}; | ||
} |
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.