Skip to content

Commit

Permalink
Misc updates/tweaks.
Browse files Browse the repository at this point in the history
Fix some code smells.
  • Loading branch information
NickPhura committed Aug 15, 2024
1 parent 9cf24be commit 3b17023
Show file tree
Hide file tree
Showing 39 changed files with 514 additions and 808 deletions.
19 changes: 0 additions & 19 deletions api/src/constants/bctw-routes.ts

This file was deleted.

2 changes: 1 addition & 1 deletion api/src/paths/gcnotify/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ POST.apiDoc = {
export function sendNotification(): RequestHandler {
return async (req, res) => {
const recipient = req.body?.recipient || null;
const message = { ...req.body?.message, footer: `To access the site, [${APP_HOST}](${APP_HOST})` } || null;
const message = { ...req.body?.message, footer: `To access the site, [${APP_HOST}](${APP_HOST})` };

try {
const gcnotifyService = new GCNotifyService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('importCsv', () => {

expect(mockFileScan).to.have.been.calledOnceWithExactly(mockFile);

expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockGetDBConnection).to.have.been.calledOnce;

expect(mockImportCSV).to.have.been.calledOnce;

Expand Down Expand Up @@ -78,7 +78,7 @@ describe('importCsv', () => {
expect(mockDBConnection.open).to.have.been.calledOnce;
expect(mockFileScan).to.have.been.calledOnceWithExactly(mockFile);

expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockRes.json).to.not.have.been.called;

expect(mockDBConnection.rollback).to.have.been.called;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ describe('getCrittersFromSurvey', () => {

await requestHandler(mockReq, mockRes, mockNext);

expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockGetCrittersInSurvey.calledOnce).to.be.true;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockGetCrittersInSurvey).to.have.been.calledOnce;
expect(mockGetMultipleCrittersByIds).to.be.calledOnceWith([mockSurveyCritter.critterbase_critter_id]);
expect(mockRes.json).to.have.been.calledWith([{ ...mockCBCritter, critter_id: mockSurveyCritter.critter_id }]);
});
Expand All @@ -56,8 +56,8 @@ describe('getCrittersFromSurvey', () => {
const requestHandler = getCrittersFromSurvey();
await requestHandler(mockReq, mockRes, mockNext);

expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockGetCrittersInSurvey.calledOnce).to.be.true;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockGetCrittersInSurvey).to.have.been.calledOnce;
expect(mockRes.json).to.have.been.calledWith([]);
});

Expand All @@ -79,8 +79,8 @@ describe('getCrittersFromSurvey', () => {
expect.fail();
} catch (actualError) {
expect(actualError).to.equal(mockError);
expect(mockGetCrittersInSurvey.calledOnce).to.be.true;
expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockGetCrittersInSurvey).to.have.been.calledOnce;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockDBConnection.release).to.have.been.called;
}
});
Expand Down Expand Up @@ -110,9 +110,9 @@ describe('addCritterToSurvey', () => {

await requestHandler(mockReq, mockRes, mockNext);

expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockAddCritterToSurvey.calledOnce).to.be.true;
expect(mockCreateCritter.notCalled).to.be.true;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockAddCritterToSurvey).to.have.been.calledOnce;
expect(mockCreateCritter).not.to.have.been.called;
expect(mockRes.status).to.have.been.calledWith(201);
expect(mockRes.json).to.have.been.calledWith(mockSurveyCritter);
});
Expand All @@ -135,9 +135,9 @@ describe('addCritterToSurvey', () => {

await requestHandler(mockReq, mockRes, mockNext);

expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockAddCritterToSurvey.calledOnce).to.be.true;
expect(mockCreateCritter.calledOnce).to.be.true;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockAddCritterToSurvey).to.have.been.calledOnce;
expect(mockCreateCritter).to.have.been.calledOnce;
expect(mockRes.status).to.have.been.calledWith(201);
expect(mockRes.json).to.have.been.calledWith(mockSurveyCritter);
});
Expand All @@ -161,9 +161,9 @@ describe('addCritterToSurvey', () => {
expect.fail();
} catch (actualError) {
expect(actualError).to.equal(mockError);
expect(mockAddCritterToSurvey.calledOnce).to.be.true;
expect(mockCreateCritter.calledOnce).to.be.true;
expect(mockGetDBConnection.calledOnce).to.be.true;
expect(mockAddCritterToSurvey).to.have.been.calledOnce;
expect(mockCreateCritter).to.have.been.calledOnce;
expect(mockGetDBConnection).to.have.been.calledOnce;
expect(mockDBConnection.release).to.have.been.called;
}
});
Expand Down
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;
}
});
});
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;
}
});
});
Loading

0 comments on commit 3b17023

Please sign in to comment.