From 8f09830f6a06f1e315de98114dc0c8bef779969f Mon Sep 17 00:00:00 2001 From: GPortas Date: Fri, 19 Jan 2024 11:33:04 +0000 Subject: [PATCH] Stash: createDataset repository logic WIP --- .../repositories/IDatasetsRepository.ts | 2 +- src/datasets/domain/useCases/CreateDataset.ts | 4 +- .../infra/repositories/DatasetsRepository.ts | 17 +++++++- test/unit/datasets/CreateDataset.test.ts | 4 +- test/unit/datasets/DatasetsRepository.test.ts | 40 +++++++++++++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/datasets/domain/repositories/IDatasetsRepository.ts b/src/datasets/domain/repositories/IDatasetsRepository.ts index 06ff92a9..2540cc9e 100644 --- a/src/datasets/domain/repositories/IDatasetsRepository.ts +++ b/src/datasets/domain/repositories/IDatasetsRepository.ts @@ -14,5 +14,5 @@ export interface IDatasetsRepository { getDatasetUserPermissions(datasetId: number | string): Promise; getDatasetLocks(datasetId: number | string): Promise; getAllDatasetPreviews(limit?: number, offset?: number): Promise; - createDataset(newDataset: NewDataset, datasetMetadataBlocks: MetadataBlock[]): Promise; + createDataset(newDataset: NewDataset, datasetMetadataBlocks: MetadataBlock[], collectionId: string): Promise; } diff --git a/src/datasets/domain/useCases/CreateDataset.ts b/src/datasets/domain/useCases/CreateDataset.ts index 1d5905d4..fd51289f 100644 --- a/src/datasets/domain/useCases/CreateDataset.ts +++ b/src/datasets/domain/useCases/CreateDataset.ts @@ -20,10 +20,10 @@ export class CreateDataset implements UseCase { this.newDatasetValidator = newDatasetValidator; } - async execute(newDataset: NewDataset): Promise { + async execute(newDataset: NewDataset, collectionId: string = 'root'): Promise { const metadataBlocks = await this.getNewDatasetMetadataBlocks(newDataset); return await this.newDatasetValidator.validate(newDataset, metadataBlocks).then(async () => { - return await this.datasetsRepository.createDataset(newDataset, metadataBlocks); + return await this.datasetsRepository.createDataset(newDataset, metadataBlocks, collectionId); }); } diff --git a/src/datasets/infra/repositories/DatasetsRepository.ts b/src/datasets/infra/repositories/DatasetsRepository.ts index 537c1df3..dfb099d2 100644 --- a/src/datasets/infra/repositories/DatasetsRepository.ts +++ b/src/datasets/infra/repositories/DatasetsRepository.ts @@ -10,6 +10,7 @@ import { transformDatasetPreviewsResponseToDatasetPreviewSubset } from './transf import { DatasetPreviewSubset } from '../../domain/models/DatasetPreviewSubset'; import { NewDataset } from '../../domain/models/NewDataset'; import { MetadataBlock } from '../../../metadataBlocks'; +import { transformNewDatasetModelToRequestPayload } from './transformers/newDatasetTransformers'; export interface GetAllDatasetPreviewsQueryParams { per_page?: number; @@ -18,6 +19,7 @@ export interface GetAllDatasetPreviewsQueryParams { export class DatasetsRepository extends ApiRepository implements IDatasetsRepository { private readonly datasetsResourceName: string = 'datasets'; + private readonly dataversesResourceName: string = 'dataverses'; public async getDatasetSummaryFieldNames(): Promise { return this.doGet(this.buildApiEndpoint(this.datasetsResourceName, 'summaryFieldNames')) @@ -109,7 +111,18 @@ export class DatasetsRepository extends ApiRepository implements IDatasetsReposi }); } - public async createDataset(newDataset: NewDataset, datasetMetadataBlocks: MetadataBlock[]): Promise { - console.log(newDataset + ' ' + datasetMetadataBlocks.length); + public async createDataset( + newDataset: NewDataset, + datasetMetadataBlocks: MetadataBlock[], + collectionId: string, + ): Promise { + return this.doPost( + this.buildApiEndpoint(this.dataversesResourceName, `datasets`, collectionId), + transformNewDatasetModelToRequestPayload(newDataset, datasetMetadataBlocks), + ) + .then(() => {}) + .catch((error) => { + throw error; + }); } } diff --git a/test/unit/datasets/CreateDataset.test.ts b/test/unit/datasets/CreateDataset.test.ts index 260e68f1..6b626a78 100644 --- a/test/unit/datasets/CreateDataset.test.ts +++ b/test/unit/datasets/CreateDataset.test.ts @@ -37,7 +37,7 @@ describe('execute', () => { await sut.execute(testDataset); assert.calledWithExactly(validateStub, testDataset, testMetadataBlocks); - assert.calledWithExactly(createDatasetStub, testDataset, testMetadataBlocks); + assert.calledWithExactly(createDatasetStub, testDataset, testMetadataBlocks, 'root') assert.callOrder(validateStub, createDatasetStub); }); @@ -77,7 +77,7 @@ describe('execute', () => { assert.match(actualError, testWriteError); assert.calledWithExactly(validateMock, testDataset, testMetadataBlocks); - assert.calledWithExactly(createDatasetStub, testDataset, testMetadataBlocks); + assert.calledWithExactly(createDatasetStub, testDataset, testMetadataBlocks, 'root'); assert.callOrder(validateMock, createDatasetStub); }); diff --git a/test/unit/datasets/DatasetsRepository.test.ts b/test/unit/datasets/DatasetsRepository.test.ts index 261df777..9179a3f2 100644 --- a/test/unit/datasets/DatasetsRepository.test.ts +++ b/test/unit/datasets/DatasetsRepository.test.ts @@ -17,6 +17,11 @@ import { createDatasetPreviewModel, createDatasetPreviewPayload, } from '../../testHelpers/datasets/datasetPreviewHelper'; +import { + createNewDatasetModel, + createNewDatasetMetadataBlockModel, + createNewDatasetRequestPayload, +} from '../../testHelpers/datasets/newDatasetHelper'; describe('DatasetsRepository', () => { const sandbox: SinonSandbox = createSandbox(); @@ -600,4 +605,39 @@ describe('DatasetsRepository', () => { expect(error).to.be.instanceOf(Error); }); }); + + describe('createDataset', () => { + const testNewDataset = createNewDatasetModel(); + const testMetadataBlocks = [createNewDatasetMetadataBlockModel()]; + const testCollectionName = 'test'; + const expectedNewDatasetRequestPayloadJson = JSON.stringify(createNewDatasetRequestPayload()); + + const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/dataverses/${testCollectionName}/datasets`; + + test('should call the API with a correct request payload', async () => { + const axiosPostMock = sandbox.stub(axios, 'post'); + + // API Key auth + await sut.createDataset(testNewDataset, testMetadataBlocks, testCollectionName); + + assert.calledWithExactly( + axiosPostMock, + expectedApiEndpoint, + expectedNewDatasetRequestPayloadJson, + TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY, + ); + + // Session cookie auth + ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE); + + await sut.createDataset(testNewDataset, testMetadataBlocks, testCollectionName); + + assert.calledWithExactly( + axiosPostMock, + expectedApiEndpoint, + expectedNewDatasetRequestPayloadJson, + TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE, + ); + }); + }); });