Skip to content

Commit

Permalink
Added: test cases for NewDatasetValidator empty field error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 12, 2024
1 parent 644f228 commit 2c35633
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 39 deletions.
20 changes: 4 additions & 16 deletions src/core/domain/useCases/validators/errors/FieldValidationError.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ResourceValidationError } from './ResourceValidationError';

export class FieldValidationError extends ResourceValidationError {
private citationBlockName: string;
private metadataFieldName: string;
private parentMetadataFieldName?: string;
citationBlockName: string;
metadataFieldName: string;
parentMetadataFieldName?: string;

constructor(metadataFieldName: string, citationBlockName: string, parentMetadataFieldName?: string, reason?: string) {
let message = `There was an error when validating the field ${metadataFieldName} from metadata block ${citationBlockName}`;
if (metadataFieldName) {
if (parentMetadataFieldName) {
message += ` with parent field ${parentMetadataFieldName}`;
}
if (reason) {
Expand All @@ -18,16 +18,4 @@ export class FieldValidationError extends ResourceValidationError {
this.metadataFieldName = metadataFieldName;
this.parentMetadataFieldName = parentMetadataFieldName;
}

getCitationBlockName(): string {
return this.citationBlockName;
}

getMetadataFieldName(): string {
return this.metadataFieldName;
}

getParentMetadataFieldName(): string | undefined {
return this.parentMetadataFieldName;
}
}
35 changes: 15 additions & 20 deletions src/datasets/domain/useCases/validators/NewDatasetValidator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NewDataset, NewDatasetMetadataFieldValue } from '../../models/NewDataset';
import { NewResourceValidator } from '../../../../core/domain/useCases/validators/NewResourceValidator';
import { IMetadataBlocksRepository } from '../../../../metadataBlocks/domain/repositories/IMetadataBlocksRepository';
import { MetadataBlock, MetadataFieldInfo } from '../../../../metadataBlocks';
import { MetadataFieldInfo } from '../../../../metadataBlocks';
import { ResourceValidationError } from '../../../../core/domain/useCases/validators/errors/ResourceValidationError';
import { WriteError } from '../../../../core';
import { EmptyFieldError } from '../../../../core/domain/useCases/validators/errors/EmptyFieldError';

export class NewDatasetValidator implements NewResourceValidator<NewDataset> {
Expand All @@ -16,24 +15,20 @@ export class NewDatasetValidator implements NewResourceValidator<NewDataset> {
async validate(resource: NewDataset): Promise<void | ResourceValidationError> {
for (const metadataBlockValues of resource.metadataBlockValues) {
const newDatasetMetadataBlockName = metadataBlockValues.name;
await this.metadataBlockRepository
.getMetadataBlockByName(newDatasetMetadataBlockName)
.then((metadataBlock: MetadataBlock) => {
Object.keys(metadataBlock.metadataFields).map((metadataFieldKey) => {
const metadataFieldInfo: MetadataFieldInfo = metadataBlock.metadataFields[metadataFieldKey];
const newDatasetMetadataFieldValue: NewDatasetMetadataFieldValue =
metadataBlockValues.fields[metadataFieldKey];
if (metadataFieldInfo.isRequired && newDatasetMetadataFieldValue == undefined) {
throw new EmptyFieldError(metadataFieldKey, newDatasetMetadataBlockName);
}
if (metadataFieldInfo.childMetadataFields != undefined) {
// TODO: child fields validation
}
});
})
.catch((error: WriteError) => {
throw new ResourceValidationError(error.message);
});

const metadataBlock = await this.metadataBlockRepository.getMetadataBlockByName(newDatasetMetadataBlockName);
for (const metadataFieldKey of Object.keys(metadataBlock.metadataFields)) {
const metadataFieldInfo: MetadataFieldInfo = metadataBlock.metadataFields[metadataFieldKey];
const newDatasetMetadataFieldValue: NewDatasetMetadataFieldValue = metadataBlockValues.fields[metadataFieldKey];

if (metadataFieldInfo.isRequired && newDatasetMetadataFieldValue == undefined) {
throw new EmptyFieldError(metadataFieldKey, newDatasetMetadataBlockName);
}

if (metadataFieldInfo.childMetadataFields != undefined) {
// TODO: child fields validation
}
}
}
}
}
13 changes: 13 additions & 0 deletions test/testHelpers/datasets/newDatasetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export const createNewDatasetModel = (): NewDataset => {
};
};

export const createNewDatasetModelWithoutRequiredField = (): NewDataset => {
return {
metadataBlockValues: [
{
name: 'citation',
fields: {
title: 'test dataset',
},
},
],
};
};

export const createNewDatasetMetadataBlockModel = (): MetadataBlock => {
return {
id: 1,
Expand Down
36 changes: 33 additions & 3 deletions test/unit/datasets/NewDatasetValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { NewDatasetValidator } from '../../../src/datasets/domain/useCases/validators/NewDatasetValidator';
import { createSandbox, SinonSandbox } from 'sinon';
import { createNewDatasetModel, createNewDatasetMetadataBlockModel } from '../../testHelpers/datasets/newDatasetHelper';
import { assert, createSandbox, SinonSandbox } from 'sinon';
import {
createNewDatasetModel,
createNewDatasetMetadataBlockModel,
createNewDatasetModelWithoutRequiredField,
} from '../../testHelpers/datasets/newDatasetHelper';
import { fail } from 'assert';
import { IMetadataBlocksRepository } from '../../../src/metadataBlocks/domain/repositories/IMetadataBlocksRepository';
import { EmptyFieldError } from '../../../src/core/domain/useCases/validators/errors/EmptyFieldError';

describe('execute', () => {
const sandbox: SinonSandbox = createSandbox();
Expand All @@ -11,7 +16,7 @@ describe('execute', () => {
sandbox.restore();
});

test('should not raise exception when new dataset is valid', async () => {
test('should not raise validation error when new dataset is valid', async () => {
const testNewDataset = createNewDatasetModel();
const testMetadataBlock = createNewDatasetMetadataBlockModel();
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
Expand All @@ -21,4 +26,29 @@ describe('execute', () => {

await sut.validate(testNewDataset).catch((e) => fail(e));
});

test('should raise empty field error when a first level field is missing', async () => {
const testNewDataset = createNewDatasetModelWithoutRequiredField();
const testMetadataBlock = createNewDatasetMetadataBlockModel();
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlock);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;
const sut = new NewDatasetValidator(metadataBlocksRepositoryStub);

await sut
.validate(testNewDataset)
.then(() => {
fail('Validation should fail');
})
.catch((error) => {
const emptyFieldError = error as EmptyFieldError;
assert.match(emptyFieldError.citationBlockName, 'citation');
assert.match(emptyFieldError.metadataFieldName, 'author');
assert.match(emptyFieldError.parentMetadataFieldName, undefined);
assert.match(
emptyFieldError.message,
'There was an error when validating the field author from metadata block citation. Reason was: The field should not be empty.',
);
});
});
});

0 comments on commit 2c35633

Please sign in to comment.