From d9fe5e0df30cc0121c35107bb86ddfbfc5b0c377 Mon Sep 17 00:00:00 2001 From: Kjartan Date: Tue, 17 Jan 2023 18:48:32 -0800 Subject: [PATCH] occurrence service tests --- api/src/services/occurrence-service.test.ts | 98 +++++++++++++++++---- 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/api/src/services/occurrence-service.test.ts b/api/src/services/occurrence-service.test.ts index 88faedec4b..634c42dd48 100644 --- a/api/src/services/occurrence-service.test.ts +++ b/api/src/services/occurrence-service.test.ts @@ -1,8 +1,9 @@ import chai, { expect } from 'chai'; +import { Feature, FeatureCollection } from 'geojson'; import { describe } from 'mocha'; import sinon from 'sinon'; import sinonChai from 'sinon-chai'; -import { OccurrenceRepository } from '../repositories/occurrence-repository'; +import { ISpatialComponentFeaturePropertiesRow, OccurrenceRepository } from '../repositories/occurrence-repository'; import { getMockDBConnection } from '../__mocks__/db'; import { OccurrenceService } from './occurrence-service'; @@ -18,24 +19,52 @@ describe('OccurrenceService', () => { return new OccurrenceService(dbConnection); }; - it('should return a post occurrence', async () => { - const submissionId = 1; - const repo = sinon.stub(OccurrenceRepository.prototype, 'getOccurrenceSubmission').resolves({ - occurrence_submission_id: 1, - survey_id: 1, - template_methodology_species_id: 1, - source: '', - input_key: '', - input_file_name: '', - output_key: '', - output_file_name: '' + describe('getOccurrenceSubmission', () => { + it('should return a post occurrence', async () => { + const submissionId = 1; + const repo = sinon.stub(OccurrenceRepository.prototype, 'getOccurrenceSubmission').resolves({ + occurrence_submission_id: 1, + survey_id: 1, + template_methodology_species_id: 1, + source: '', + input_key: '', + input_file_name: '', + output_key: '', + output_file_name: '' + }); + const dbConnection = getMockDBConnection(); + const service = new OccurrenceService(dbConnection); + const response = await service.getOccurrenceSubmission(submissionId); + + expect(repo).to.be.calledOnce; + expect(response?.occurrence_submission_id).to.be.eql(submissionId); }); - const dbConnection = getMockDBConnection(); - const service = new OccurrenceService(dbConnection); - const response = await service.getOccurrenceSubmission(submissionId); + }); + + describe('getOccurrences', () => { + it('should return a post occurrence', async () => { + const submissionId = 1; + const repo = sinon.stub(OccurrenceRepository.prototype, 'getOccurrencesForView').resolves([ + { + taxa_data: [{ associated_taxa: 'string;', vernacular_name: 'string;', submission_spatial_component_id: 1 }], + spatial_component: { + spatial_data: ({ features: [({ id: 1 } as unknown) as Feature] } as unknown) as FeatureCollection + } + } + ]); + + const dbConnection = getMockDBConnection(); + const service = new OccurrenceService(dbConnection); + const response = await service.getOccurrences(submissionId); - expect(repo).to.be.calledOnce; - expect(response?.occurrence_submission_id).to.be.eql(submissionId); + expect(repo).to.be.calledOnce; + expect(response).to.be.eql([ + { + taxa_data: [{ associated_taxa: 'string;', vernacular_name: 'string;', submission_spatial_component_id: 1 }], + spatial_data: { features: [({ id: 1 } as unknown) as Feature] } + } + ]); + }); }); describe('updateSurveyOccurrenceSubmission', () => { @@ -57,4 +86,39 @@ describe('OccurrenceService', () => { expect(id).to.be.eql(1); }); }); + + describe('findSpatialMetadataBySubmissionSpatialComponentIds', () => { + it('should return spatial components', async () => { + const service = mockService(); + sinon + .stub(OccurrenceRepository.prototype, 'findSpatialMetadataBySubmissionSpatialComponentIds') + .resolves([({ spatial_component_properties: { id: 1 } } as unknown) as ISpatialComponentFeaturePropertiesRow]); + + const id = await service.findSpatialMetadataBySubmissionSpatialComponentIds([1]); + expect(id).to.be.eql([{ id: 1 }]); + }); + }); + + describe('deleteOccurrenceSubmission', () => { + it('should delete all occurrence data by id', async () => { + const service = mockService(); + + const softDeleteOccurrenceSubmissionStub = sinon + .stub(OccurrenceRepository.prototype, 'softDeleteOccurrenceSubmission') + .resolves(); + const deleteSpatialTransformSubmissionStub = sinon + .stub(OccurrenceRepository.prototype, 'deleteSpatialTransformSubmission') + .resolves(); + const deleteSubmissionSpatialComponentStub = sinon + .stub(OccurrenceRepository.prototype, 'deleteSubmissionSpatialComponent') + .resolves([{ submission_spatial_component_id: 1 }]); + + const id = await service.deleteOccurrenceSubmission(1); + + expect(softDeleteOccurrenceSubmissionStub).to.be.calledOnce; + expect(deleteSpatialTransformSubmissionStub).to.be.calledOnce; + expect(deleteSubmissionSpatialComponentStub).to.be.calledOnce; + expect(id).to.be.eql([{ submission_spatial_component_id: 1 }]); + }); + }); });