-
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.
Fix some code smells.
- Loading branch information
Showing
39 changed files
with
514 additions
and
808 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
158 changes: 70 additions & 88 deletions
158
...aths/project/{projectId}/survey/{surveyId}/critters/{critterId}/deployments/index.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 |
---|---|---|
@@ -1,106 +1,88 @@ | ||
import Ajv from 'ajv'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { createDeployment, POST } from '.'; | ||
import { createDeployment } from '.'; | ||
import * as db from '../../../../../../../../database/db'; | ||
import { IBctwDeploymentRecord } from '../../../../../../../../models/bctw'; | ||
import { BctwDeploymentService } from '../../../../../../../../services/bctw-service/bctw-deployment-service'; | ||
import { CritterbaseService, ICapture } from '../../../../../../../../services/critterbase-service'; | ||
import { DeploymentService } from '../../../../../../../../services/deployment-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../../../../../__mocks__/db'; | ||
import { PATCH, updateDeployment } from './{deploymentId}'; | ||
|
||
describe('critter deployments', () => { | ||
describe('createDeployment', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
const mockDBConnection = getMockDBConnection({ release: sinon.stub() }); | ||
|
||
describe('openapi schema', () => { | ||
const ajv = new Ajv(); | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema(POST.apiDoc as unknown as object)).to.be.true; | ||
expect(ajv.validateSchema(PATCH.apiDoc as unknown as object)).to.be.true; | ||
}); | ||
it('creates a new deployment', async () => { | ||
const mockDBConnection = getMockDBConnection({ release: sinon.stub() }); | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
|
||
const mockCapture: ICapture = { | ||
capture_id: '111', | ||
critter_id: '222', | ||
capture_method_id: null, | ||
capture_location_id: '333', | ||
release_location_id: null, | ||
capture_date: '2021-01-01', | ||
capture_time: '12:00:00', | ||
release_date: null, | ||
release_time: null, | ||
capture_comment: null, | ||
release_comment: null | ||
}; | ||
|
||
const mockDeployment: IBctwDeploymentRecord = { | ||
assignment_id: '111', | ||
collar_id: '222', | ||
critter_id: '333', | ||
created_at: '2021-01-01', | ||
created_by_user_id: '444', | ||
updated_at: '2021-01-01', | ||
updated_by_user_id: '555', | ||
valid_from: '2021-01-01', | ||
valid_to: '2021-01-01', | ||
attachment_start: '2021-01-01', | ||
attachment_end: '2021-01-01', | ||
deployment_id: '666', | ||
device_id: 777 | ||
}; | ||
|
||
const insertDeploymentStub = sinon.stub(DeploymentService.prototype, 'insertDeployment').resolves(); | ||
const createDeploymentStub = sinon | ||
.stub(BctwDeploymentService.prototype, 'createDeployment') | ||
.resolves(mockDeployment); | ||
const getCaptureByIdStub = sinon.stub(CritterbaseService.prototype, 'getCaptureById').resolves(mockCapture); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const requestHandler = createDeployment(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetDBConnection).to.have.been.calledOnce; | ||
expect(insertDeploymentStub).to.have.been.calledOnce; | ||
expect(createDeploymentStub).to.have.been.calledOnce; | ||
expect(getCaptureByIdStub).to.have.been.calledOnce; | ||
expect(mockRes.status).to.have.been.calledWith(201); | ||
}); | ||
|
||
describe('updateDeployment', () => { | ||
it('updates an existing deployment', async () => { | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
const mockcreateDeployment = sinon.stub(DeploymentService.prototype, 'updateDeployment').resolves(); | ||
const mockBctwDeploymentService = sinon.stub(BctwDeploymentService.prototype, 'updateDeployment'); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const requestHandler = updateDeployment(); | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetDBConnection.calledOnce).to.be.true; | ||
expect(mockcreateDeployment.calledOnce).to.be.true; | ||
expect(mockBctwDeploymentService.calledOnce).to.be.true; | ||
expect(mockRes.status).to.have.been.calledWith(200); | ||
}); | ||
|
||
it('catches and re-throws errors', async () => { | ||
const mockError = new Error('a test error'); | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
const mockcreateDeployment = sinon.stub(DeploymentService.prototype, 'updateDeployment').rejects(mockError); | ||
const mockBctwDeploymentService = sinon | ||
.stub(BctwDeploymentService.prototype, 'updateDeployment') | ||
.rejects(mockError); | ||
it('catches and re-throws errors', async () => { | ||
const mockDBConnection = getMockDBConnection({ release: sinon.stub() }); | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const mockError = new Error('a test error'); | ||
const insertDeploymentStub = sinon.stub(DeploymentService.prototype, 'insertDeployment').rejects(mockError); | ||
|
||
const requestHandler = updateDeployment(); | ||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetDBConnection.calledOnce).to.be.true; | ||
expect(mockcreateDeployment.calledOnce).to.be.true; | ||
expect(mockBctwDeploymentService.notCalled).to.be.true; | ||
} | ||
}); | ||
describe('createDeployment', () => { | ||
it('deploys a new telemetry device', async () => { | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
const mockcreateDeployment = sinon.stub(DeploymentService.prototype, 'updateDeployment').resolves(); | ||
const mockBctwDeploymentService = sinon.stub(BctwDeploymentService.prototype, 'createDeployment'); | ||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const requestHandler = createDeployment(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetDBConnection.calledOnce).to.be.true; | ||
expect(mockcreateDeployment.calledOnce).to.be.true; | ||
expect(mockBctwDeploymentService.calledOnce).to.be.true; | ||
expect(mockRes.status).to.have.been.calledWith(201); | ||
}); | ||
|
||
it('catches and re-throws errors', async () => { | ||
const mockError = new Error('a test error'); | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
const mockcreateDeployment = sinon.stub(DeploymentService.prototype, 'updateDeployment').rejects(mockError); | ||
const mockBctwDeploymentService = sinon | ||
.stub(BctwDeploymentService.prototype, 'createDeployment') | ||
.rejects(mockError); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const requestHandler = createDeployment(); | ||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetDBConnection.calledOnce).to.be.true; | ||
expect(mockcreateDeployment.calledOnce).to.be.true; | ||
expect(mockBctwDeploymentService.notCalled).to.be.true; | ||
} | ||
}); | ||
}); | ||
const requestHandler = createDeployment(); | ||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetDBConnection).to.have.been.calledOnce; | ||
expect(insertDeploymentStub).to.have.been.calledOnce; | ||
} | ||
}); | ||
}); |
71 changes: 49 additions & 22 deletions
71
...rojectId}/survey/{surveyId}/critters/{critterId}/deployments/{deploymentId}/index.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 |
---|---|---|
@@ -1,42 +1,69 @@ | ||
import Ajv from 'ajv'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { updateDeployment } from '.'; | ||
import * as db from '../../../../../../../../../database/db'; | ||
import { BctwDeploymentService } from '../../../../../../../../../services/bctw-service/bctw-deployment-service'; | ||
import { CritterbaseService, ICapture } from '../../../../../../../../../services/critterbase-service'; | ||
import { DeploymentService } from '../../../../../../../../../services/deployment-service'; | ||
import { getMockDBConnection, getRequestHandlerMocks } from '../../../../../../../../../__mocks__/db'; | ||
import { DELETE, deleteDeployment } from '../{bctwDeploymentId}'; | ||
|
||
describe('critter deployments', () => { | ||
describe('updateDeployment', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
const mockDBConnection = getMockDBConnection({ release: sinon.stub() }); | ||
it('updates an existing deployment', async () => { | ||
const mockDBConnection = getMockDBConnection({ release: sinon.stub() }); | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
|
||
describe('openapi schema', () => { | ||
const ajv = new Ajv(); | ||
const mockCapture: ICapture = { | ||
capture_id: '111', | ||
critter_id: '222', | ||
capture_method_id: null, | ||
capture_location_id: '333', | ||
release_location_id: null, | ||
capture_date: '2021-01-01', | ||
capture_time: '12:00:00', | ||
release_date: null, | ||
release_time: null, | ||
capture_comment: null, | ||
release_comment: null | ||
}; | ||
|
||
it('is valid openapi v3 schema', () => { | ||
expect(ajv.validateSchema(DELETE.apiDoc as unknown as object)).to.be.true; | ||
}); | ||
const updateDeploymentStub = sinon.stub(DeploymentService.prototype, 'updateDeployment').resolves(); | ||
const updateBctwDeploymentStub = sinon.stub(BctwDeploymentService.prototype, 'updateDeployment'); | ||
const getCaptureByIdStub = sinon.stub(CritterbaseService.prototype, 'getCaptureById').resolves(mockCapture); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
const requestHandler = updateDeployment(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetDBConnection).to.have.been.calledOnce; | ||
expect(updateDeploymentStub).to.have.been.calledOnce; | ||
expect(updateBctwDeploymentStub).to.have.been.calledOnce; | ||
expect(getCaptureByIdStub).to.have.been.calledOnce; | ||
expect(mockRes.status).to.have.been.calledWith(200); | ||
}); | ||
|
||
describe('deleteDeployment', () => { | ||
it('deletes an existing deployment', async () => { | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
const mockRemoveDeployment = sinon.stub(DeploymentService.prototype, 'endDeployment').resolves(); | ||
const mockBctwService = sinon.stub(BctwDeploymentService.prototype, 'deleteDeployment'); | ||
it('catches and re-throws errors', async () => { | ||
const mockDBConnection = getMockDBConnection({ release: sinon.stub() }); | ||
const mockGetDBConnection = sinon.stub(db, 'getDBConnection').returns(mockDBConnection); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const mockError = new Error('a test error'); | ||
const updateDeploymentStub = sinon.stub(DeploymentService.prototype, 'updateDeployment').rejects(mockError); | ||
|
||
const requestHandler = deleteDeployment(); | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
|
||
expect(mockGetDBConnection.calledOnce).to.be.true; | ||
expect(mockRemoveDeployment.calledOnce).to.be.true; | ||
expect(mockBctwService.calledOnce).to.be.true; | ||
expect(mockRes.status).to.have.been.calledWith(200); | ||
}); | ||
const requestHandler = updateDeployment(); | ||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetDBConnection).to.have.been.calledOnce; | ||
expect(updateDeploymentStub).to.have.been.calledOnce; | ||
} | ||
}); | ||
}); |
Oops, something went wrong.