Skip to content

Commit

Permalink
Added: test case to NewDatasetValidator and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 15, 2024
1 parent 32ce475 commit 3306896
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions test/unit/datasets/NewDatasetValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { fail } from 'assert';
import { IMetadataBlocksRepository } from '../../../src/metadataBlocks/domain/repositories/IMetadataBlocksRepository';
import { EmptyFieldError } from '../../../src/core/domain/useCases/validators/errors/EmptyFieldError';
import { FieldValidationError } from '../../../src/core/domain/useCases/validators/errors/FieldValidationError';
import { NewDataset } from '../../../src/datasets/domain/models/NewDataset';

describe('execute', () => {
const sandbox: SinonSandbox = createSandbox();
Expand All @@ -25,6 +26,25 @@ describe('execute', () => {
return metadataBlocksRepositoryStub;
}

async function runValidateExpectingFieldValidationError<T extends FieldValidationError>(
newDataset: NewDataset,
expectedErrorMessage: string,
): Promise<void> {
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());
await sut
.validate(newDataset)
.then(() => {
fail('Validation should fail');
})
.catch((error) => {
const fieldValidationError = error as T;
assert.match(fieldValidationError.citationBlockName, 'citation');
assert.match(fieldValidationError.metadataFieldName, 'author');
assert.match(fieldValidationError.parentMetadataFieldName, undefined);
assert.match(fieldValidationError.message, expectedErrorMessage);
});
}

test('should not raise validation error when new dataset is valid', async () => {
const testNewDataset = createNewDatasetModel();
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());
Expand All @@ -33,45 +53,27 @@ describe('execute', () => {
});

test('should raise an empty field error when a first level field is missing', async () => {
const testNewDataset = createNewDatasetModelWithoutFirstLevelRequiredField();
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());

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.',
);
});
await runValidateExpectingFieldValidationError<EmptyFieldError>(
createNewDatasetModelWithoutFirstLevelRequiredField(),
'There was an error when validating the field author from metadata block citation. Reason was: The field should not be empty.',
);
});

test('should raise an error when the provided field value for a multiple field is a string', async () => {
const invalidAuthorFieldValue = 'invalidValue';
const testNewDataset = createNewDatasetModel(invalidAuthorFieldValue);
const sut = new NewDatasetValidator(setupMetadataBlocksRepositoryStub());
await runValidateExpectingFieldValidationError<FieldValidationError>(
testNewDataset,
'There was an error when validating the field author from metadata block citation. Reason was: Expecting an array of values.',
);
});

await sut
.validate(testNewDataset)
.then(() => {
fail('Validation should fail');
})
.catch((error) => {
const fieldValidationError = error as FieldValidationError;
assert.match(fieldValidationError.citationBlockName, 'citation');
assert.match(fieldValidationError.metadataFieldName, 'author');
assert.match(fieldValidationError.parentMetadataFieldName, undefined);
assert.match(
fieldValidationError.message,
'There was an error when validating the field author from metadata block citation. Reason was: Expecting an array of values.',
);
});
test('should raise an error when the provided field value is an array of strings and the field expects an array of objects', async () => {
const invalidAuthorFieldValue = ['invalidValue1', 'invalidValue2'];
const testNewDataset = createNewDatasetModel(invalidAuthorFieldValue);
await runValidateExpectingFieldValidationError<FieldValidationError>(
testNewDataset,
'There was an error when validating the field author from metadata block citation. Reason was: Expecting an array of sub fields, not strings',
);
});
});

0 comments on commit 3306896

Please sign in to comment.