Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/datasets/domain/repositories/IDatasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export interface IDatasetsRepository {
createDataset(
newDataset: DatasetDTO,
datasetMetadataBlocks: MetadataBlock[],
collectionId: string
collectionId: string,
datasetType?: string
): Promise<CreatedDatasetIdentifiers>
publishDataset(datasetId: number | string, versionUpdateType: VersionUpdateType): Promise<void>
updateDataset(
Expand Down
11 changes: 9 additions & 2 deletions src/datasets/domain/useCases/CreateDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ export class CreateDataset extends DatasetWriteUseCase<CreatedDatasetIdentifiers
*
* @param {DatasetDTO} [newDataset] - DatasetDTO object including the new dataset metadata field values for each metadata block.
* @param {string} [collectionId] - Specifies the collection identifier where the new dataset should be created (optional, defaults to :root).
* @param {string} [datasetType] - Specifies the dataset type (optional, when omitted, defaults to "dataset").
* @returns {Promise<CreatedDatasetIdentifiers>}
* @throws {ResourceValidationError} - If there are validation errors related to the provided information.
* @throws {ReadError} - If there are errors while reading data.
* @throws {WriteError} - If there are errors while writing data.
*/
async execute(
newDataset: DatasetDTO,
collectionId = ROOT_COLLECTION_ID
collectionId = ROOT_COLLECTION_ID,
datasetType?: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the param to the description above @param {string}...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, added in 156963d.

): Promise<CreatedDatasetIdentifiers> {
const metadataBlocks = await this.getNewDatasetMetadataBlocks(newDataset)
this.getNewDatasetValidator().validate(newDataset, metadataBlocks)
return this.getDatasetsRepository().createDataset(newDataset, metadataBlocks, collectionId)
return this.getDatasetsRepository().createDataset(
newDataset,
metadataBlocks,
collectionId,
datasetType
)
}
}
9 changes: 7 additions & 2 deletions src/datasets/infra/repositories/DatasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ export class DatasetsRepository extends ApiRepository implements IDatasetsReposi
public async createDataset(
newDataset: DatasetDTO,
datasetMetadataBlocks: MetadataBlock[],
collectionId: string
collectionId: string,
datasetType?: string
): Promise<CreatedDatasetIdentifiers> {
return this.doPost(
`/dataverses/${collectionId}/datasets`,
transformDatasetModelToNewDatasetRequestPayload(newDataset, datasetMetadataBlocks)
transformDatasetModelToNewDatasetRequestPayload(
newDataset,
datasetMetadataBlocks,
datasetType
)
)
.then((response) => {
const responseData = response.data.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface NewDatasetRequestPayload {
license?: DatasetLicense
metadataBlocks: Record<string, MetadataBlockRequestPayload>
}
datasetType?: string
}

export interface MetadataBlockRequestPayload {
Expand Down Expand Up @@ -96,9 +97,11 @@ export const transformDatasetModelToUpdateDatasetRequestPayload = (

export const transformDatasetModelToNewDatasetRequestPayload = (
dataset: DatasetDTO,
metadataBlocks: MetadataBlock[]
metadataBlocks: MetadataBlock[],
datasetType?: string
): NewDatasetRequestPayload => {
return {
datasetType: datasetType,
datasetVersion: {
...(dataset.license && { license: dataset.license }),
metadataBlocks: transformMetadataBlockModelsToRequestPayload(
Expand Down
16 changes: 14 additions & 2 deletions test/functional/datasets/PublishDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
waitForNoLocks,
deletePublishedDatasetViaApi
} from '../../testHelpers/datasets/datasetHelper'
import { ROOT_COLLECTION_ID } from '../../../src/collections/domain/models/Collection'

// "dataset" is the default so this is equivalent to not passing a datasetType
const datasetType = 'dataset'

const testNewDataset = {
license: {
Expand Down Expand Up @@ -61,7 +65,11 @@ describe('execute', () => {
})

test('should successfully publish a dataset', async () => {
const createdDatasetIdentifiers = await createDataset.execute(testNewDataset)
const createdDatasetIdentifiers = await createDataset.execute(
testNewDataset,
ROOT_COLLECTION_ID,
datasetType
)

const response = await publishDataset.execute(
createdDatasetIdentifiers.persistentId,
Expand All @@ -74,7 +82,11 @@ describe('execute', () => {
})

test('should successfully publish a dataset with update current version', async () => {
const createdDatasetIdentifiers = await createDataset.execute(testNewDataset)
const createdDatasetIdentifiers = await createDataset.execute(
testNewDataset,
ROOT_COLLECTION_ID,
datasetType
)

const firstPublishResponse = await publishDataset.execute(
createdDatasetIdentifiers.persistentId,
Expand Down
4 changes: 3 additions & 1 deletion test/testHelpers/datasets/datasetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,11 @@ export const createDatasetMetadataBlockModel = (): MetadataBlock => {
}

export const createNewDatasetRequestPayload = (
license?: DatasetLicense
license?: DatasetLicense,
datasetType?: string
): NewDatasetRequestPayload => {
return {
datasetType: datasetType,
datasetVersion: {
...(license && { license }),
metadataBlocks: {
Expand Down
47 changes: 45 additions & 2 deletions test/unit/datasets/CreateDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,49 @@ describe('execute', () => {
expect(datasetsRepositoryStub.createDataset).toHaveBeenCalledWith(
testDataset,
testMetadataBlocks,
ROOT_COLLECTION_ID
ROOT_COLLECTION_ID,
undefined
Copy link
Contributor

@ChengShi-1 ChengShi-1 Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add an additional test to make sure datasetType would be called here.
For example,
you could create a function createDatasetwithDatasetType in DataseHelper and do something similar with this
expect(datasetsRepositoryStub.createDatasetwithDatasetType).toHaveBeenCalledWith( testDataset, testMetadataBlocks, ROOT_COLLECTION_ID, datasetType )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Added in 48cc5dc. Thanks for the help with this!

)
})

test('should return a dataset type', async () => {
const testCreatedDatasetIdentifiers: CreatedDatasetIdentifiers = {
persistentId: 'test',
numericId: 1
}

const datasetsRepositoryStub = <IDatasetsRepository>{}
datasetsRepositoryStub.createDataset = jest
.fn()
.mockResolvedValue(testCreatedDatasetIdentifiers)

const datasetValidatorStub = <ResourceValidator>{}
datasetValidatorStub.validate = jest.fn().mockResolvedValue(undefined)

const metadataBlocksRepositoryStub = <IMetadataBlocksRepository>{}
metadataBlocksRepositoryStub.getMetadataBlockByName = jest
.fn()
.mockResolvedValue(testMetadataBlocks[0])

const sut = new CreateDataset(
datasetsRepositoryStub,
metadataBlocksRepositoryStub,
datasetValidatorStub
)

const actual = await sut.execute(testDataset, ROOT_COLLECTION_ID, 'software')

expect(actual).toEqual(testCreatedDatasetIdentifiers)

expect(metadataBlocksRepositoryStub.getMetadataBlockByName).toHaveBeenCalledWith(
testMetadataBlocks[0].name
)
expect(datasetValidatorStub.validate).toHaveBeenCalledWith(testDataset, testMetadataBlocks)
expect(datasetsRepositoryStub.createDataset).toHaveBeenCalledWith(
testDataset,
testMetadataBlocks,
ROOT_COLLECTION_ID,
'software'
)
})

Expand Down Expand Up @@ -111,7 +153,8 @@ describe('execute', () => {
expect(datasetsRepositoryStub.createDataset).toHaveBeenCalledWith(
testDataset,
testMetadataBlocks,
ROOT_COLLECTION_ID
ROOT_COLLECTION_ID,
undefined
)
})

Expand Down
12 changes: 9 additions & 3 deletions test/unit/datasets/datasetTransformers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('transformNewDatasetModelToRequestPayload', () => {
expect(actual).toEqual(expectedNewDatasetRequestPayload)
})

it('should correctly transform a new dataset model to a new dataset request payload when it contains a license', () => {
it('should correctly transform a new dataset model to a new dataset request payload when it contains a license and a datasetType', () => {
const testDataset = createDatasetDTO(
undefined,
undefined,
Expand All @@ -26,10 +26,16 @@ describe('transformNewDatasetModelToRequestPayload', () => {
createDatasetLicenseModel()
)
const testMetadataBlocks = [createDatasetMetadataBlockModel()]
const datasetType = 'software'
const expectedNewDatasetRequestPayload = createNewDatasetRequestPayload(
createDatasetLicenseModel()
createDatasetLicenseModel(),
datasetType
)
const actual = transformDatasetModelToNewDatasetRequestPayload(
testDataset,
testMetadataBlocks,
datasetType
)
const actual = transformDatasetModelToNewDatasetRequestPayload(testDataset, testMetadataBlocks)

expect(actual).toEqual(expectedNewDatasetRequestPayload)
})
Expand Down
Loading