-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for critters and lookups paths
- Loading branch information
1 parent
118d022
commit 6660d97
Showing
6 changed files
with
210 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,62 @@ | ||
import Ajv from 'ajv'; | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { CritterbaseService, IBulkCreate } from '../../../services/critterbase-service'; | ||
import { getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
import * as createCritter from './post'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('paths/critter-data/critters/post', () => { | ||
const ajv = new Ajv(); | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema((createCritter.POST.apiDoc as unknown) as object)).to.be.true; | ||
}); | ||
|
||
const payload: IBulkCreate = { | ||
critters: [], | ||
captures: [], | ||
mortality: [], | ||
locations: [], | ||
markings: [], | ||
measurement_quantitative: [], | ||
measurement_qualitative: [], | ||
family: [] | ||
}; | ||
|
||
describe('createCritter', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
it('should succeed', async () => { | ||
const mockCreateCritter = sinon.stub(CritterbaseService.prototype, 'createCritter').resolves({ count: 0 }); | ||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.body = payload; | ||
const requestHandler = createCritter.createCritter(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockCreateCritter.calledOnce).to.be.true; | ||
expect(mockCreateCritter).calledWith(payload); | ||
expect(mockRes.statusValue).to.equal(200); | ||
expect(mockRes.json.calledWith({ count: 0 })).to.be.true; | ||
}); | ||
it('should fail', async () => { | ||
const mockError = new Error('mock error'); | ||
const mockCreateCritter = sinon.stub(CritterbaseService.prototype, 'createCritter').rejects(mockError); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const requestHandler = createCritter.createCritter(); | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockCreateCritter.calledOnce).to.be.true; | ||
} | ||
}); | ||
}); | ||
}); |
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,68 @@ | ||
import Ajv from 'ajv'; | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { CritterbaseService } from '../../../services/critterbase-service'; | ||
import { getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
import * as critter from './{critterId}'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('paths/critter-data/critters/{critterId}', () => { | ||
const ajv = new Ajv(); | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema((critter.GET.apiDoc as unknown) as object)).to.be.true; | ||
}); | ||
|
||
const mockCritter = { | ||
critter_id: 'asdf', | ||
wlh_id: '17-10748', | ||
animal_id: '6', | ||
sex: 'Female', | ||
taxon: 'Caribou', | ||
collection_units: [ | ||
{ | ||
category_name: 'Population Unit', | ||
unit_name: 'Itcha-Ilgachuz', | ||
collection_unit_id: '0284c4ca-a279-4135-b6ef-d8f4f8c3d1e6', | ||
collection_category_id: '9dcf05a8-9bfe-421b-b487-ce65299441ca' | ||
} | ||
], | ||
mortality_timestamp: new Date() | ||
}; | ||
|
||
describe('getCritter', async () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
it('should succeed', async () => { | ||
const mockGetCritter = sinon.stub(CritterbaseService.prototype, 'getCritter').resolves(mockCritter); | ||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
mockReq.params = { critterId: 'asdf' }; | ||
const requestHandler = critter.getCritter(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetCritter.calledOnce).to.be.true; | ||
expect(mockGetCritter).calledWith('asdf'); | ||
expect(mockRes.statusValue).to.equal(200); | ||
expect(mockRes.json.calledWith(mockCritter)).to.be.true; | ||
}); | ||
it('should fail', async () => { | ||
const mockError = new Error('mock error'); | ||
const mockGetCritter = sinon.stub(CritterbaseService.prototype, 'getCritter').rejects(mockError); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const requestHandler = critter.getCritter(); | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetCritter.calledOnce).to.be.true; | ||
} | ||
}); | ||
}); | ||
}); |
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,53 @@ | ||
import Ajv from 'ajv'; | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { CritterbaseService } from '../../../services/critterbase-service'; | ||
import { getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
import * as key from './{key}'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('paths/critter-data/lookups/{key}', () => { | ||
const ajv = new Ajv(); | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema((key.GET.apiDoc as unknown) as object)).to.be.true; | ||
}); | ||
|
||
const mockSelectOptions = [{ key: 'a', value: 'a', id: 'a' }]; | ||
|
||
describe('getSelectOptions', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
it('should succeed', async () => { | ||
const mockGetLookupValues = sinon | ||
.stub(CritterbaseService.prototype, 'getLookupValues') | ||
.resolves(mockSelectOptions); | ||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const requestHandler = key.getLookupValues(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetLookupValues.calledOnce).to.be.true; | ||
expect(mockRes.statusValue).to.equal(200); | ||
expect(mockRes.json.calledWith(mockSelectOptions)).to.be.true; | ||
}); | ||
it('should fail', async () => { | ||
const mockError = new Error('mock error'); | ||
const mockGetLookupValues = sinon.stub(CritterbaseService.prototype, 'getLookupValues').rejects(mockError); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const requestHandler = key.getLookupValues(); | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetLookupValues.calledOnce).to.be.true; | ||
} | ||
}); | ||
}); | ||
}); |
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