Skip to content

Commit

Permalink
Stash: createDataset repository logic WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 19, 2024
1 parent 5ad4e25 commit 8f09830
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/datasets/domain/repositories/IDatasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export interface IDatasetsRepository {
getDatasetUserPermissions(datasetId: number | string): Promise<DatasetUserPermissions>;
getDatasetLocks(datasetId: number | string): Promise<DatasetLock[]>;
getAllDatasetPreviews(limit?: number, offset?: number): Promise<DatasetPreviewSubset>;
createDataset(newDataset: NewDataset, datasetMetadataBlocks: MetadataBlock[]): Promise<void>;
createDataset(newDataset: NewDataset, datasetMetadataBlocks: MetadataBlock[], collectionId: string): Promise<void>;
}
4 changes: 2 additions & 2 deletions src/datasets/domain/useCases/CreateDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export class CreateDataset implements UseCase<void> {
this.newDatasetValidator = newDatasetValidator;
}

async execute(newDataset: NewDataset): Promise<void> {
async execute(newDataset: NewDataset, collectionId: string = 'root'): Promise<void> {
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);
});
}

Expand Down
17 changes: 15 additions & 2 deletions src/datasets/infra/repositories/DatasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string[]> {
return this.doGet(this.buildApiEndpoint(this.datasetsResourceName, 'summaryFieldNames'))
Expand Down Expand Up @@ -109,7 +111,18 @@ export class DatasetsRepository extends ApiRepository implements IDatasetsReposi
});
}

public async createDataset(newDataset: NewDataset, datasetMetadataBlocks: MetadataBlock[]): Promise<void> {
console.log(newDataset + ' ' + datasetMetadataBlocks.length);
public async createDataset(
newDataset: NewDataset,
datasetMetadataBlocks: MetadataBlock[],
collectionId: string,
): Promise<void> {
return this.doPost(
this.buildApiEndpoint(this.dataversesResourceName, `datasets`, collectionId),
transformNewDatasetModelToRequestPayload(newDataset, datasetMetadataBlocks),
)
.then(() => {})
.catch((error) => {
throw error;
});
}
}
4 changes: 2 additions & 2 deletions test/unit/datasets/CreateDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
40 changes: 40 additions & 0 deletions test/unit/datasets/DatasetsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);
});
});
});

0 comments on commit 8f09830

Please sign in to comment.