Skip to content

Commit

Permalink
Fixed: CreateDataset dependency calls
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 22, 2024
1 parent b86911f commit 53bc05f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/datasets/domain/repositories/IDatasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ export interface IDatasetsRepository {
getDatasetUserPermissions(datasetId: number | string): Promise<DatasetUserPermissions>;
getDatasetLocks(datasetId: number | string): Promise<DatasetLock[]>;
getAllDatasetPreviews(limit?: number, offset?: number): Promise<DatasetPreviewSubset>;
createDataset(newDataset: NewDatasetDTO, datasetMetadataBlocks: MetadataBlock[], collectionId: string): Promise<CreatedDatasetIdentifiers>;
createDataset(
newDataset: NewDatasetDTO,
datasetMetadataBlocks: MetadataBlock[],
collectionId: string,
): Promise<CreatedDatasetIdentifiers>;
}
17 changes: 7 additions & 10 deletions src/datasets/domain/useCases/CreateDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ export class CreateDataset implements UseCase<CreatedDatasetIdentifiers> {

async execute(newDataset: NewDatasetDTO, collectionId: string = 'root'): Promise<CreatedDatasetIdentifiers> {
const metadataBlocks = await this.getNewDatasetMetadataBlocks(newDataset);
return await this.newDatasetValidator.validate(newDataset, metadataBlocks).then(async () => {
return await this.datasetsRepository.createDataset(newDataset, metadataBlocks, collectionId);
});
this.newDatasetValidator.validate(newDataset, metadataBlocks);
return this.datasetsRepository.createDataset(newDataset, metadataBlocks, collectionId);
}

async getNewDatasetMetadataBlocks(newDataset: NewDatasetDTO): Promise<MetadataBlock[]> {
let metadataBlocks: MetadataBlock[] = [];
for (const metadataBlockValue in newDataset.metadataBlockValues) {
metadataBlocks.push(
await this.metadataBlocksRepository.getMetadataBlockByName(
(metadataBlockValue as unknown as NewDatasetMetadataBlockValuesDTO).name,
),
);
}
await Promise.all(
newDataset.metadataBlockValues.map(async (metadataBlockValue: NewDatasetMetadataBlockValuesDTO) => {
metadataBlocks.push(await this.metadataBlocksRepository.getMetadataBlockByName(metadataBlockValue.name));
}),
);
return metadataBlocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ export class NewDatasetValidator implements NewResourceValidator {
}

private isEmptyArray(metadataFieldValue: NewDatasetMetadataFieldValueDTO): boolean {
return Array.isArray(metadataFieldValue) && (metadataFieldValue as Array<NewDatasetMetadataFieldValueDTO>).length == 0;
return (
Array.isArray(metadataFieldValue) && (metadataFieldValue as Array<NewDatasetMetadataFieldValueDTO>).length == 0
);
}

private isValidArrayType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export const transformMetadataFieldModelsToRequestPayload = (
): MetadataFieldRequestPayload[] => {
let metadataFieldsRequestPayload: MetadataFieldRequestPayload[] = [];
for (const metadataFieldKey of Object.keys(newDatasetMetadataFields)) {
const newDatasetMetadataChildFieldValue: NewDatasetMetadataFieldValueDTO = newDatasetMetadataFields[metadataFieldKey];
const newDatasetMetadataChildFieldValue: NewDatasetMetadataFieldValueDTO =
newDatasetMetadataFields[metadataFieldKey];
metadataFieldsRequestPayload.push({
value: transformMetadataFieldValueToRequestPayload(
newDatasetMetadataChildFieldValue,
Expand Down
65 changes: 49 additions & 16 deletions test/unit/datasets/CreateDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { assert, createSandbox, SinonSandbox } from 'sinon';
import { NewResourceValidator } from '../../../src/core/domain/useCases/validators/NewResourceValidator';
import { createNewDatasetDTO, createNewDatasetMetadataBlockModel } from '../../testHelpers/datasets/newDatasetHelper';
import { ResourceValidationError } from '../../../src/core/domain/useCases/validators/errors/ResourceValidationError';
import { WriteError } from '../../../src';
import { WriteError, ReadError } from '../../../src';
import { IMetadataBlocksRepository } from '../../../src/metadataBlocks/domain/repositories/IMetadataBlocksRepository';

describe('execute', () => {
Expand All @@ -17,13 +17,6 @@ describe('execute', () => {
sandbox.restore();
});

function setupMetadataBlocksRepositoryStub(): IMetadataBlocksRepository {
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlocks[0]);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;
return metadataBlocksRepositoryStub;
}

test('should return new dataset identifiers when validation is successful and repository call is successful', async () => {
const testCreatedDatasetIdentifiers: CreatedDatasetIdentifiers = {
persistentId: 'test',
Expand All @@ -38,35 +31,45 @@ describe('execute', () => {
const validateStub = sandbox.stub().resolves();
newDatasetValidatorStub.validate = validateStub;

const sut = new CreateDataset(datasetsRepositoryStub, setupMetadataBlocksRepositoryStub(), newDatasetValidatorStub);
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlocks[0]);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;

const sut = new CreateDataset(datasetsRepositoryStub, metadataBlocksRepositoryStub, newDatasetValidatorStub);

const actual = await sut.execute(testDataset);

assert.match(actual, testCreatedDatasetIdentifiers);

assert.calledWithExactly(getMetadataBlockByNameStub, testMetadataBlocks[0].name);
assert.calledWithExactly(validateStub, testDataset, testMetadataBlocks);
assert.calledWithExactly(createDatasetStub, testDataset, testMetadataBlocks, 'root');

assert.callOrder(validateStub, createDatasetStub);
});

test('should throw ResourceValidationError and not call repository when validation is unsuccessful', async () => {
const datasetsRepositoryStub = <IDatasetsRepository>{};
const createDatasetStub = sandbox.stub();
datasetsRepositoryStub.createDataset = createDatasetStub;
const datasetsRepositoryMock = <IDatasetsRepository>{};
const createDatasetMock = sandbox.stub();
datasetsRepositoryMock.createDataset = createDatasetMock;

const newDatasetValidatorStub = <NewResourceValidator>{};
const testValidationError = new ResourceValidationError('Test error');
const validateStub = sandbox.stub().throwsException(testValidationError);
newDatasetValidatorStub.validate = validateStub;

const sut = new CreateDataset(datasetsRepositoryStub, setupMetadataBlocksRepositoryStub(), newDatasetValidatorStub);
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlocks[0]);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;

const sut = new CreateDataset(datasetsRepositoryMock, metadataBlocksRepositoryStub, newDatasetValidatorStub);
let actualError: ResourceValidationError = undefined;
await sut.execute(testDataset).catch((e) => (actualError = e));
assert.match(actualError, testValidationError);

assert.calledWithExactly(getMetadataBlockByNameStub, testMetadataBlocks[0].name);
assert.calledWithExactly(validateStub, testDataset, testMetadataBlocks);
assert.notCalled(createDatasetStub);
assert.notCalled(createDatasetMock);
});

test('should throw WriteError when validation is successful and repository raises an error', async () => {
Expand All @@ -79,14 +82,44 @@ describe('execute', () => {
const validateMock = sandbox.stub().resolves();
newDatasetValidatorStub.validate = validateMock;

const sut = new CreateDataset(datasetsRepositoryStub, setupMetadataBlocksRepositoryStub(), newDatasetValidatorStub);
let actualError: ResourceValidationError = undefined;
const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const getMetadataBlockByNameStub = sandbox.stub().resolves(testMetadataBlocks[0]);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;

const sut = new CreateDataset(datasetsRepositoryStub, metadataBlocksRepositoryStub, newDatasetValidatorStub);
let actualError: WriteError = undefined;
await sut.execute(testDataset).catch((e) => (actualError = e));
assert.match(actualError, testWriteError);

assert.calledWithExactly(getMetadataBlockByNameStub, testMetadataBlocks[0].name);
assert.calledWithExactly(validateMock, testDataset, testMetadataBlocks);
assert.calledWithExactly(createDatasetStub, testDataset, testMetadataBlocks, 'root');

assert.callOrder(validateMock, createDatasetStub);
});

test('should throw ReadError when metadata blocks repository raises an error', async () => {
const datasetsRepositoryMock = <IDatasetsRepository>{};
const createDatasetMock = sandbox.stub();
datasetsRepositoryMock.createDataset = createDatasetMock;

const newDatasetValidatorMock = <NewResourceValidator>{};
const validateMock = sandbox.stub().resolves();
newDatasetValidatorMock.validate = validateMock;

const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{};
const testReadError = new ReadError('Test error');
const getMetadataBlockByNameStub = sandbox.stub().throwsException(testReadError);
metadataBlocksRepositoryStub.getMetadataBlockByName = getMetadataBlockByNameStub;

const sut = new CreateDataset(datasetsRepositoryMock, metadataBlocksRepositoryStub, newDatasetValidatorMock);
let actualError: ReadError = undefined;
await sut.execute(testDataset).catch((e) => (actualError = e));
assert.match(actualError, testReadError);

assert.notCalled(validateMock);
assert.notCalled(createDatasetMock);

assert.calledWithExactly(getMetadataBlockByNameStub, testMetadataBlocks[0].name);
});
});

0 comments on commit 53bc05f

Please sign in to comment.