Skip to content

Commit

Permalink
feat: added tests for manual telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
MacQSL committed Oct 21, 2024
1 parent 81866c5 commit b23edf3
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
117 changes: 116 additions & 1 deletion api/src/services/telemetry-services/telemetry-vendor-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { TelemetryManualRepository } from '../../repositories/telemetry-repositories/telemetry-manual-repository';
import { TelemetryVendorRepository } from '../../repositories/telemetry-repositories/telemetry-vendor-repository';
import { getMockDBConnection } from '../../__mocks__/db';
import { TelemetryVendorService } from './telemetry-vendor-service';

chai.use(sinonChai);

describe('TelemetryVendorService', () => {
describe.only('TelemetryVendorService', () => {
beforeEach(() => {
sinon.restore();
});
Expand Down Expand Up @@ -48,4 +49,118 @@ describe('TelemetryVendorService', () => {
expect(data).to.deep.equal([]);
});
});

describe('_validateSurveyDeploymentIds', () => {
it('should throw an error if a deployment id does not exist for survey', async () => {
const mockDBConnection = getMockDBConnection();
const service = new TelemetryVendorService(mockDBConnection);
const deploymentServiceStub = sinon.stub(service.deploymentService, 'getDeploymentsForSurveyId').resolves([]);

try {
await service._validateSurveyDeploymentIds(1, [1]);
expect.fail();
} catch (err: any) {
expect(deploymentServiceStub).to.have.been.calledWith(1);
expect(err.message).to.equal('Invalid deployment ID provided for survey');
}
});

it('should not throw error if validation success', async () => {
const mockDBConnection = getMockDBConnection();
const service = new TelemetryVendorService(mockDBConnection);
const deploymentServiceStub = sinon
.stub(service.deploymentService, 'getDeploymentsForSurveyId')
.resolves([{ deployment2_id: 1 } as any]);

try {
await service._validateSurveyDeploymentIds(1, [1]);
expect(deploymentServiceStub).to.have.been.calledWith(1);
} catch (err: any) {
expect.fail();
}
});
});

describe('bulkCreateManualTelemetry', () => {
it('should create manual telemetry records', async () => {
const mockDBConnection = getMockDBConnection();
const service = new TelemetryVendorService(mockDBConnection);

const repoStub = sinon.stub(TelemetryManualRepository.prototype, 'bulkCreateManualTelemetry');
const validateStub = sinon.stub(service, '_validateSurveyDeploymentIds');

await service.bulkCreateManualTelemetry(1, [
{
deployment2_id: 1,
latitude: 1,
longitude: 1,
acquisition_date: '2021-01-01',
transmission_date: '2021-01-01'
}
]);

expect(validateStub).to.have.been.calledWith(1, [1]);
expect(repoStub).to.have.been.calledWith([
{
deployment2_id: 1,
latitude: 1,
longitude: 1,
acquisition_date: '2021-01-01',
transmission_date: '2021-01-01'
}
]);
});
});

describe('bulkUpdateManualTelemetry', () => {
it('should update manual telemetry records', async () => {
const mockDBConnection = getMockDBConnection();
const service = new TelemetryVendorService(mockDBConnection);

const repoStub = sinon.stub(TelemetryManualRepository.prototype, 'bulkUpdateManualTelemetry');
const validateStub = sinon.stub(service, '_validateSurveyDeploymentIds');

await service.bulkUpdateManualTelemetry(1, [
{
telemetry_manual_id: '09556e24-153b-4dbb-add6-f00e74131e48',
deployment2_id: 1,
latitude: 1,
longitude: 1,
acquisition_date: '2021-01-01',
transmission_date: '2021-01-01'
}
]);

expect(validateStub).to.have.been.calledWith(1, [1]);
expect(repoStub).to.have.been.calledWith([
{
telemetry_manual_id: '09556e24-153b-4dbb-add6-f00e74131e48',
latitude: 1,
longitude: 1,
acquisition_date: '2021-01-01',
transmission_date: '2021-01-01'
}
]);
});
});

describe('bulkDeleteManualTelemetry', () => {
it('should update manual telemetry records', async () => {
const mockDBConnection = getMockDBConnection();
const service = new TelemetryVendorService(mockDBConnection);

const repoStub = sinon.stub(TelemetryManualRepository.prototype, 'bulkDeleteManualTelemetry');
const validateStub = sinon.stub(service, '_validateSurveyDeploymentIds');

await service.bulkDeleteManualTelemetry(1, [
{
telemetry_manual_id: '09556e24-153b-4dbb-add6-f00e74131e48',
deployment2_id: 1
}
]);

expect(validateStub).to.have.been.calledWith(1, [1]);
expect(repoStub).to.have.been.calledWith(['09556e24-153b-4dbb-add6-f00e74131e48']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export class TelemetryVendorService extends DBService {
/**
* Update manual telemetry records.
*
* Note: Removes the `deployent2_id` from the records before updating.
*
* @async
* @param {number} surveyId
* @param {TelemetryManualRecord[]} telemetry - List of manual telemetry data to update
Expand All @@ -132,7 +134,7 @@ export class TelemetryVendorService extends DBService {
// Validate the deployment IDs exist in the survey
await this._validateSurveyDeploymentIds(surveyId, deploymentIds);

return this.manualRepository.bulkUpdateManualTelemetry(telemetry);
return this.manualRepository.bulkUpdateManualTelemetry(updateTelemetry);
}

/**
Expand Down

0 comments on commit b23edf3

Please sign in to comment.