|
1 | 1 | import { createDataset, DatasetDTO } from '../../../src/datasets' |
2 | | -import { ApiConfig } from '../../../src' |
| 2 | +import { ApiConfig, WriteError } from '../../../src' |
3 | 3 | import { TestConstants } from '../../testHelpers/TestConstants' |
4 | 4 | import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
5 | 5 | import { FieldValidationError } from '../../../src/datasets/domain/useCases/validators/errors/FieldValidationError' |
@@ -61,6 +61,58 @@ describe('execute', () => { |
61 | 61 | } |
62 | 62 | }) |
63 | 63 |
|
| 64 | + test('should successfully create a new dataset when a valid dataset type is sent', async () => { |
| 65 | + const testNewDataset = { |
| 66 | + metadataBlockValues: [ |
| 67 | + { |
| 68 | + name: 'citation', |
| 69 | + fields: { |
| 70 | + title: 'Dataset created using the createDataset use case', |
| 71 | + author: [ |
| 72 | + { |
| 73 | + authorName: 'Admin, Dataverse', |
| 74 | + authorAffiliation: 'Dataverse.org' |
| 75 | + }, |
| 76 | + { |
| 77 | + authorName: 'Owner, Dataverse', |
| 78 | + authorAffiliation: 'Dataversedemo.org' |
| 79 | + } |
| 80 | + ], |
| 81 | + datasetContact: [ |
| 82 | + { |
| 83 | + datasetContactEmail: 'finch@mailinator.com', |
| 84 | + datasetContactName: 'Finch, Fiona' |
| 85 | + } |
| 86 | + ], |
| 87 | + dsDescription: [ |
| 88 | + { |
| 89 | + dsDescriptionValue: 'This is the description of the dataset.' |
| 90 | + } |
| 91 | + ], |
| 92 | + subject: ['Medicine, Health and Life Sciences'] |
| 93 | + } |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | + expect.assertions(3) |
| 98 | + |
| 99 | + try { |
| 100 | + const defaultDatasetType = 'dataset' |
| 101 | + const createdDatasetIdentifiers = await createDataset.execute( |
| 102 | + testNewDataset, |
| 103 | + ':root', |
| 104 | + defaultDatasetType |
| 105 | + ) |
| 106 | + |
| 107 | + expect(createdDatasetIdentifiers).not.toBeNull() |
| 108 | + expect(createdDatasetIdentifiers.numericId).not.toBeNull() |
| 109 | + expect(createdDatasetIdentifiers.persistentId).not.toBeNull() |
| 110 | + await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId) |
| 111 | + } catch (error) { |
| 112 | + throw new Error('Dataset should be created') |
| 113 | + } |
| 114 | + }) |
| 115 | + |
64 | 116 | test('should throw an error when a first level required field is missing', async () => { |
65 | 117 | const testNewDataset = { |
66 | 118 | metadataBlockValues: [ |
@@ -213,4 +265,52 @@ describe('execute', () => { |
213 | 265 | ) |
214 | 266 | } |
215 | 267 | }) |
| 268 | + |
| 269 | + test('should throw an error when an invalid dataset type is sent', async () => { |
| 270 | + const testNewDataset = { |
| 271 | + metadataBlockValues: [ |
| 272 | + { |
| 273 | + name: 'citation', |
| 274 | + fields: { |
| 275 | + title: 'Dataset created using the createDataset use case', |
| 276 | + author: [ |
| 277 | + { |
| 278 | + authorName: 'Admin, Dataverse', |
| 279 | + authorAffiliation: 'Dataverse.org' |
| 280 | + }, |
| 281 | + { |
| 282 | + authorName: 'Owner, Dataverse', |
| 283 | + authorAffiliation: 'Dataversedemo.org' |
| 284 | + } |
| 285 | + ], |
| 286 | + datasetContact: [ |
| 287 | + { |
| 288 | + datasetContactEmail: 'finch@mailinator.com', |
| 289 | + datasetContactName: 'Finch, Fiona' |
| 290 | + } |
| 291 | + ], |
| 292 | + dsDescription: [ |
| 293 | + { |
| 294 | + dsDescriptionValue: 'This is the description of the dataset.' |
| 295 | + } |
| 296 | + ], |
| 297 | + subject: ['Medicine, Health and Life Sciences'] |
| 298 | + } |
| 299 | + } |
| 300 | + ] |
| 301 | + } |
| 302 | + expect.assertions(1) |
| 303 | + let writeError: WriteError | undefined = undefined |
| 304 | + try { |
| 305 | + const invalidDatasetType = 'doesNotExist' |
| 306 | + await createDataset.execute(testNewDataset, ':root', invalidDatasetType) |
| 307 | + throw new Error('Use case should throw an error') |
| 308 | + } catch (error) { |
| 309 | + writeError = error as WriteError |
| 310 | + } finally { |
| 311 | + expect(writeError?.message).toEqual( |
| 312 | + 'There was an error when writing the resource. Reason was: [400] Error parsing Json: Invalid dataset type: doesNotExist' |
| 313 | + ) |
| 314 | + } |
| 315 | + }) |
216 | 316 | }) |
0 commit comments