From 462ce6d1c7dc13c0c0457328192503bafebd989a Mon Sep 17 00:00:00 2001 From: Alfred Rosenthal Date: Fri, 12 Jan 2024 16:48:48 -0800 Subject: [PATCH] added more tests --- api/src/paths/submission/intake.test.ts | 6 +++ api/src/paths/submission/intake.ts | 2 +- api/src/repositories/region-repository.ts | 6 +-- api/src/services/region-service.test.ts | 47 +++++++++++++++++++++++ api/src/services/region-service.ts | 12 +++++- 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 api/src/services/region-service.test.ts diff --git a/api/src/paths/submission/intake.test.ts b/api/src/paths/submission/intake.test.ts index 45a396e4..dcf6ed8a 100644 --- a/api/src/paths/submission/intake.test.ts +++ b/api/src/paths/submission/intake.test.ts @@ -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'; @@ -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(); @@ -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 }); }); diff --git a/api/src/paths/submission/intake.ts b/api/src/paths/submission/intake.ts index 01dd24f9..fbe426d3 100644 --- a/api/src/paths/submission/intake.ts +++ b/api/src/paths/submission/intake.ts @@ -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(); diff --git a/api/src/repositories/region-repository.ts b/api/src/repositories/region-repository.ts index 90da3206..7778e8a3 100644 --- a/api/src/repositories/region-repository.ts +++ b/api/src/repositories/region-repository.ts @@ -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, intersectThreshold: number = 1 ): Promise<{ region_id: number }[]> { const sql = SQL` @@ -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} diff --git a/api/src/services/region-service.test.ts b/api/src/services/region-service.test.ts new file mode 100644 index 00000000..5d055dcf --- /dev/null +++ b/api/src/services/region-service.test.ts @@ -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; + }); + }); +}); diff --git a/api/src/services/region-service.ts b/api/src/services/region-service.ts index 0f532c23..1ab95392 100644 --- a/api/src/services/region-service.ts +++ b/api/src/services/region-service.ts @@ -16,8 +16,16 @@ export class RegionService extends DBService { * * @param submissionId */ - async calculateRegionsForSubmission(submissionId: number): Promise { - const regionIds = await this.regionRepository.calculateRegionsForASubmission(submissionId); + async calculateAndAddRegionsForSubmission( + submissionId: number, + rowPercentage: number = 1, + intersectThreshold: number = 1 + ): Promise { + const regionIds = await this.regionRepository.calculateRegionsForASubmission( + submissionId, + rowPercentage, + intersectThreshold + ); await this.regionRepository.insertSubmissionRegions(submissionId, regionIds); } }