Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
al-rosenthal committed Jan 13, 2024
1 parent 88c6aa7 commit 462ce6d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
6 changes: 6 additions & 0 deletions api/src/paths/submission/intake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sinonChai from 'sinon-chai';
import * as db from '../../database/db';
import { HTTPError } from '../../errors/http-error';
import { SystemUser } from '../../repositories/user-repository';
import { RegionService } from '../../services/region-service';
import { SearchIndexService } from '../../services/search-index-service';
import { SubmissionService } from '../../services/submission-service';
import { ValidationService } from '../../services/validation-service';
Expand Down Expand Up @@ -101,6 +102,10 @@ describe('intake', () => {
.stub(SearchIndexService.prototype, 'indexFeaturesBySubmissionId')
.resolves();

const calculateAndAddRegionsForSubmissionStub = sinon
.stub(RegionService.prototype, 'calculateAndAddRegionsForSubmission')
.resolves();

const requestHandler = intake.submissionIntake();

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
Expand All @@ -118,6 +123,7 @@ describe('intake', () => {
expect(insertSubmissionRecordWithPotentialConflictStub).to.have.been.calledOnce;
expect(insertSubmissionFeatureRecordsStub).to.have.been.calledOnce;
expect(indexFeaturesBySubmissionIdStub).to.have.been.calledOnce;
expect(calculateAndAddRegionsForSubmissionStub).to.have.been.calledOnce;
expect(mockRes.statusValue).to.eql(200);
expect(mockRes.jsonValue).to.eql({ submission_id: 1 });
});
Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/submission/intake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function submissionIntake(): RequestHandler {
await searchIndexService.indexFeaturesBySubmissionId(response.submission_id);

// Calculate and add submission regions
await regionService.calculateRegionsForSubmission(response.submission_id);
await regionService.calculateAndAddRegionsForSubmission(response.submission_id);

await connection.commit();

Expand Down
6 changes: 3 additions & 3 deletions api/src/repositories/region-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export class RegionRepository extends BaseRepository {
* Any regions intersecting with this calculated value are returned.
*
* @param {number} submissionId
* @param {number} [regionAccuracy=1] regionAccuracy Expected 0-1. Determines the percentage of rows to use
* @param {number} [rowPercentage=1] rowPercentage Expected 0-1. Determines the percentage of rows to use
* @param {number} [intersectThreshold=1] intersectThreshold Expected 0-1. Determines the percentage threshold for intersections to be valid
* @returns {*} {Promise<{region_id: number}}[]>} An array of found region ids
* @memberof RegionRepository
*/
async calculateRegionsForASubmission(
submissionId: number,
regionAccuracy: number = 1,
rowPercentage: number = 1,

Check failure on line 60 in api/src/repositories/region-repository.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Type number trivially inferred from a number literal, remove type annotation
intersectThreshold: number = 1

Check failure on line 61 in api/src/repositories/region-repository.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Type number trivially inferred from a number literal, remove type annotation
): Promise<{ region_id: number }[]> {
const sql = SQL`
Expand All @@ -67,7 +67,7 @@ export class RegionRepository extends BaseRepository {
FROM search_spatial
ORDER BY RANDOM()
LIMIT (
SELECT CEIL(${regionAccuracy} * COUNT(*))
SELECT CEIL(${rowPercentage} * COUNT(*))
FROM search_spatial ss, submission_feature sf
WHERE ss.submission_feature_id = sf.submission_feature_id
AND sf.submission_id = ${submissionId}
Expand Down
47 changes: 47 additions & 0 deletions api/src/services/region-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { RegionRepository } from '../repositories/region-repository';
import { getMockDBConnection } from '../__mocks__/db';
import { RegionService } from './region-service';

chai.use(sinonChai);

describe('RegionService', () => {
describe('calculateAndAddRegionsForSubmission', () => {
afterEach(() => {
sinon.restore();
});

it('should succeed with valid data', async () => {
const mockDBConnection = getMockDBConnection();
const service = new RegionService(mockDBConnection);

const calculate = sinon
.stub(RegionRepository.prototype, 'calculateRegionsForASubmission')
.resolves([{ region_id: 1 }]);
const insert = sinon.stub(RegionRepository.prototype, 'insertSubmissionRegions').resolves();

await service.calculateAndAddRegionsForSubmission(1);

expect(calculate).to.be.called;
expect(insert).to.be.called;
});

it('should succeed with modified parameters', async () => {
const mockDBConnection = getMockDBConnection();
const service = new RegionService(mockDBConnection);

const calculate = sinon
.stub(RegionRepository.prototype, 'calculateRegionsForASubmission')
.resolves([{ region_id: 1 }]);
const insert = sinon.stub(RegionRepository.prototype, 'insertSubmissionRegions').resolves();

await service.calculateAndAddRegionsForSubmission(1, 0.5, 0.5);

expect(calculate).to.be.calledWith(1, 0.5, 0.5);
expect(insert).to.be.called;
});
});
});
12 changes: 10 additions & 2 deletions api/src/services/region-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ export class RegionService extends DBService {
*
* @param submissionId
*/
async calculateRegionsForSubmission(submissionId: number): Promise<void> {
const regionIds = await this.regionRepository.calculateRegionsForASubmission(submissionId);
async calculateAndAddRegionsForSubmission(
submissionId: number,
rowPercentage: number = 1,

Check failure on line 21 in api/src/services/region-service.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Type number trivially inferred from a number literal, remove type annotation
intersectThreshold: number = 1

Check failure on line 22 in api/src/services/region-service.ts

View workflow job for this annotation

GitHub Actions / Running Linter and Formatter

Type number trivially inferred from a number literal, remove type annotation
): Promise<void> {
const regionIds = await this.regionRepository.calculateRegionsForASubmission(
submissionId,
rowPercentage,
intersectThreshold
);
await this.regionRepository.insertSubmissionRegions(submissionId, regionIds);
}
}

0 comments on commit 462ce6d

Please sign in to comment.