-
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
1 parent
9183875
commit be77b0b
Showing
4 changed files
with
241 additions
and
8 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,103 @@ | ||
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 { HTTPError } from '../../../../errors/http-error'; | ||
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); | ||
}); | ||
|
||
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(ItisService.prototype, 'getHierarchyForTSNs').rejects(new Error('a test error')); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.query = { | ||
tsn: ['1', '2'] | ||
}; | ||
|
||
try { | ||
const requestHandler = getHierarchyForTSNs(); | ||
|
||
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
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