Skip to content

Commit

Permalink
Tweak artifact submission/tests.
Browse files Browse the repository at this point in the history
ignore-skip
  • Loading branch information
NickPhura committed Jan 22, 2024
1 parent d12f517 commit ec21bbc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
4 changes: 3 additions & 1 deletion api/src/models/biohub-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('PostSurveyToBiohubObject', () => {
survey_obj,
[observation_obj],
{ type: 'FeatureCollection', features: [] },
[],
[]
);
});
Expand Down Expand Up @@ -206,6 +207,7 @@ describe('PostSurveySubmissionToBioHubObject', () => {
observation_obj,
survey_geometry,
[],
[],
submissionComment
);
});
Expand All @@ -227,7 +229,7 @@ describe('PostSurveySubmissionToBioHubObject', () => {
});

it('sets content', () => {
expect(data.content).to.eql(new PostSurveyToBiohubObject(survey_obj, observation_obj, survey_geometry, []));
expect(data.content).to.eql(new PostSurveyToBiohubObject(survey_obj, observation_obj, survey_geometry, [], []));
});
});
});
41 changes: 41 additions & 0 deletions api/src/repositories/attachment-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { QueryResult } from 'pg';
import SQL from 'sql-template-strings';
import { ATTACHMENT_TYPE } from '../constants/attachments';
import { getKnex } from '../database/db';
import { ApiExecuteSQLError } from '../errors/api-error';
import { PostReportAttachmentMetadata, PutReportAttachmentMetadata } from '../models/project-survey-attachments';
Expand Down Expand Up @@ -414,6 +415,46 @@ export class AttachmentRepository extends BaseRepository {
return response.rows;
}

/**
* Get all survey attachments for the given survey id, which are safe to publish to BioHub.
*
* Note: Not all attachment types are publishable to BioHub. This method filters out attachment types that should not
* be published.
*
* @param {number} surveyId
* @param {number[]} attachmentIds
* @return {*} {Promise<ISurveyAttachment[]>}
* @memberof AttachmentRepository
*/
async getSurveyAttachmentsForBioHubSubmission(surveyId: number): Promise<ISurveyAttachment[]> {
defaultLog.debug({ label: 'getSurveyAttachmentsForBioHubSubmission' });

Check warning on line 430 in api/src/repositories/attachment-repository.ts

View check run for this annotation

Codecov / codecov/patch

api/src/repositories/attachment-repository.ts#L430

Added line #L430 was not covered by tests

const sqlStatement = SQL`

Check warning on line 432 in api/src/repositories/attachment-repository.ts

View check run for this annotation

Codecov / codecov/patch

api/src/repositories/attachment-repository.ts#L432

Added line #L432 was not covered by tests
SELECT
survey_attachment_id,
uuid,
file_name,
file_type,
title,
description,
create_date,
update_date,
create_date,
file_size,
key
FROM
survey_attachment
WHERE
survey_id = ${surveyId}
AND
LOWER(file_type) != LOWER(${ATTACHMENT_TYPE.KEYX});
`;

const response = await this.connection.sql<ISurveyAttachment>(sqlStatement);

Check warning on line 453 in api/src/repositories/attachment-repository.ts

View check run for this annotation

Codecov / codecov/patch

api/src/repositories/attachment-repository.ts#L453

Added line #L453 was not covered by tests

return response.rows;

Check warning on line 455 in api/src/repositories/attachment-repository.ts

View check run for this annotation

Codecov / codecov/patch

api/src/repositories/attachment-repository.ts#L455

Added line #L455 was not covered by tests
}

/**
* Query to return all survey report attachments belonging to the given survey.
* @param {number} surveyId the ID of the survey
Expand Down
14 changes: 14 additions & 0 deletions api/src/services/attachment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ export class AttachmentService extends DBService {
return this.attachmentRepository.getSurveyAttachmentsByIds(surveyId, attachmentIds);
}

/**
* Get all survey attachments for the given survey ID, which are publishable to BioHub.
*
* Note: Not all attachment types are publishable to BioHub. This method filters out attachment types that should not
* be published.
*
* @param {number} surveyId the ID of the survey.
* @return {Promise<ISurveyAttachment[]>} Promise resolving all survey publishable attachments.
* @memberof AttachmentService
*/
async getSurveyAttachmentsForBioHubSubmission(surveyId: number): Promise<ISurveyAttachment[]> {
return this.attachmentRepository.getSurveyAttachmentsForBioHubSubmission(surveyId);

Check warning on line 237 in api/src/services/attachment-service.ts

View check run for this annotation

Codecov / codecov/patch

api/src/services/attachment-service.ts#L237

Added line #L237 was not covered by tests
}

/**
* Finds all of the survey report attachments for the given survey ID.
* @param {number} surveyId the ID of the survey
Expand Down
19 changes: 14 additions & 5 deletions api/src/services/platform-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ describe('PlatformService', () => {
.stub(KeycloakService.prototype, 'getKeycloakServiceToken')
.resolves('token');

sinon.stub(AttachmentService.prototype, 'getSurveyAttachments').resolves([]);
sinon.stub(AttachmentService.prototype, 'getSurveyAttachmentsForBioHubSubmission').resolves([]);

sinon.stub(AttachmentService.prototype, 'getSurveyReportAttachments').resolves([]);

const _generateSurveyDataPackageStub = sinon
.stub(PlatformService.prototype, '_generateSurveyDataPackage')
Expand All @@ -61,7 +63,7 @@ describe('PlatformService', () => {
} catch (error) {
expect((error as Error).message).to.equal('Failed to submit survey ID to Biohub');
expect(getKeycloakServiceTokenStub).to.have.been.calledOnce;
expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], 'test');
expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], [], 'test');
}
});

Expand All @@ -76,7 +78,9 @@ describe('PlatformService', () => {
.stub(KeycloakService.prototype, 'getKeycloakServiceToken')
.resolves('token');

sinon.stub(AttachmentService.prototype, 'getSurveyAttachments').resolves([]);
sinon.stub(AttachmentService.prototype, 'getSurveyAttachmentsForBioHubSubmission').resolves([]);

sinon.stub(AttachmentService.prototype, 'getSurveyReportAttachments').resolves([]);

const _generateSurveyDataPackageStub = sinon
.stub(PlatformService.prototype, '_generateSurveyDataPackage')
Expand All @@ -88,15 +92,20 @@ describe('PlatformService', () => {
.stub(PlatformService.prototype, '_submitSurveyAttachmentsToBioHub')
.resolves();

const _submitSurveyReportAttachmentsToBioHubStub = sinon
.stub(PlatformService.prototype, '_submitSurveyReportAttachmentsToBioHub')
.resolves();

const insertSurveyMetadataPublishRecordStub = sinon
.stub(HistoryPublishService.prototype, 'insertSurveyMetadataPublishRecord')
.resolves();

const response = await platformService.submitSurveyToBioHub(1, { submissionComment: 'test' });

expect(getKeycloakServiceTokenStub).to.have.been.calledOnce;
expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], 'test');
expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], [], 'test');
expect(_submitSurveyAttachmentsToBioHubStub).to.have.been.calledOnceWith('123-456-789', [], []);
expect(_submitSurveyReportAttachmentsToBioHubStub).to.have.been.calledOnceWith('123-456-789', [], []);
expect(insertSurveyMetadataPublishRecordStub).to.have.been.calledOnceWith({
survey_id: 1,
submission_uuid: '123-456-789'
Expand Down Expand Up @@ -128,7 +137,7 @@ describe('PlatformService', () => {
.stub(SurveyService.prototype, 'getSurveyLocationsData')
.resolves([] as any);

const response = await platformService._generateSurveyDataPackage(1, [], 'a comment about the submission');
const response = await platformService._generateSurveyDataPackage(1, [], [], 'a comment about the submission');

expect(getSurveyDataStub).to.have.been.calledOnceWith(1);
expect(getSurveyPurposeAndMethodologyStub).to.have.been.calledOnceWith(1);
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class PlatformService extends DBService {
const backboneSurveyIntakeUrl = new URL(getBackboneSurveyIntakePath(), getBackboneApiHost()).href;

// Get survey attachments
const surveyAttachments = await this.attachmentService.getSurveyAttachments(surveyId);
const surveyAttachments = await this.attachmentService.getSurveyAttachmentsForBioHubSubmission(surveyId);

// Get survey report attachments
const surveyReportAttachments = await this.attachmentService.getSurveyReportAttachments(surveyId);
Expand Down

0 comments on commit ec21bbc

Please sign in to comment.