Skip to content

Commit

Permalink
occurrence service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE committed Jan 18, 2023
1 parent ce2c0ef commit d9fe5e0
Showing 1 changed file with 81 additions and 17 deletions.
98 changes: 81 additions & 17 deletions api/src/services/occurrence-service.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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', () => {
Expand All @@ -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 }]);
});
});
});

0 comments on commit d9fe5e0

Please sign in to comment.