Skip to content

Commit

Permalink
apply fix to summary result validateXLSX
Browse files Browse the repository at this point in the history
  • Loading branch information
anissa-agahchen committed May 2, 2023
1 parent 819520c commit faaa6d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
38 changes: 12 additions & 26 deletions api/src/services/summary-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,36 +601,22 @@ describe('SummaryService', () => {
}
});

it('should throw INVALID_MEDIA error if validateXLSX fails with invalid media', async () => {
it('should return early if media_state is invalid', async () => {
const service = mockService();
const file = new MediaFile('test.txt', 'text/plain', Buffer.of(0));
const xlsxCsv = new XLSXCSV(file);
const validation = 'test-template-validation-schema';
const mockSchemaParser = { validationSchema: validation };
const mockMediaState = {
fileName: 'test file',
isValid: false
} as IMediaState;
const xlsx = new XLSXCSV(buildFile('test file', {}));
const parser = new ValidationSchemaParser({});

sinon.stub(XLSXCSV.prototype, 'validateMedia');
sinon.stub(XLSXCSV.prototype, 'getMediaState').returns({
isValid: false,
fileName: 'test filename'
});
sinon.stub(XLSXCSV.prototype, 'getMediaState').returns(mockMediaState);

const getValidation = sinon.stub(service, 'getValidationRules').resolves(mockSchemaParser);
sinon.stub(FileUtils, 'getFileFromS3').resolves('file from s3' as any);
sinon
.stub(service, 'getSummaryTemplateSpeciesRecords')
.resolves([{ ...makeMockTemplateSpeciesRecord(1), validation }]);
const result = await service.validateXLSX(xlsx, parser);

try {
await service.summaryTemplateValidation(xlsxCsv, 1);
expect.fail();
} catch (error) {
expect(getValidation).to.be.calledWith('test-template-validation-schema');
expect(error).to.be.instanceOf(SummarySubmissionError);
if (error instanceof SummarySubmissionError) {
expect(error.summarySubmissionMessages.length).to.equal(1);
expect(error.summarySubmissionMessages[0].type).to.equal(SUMMARY_SUBMISSION_MESSAGE_TYPE.INVALID_MEDIA);
}
}
expect(result.csv_state).to.be.eql([]);
expect(result.media_state.fileName).to.be.eql(mockMediaState.fileName);
expect(result.media_state.isValid).to.be.eql(false);
});
});

Expand Down
20 changes: 13 additions & 7 deletions api/src/services/summary-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class SummaryService extends DBService {

const media_state = file.getMediaState();
if (!media_state.isValid) {
throw SummarySubmissionErrorFromMessageType(SUMMARY_SUBMISSION_MESSAGE_TYPE.INVALID_MEDIA);
return { csv_state: ([] as unknown) as ICsvState[], media_state };
}

// Run CSV content validations
Expand All @@ -331,7 +331,13 @@ export class SummaryService extends DBService {
const errors: MessageError<SUMMARY_SUBMISSION_MESSAGE_TYPE>[] = [];

mediaState.fileErrors?.forEach((fileError) => {
errors.push(new MessageError(SUMMARY_SUBMISSION_MESSAGE_TYPE.INVALID_MEDIA, `${fileError}`, 'Miscellaneous'));
errors.push(
new MessageError(
SUMMARY_SUBMISSION_MESSAGE_TYPE.INVALID_MEDIA,
`${fileError}`,
SUMMARY_SUBMISSION_MESSAGE_TYPE.INVALID_MEDIA
)
);
});

csvState?.forEach((csvStateItem) => {
Expand Down Expand Up @@ -364,13 +370,13 @@ export class SummaryService extends DBService {
)
);
});

if (!mediaState.isValid || csvState?.some((item) => !item.isValid)) {
// At least 1 error exists, skip remaining steps
parseError = true;
}
});

if (mediaState.fileErrors?.length || csvState?.some((item) => !item.isValid)) {
// At least 1 error exists, skip remaining steps
parseError = true;
}

if (parseError) {
throw new SummarySubmissionError({ messages: errors });
}
Expand Down
3 changes: 1 addition & 2 deletions api/src/services/validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export class ValidationService extends DBService {
}

validateXLSX(file: XLSXCSV, parser: ValidationSchemaParser) {
// Run media validations
defaultLog.debug({ label: 'validateXLSX' });
file.validateMedia(parser);

const media_state = file.getMediaState();
Expand All @@ -369,7 +369,6 @@ export class ValidationService extends DBService {
return { csv_state: ([] as unknown) as ICsvState[], media_state };
}

// Run CSV content validations
file.validateContent(parser);
const csv_state = file.getContentState();

Expand Down

0 comments on commit faaa6d8

Please sign in to comment.