|
| 1 | +import { |
| 2 | + deaccessionDataset, |
| 3 | + DatasetDeaccessionDTO, |
| 4 | + createDataset, |
| 5 | + publishDataset, |
| 6 | + VersionUpdateType |
| 7 | +} from '../../../src/datasets' |
| 8 | +import { ApiConfig, WriteError } from '../../../src' |
| 9 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 10 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 11 | +import { |
| 12 | + waitForNoLocks, |
| 13 | + deletePublishedDatasetViaApi, |
| 14 | + deleteUnpublishedDatasetViaApi |
| 15 | +} from '../../testHelpers/datasets/datasetHelper' |
| 16 | + |
| 17 | +const testDataset = { |
| 18 | + license: { |
| 19 | + name: 'CC0 1.0', |
| 20 | + uri: 'http://creativecommons.org/publicdomain/zero/1.0', |
| 21 | + iconUri: 'https://licensebuttons.net/p/zero/1.0/88x31.png' |
| 22 | + }, |
| 23 | + metadataBlockValues: [ |
| 24 | + { |
| 25 | + name: 'citation', |
| 26 | + fields: { |
| 27 | + title: 'Dataset created using the createDataset use case', |
| 28 | + author: [ |
| 29 | + { |
| 30 | + authorName: 'Admin, Dataverse', |
| 31 | + authorAffiliation: 'Dataverse.org' |
| 32 | + }, |
| 33 | + { |
| 34 | + authorName: 'Owner, Dataverse', |
| 35 | + authorAffiliation: 'Dataversedemo.org' |
| 36 | + } |
| 37 | + ], |
| 38 | + datasetContact: [ |
| 39 | + { |
| 40 | + datasetContactEmail: '[email protected]', |
| 41 | + datasetContactName: 'Finch, Fiona' |
| 42 | + } |
| 43 | + ], |
| 44 | + dsDescription: [ |
| 45 | + { |
| 46 | + dsDescriptionValue: 'This is the description of the dataset.' |
| 47 | + } |
| 48 | + ], |
| 49 | + subject: ['Medicine, Health and Life Sciences'] |
| 50 | + } |
| 51 | + } |
| 52 | + ] |
| 53 | +} |
| 54 | + |
| 55 | +describe('execute', () => { |
| 56 | + beforeEach(async () => { |
| 57 | + ApiConfig.init( |
| 58 | + TestConstants.TEST_API_URL, |
| 59 | + DataverseApiAuthMechanism.API_KEY, |
| 60 | + process.env.TEST_API_KEY |
| 61 | + ) |
| 62 | + }) |
| 63 | + |
| 64 | + test('should deaccession a dataset when required fields are sent', async () => { |
| 65 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 66 | + |
| 67 | + const response = await publishDataset.execute( |
| 68 | + createdDatasetIdentifiers.persistentId, |
| 69 | + VersionUpdateType.MAJOR |
| 70 | + ) |
| 71 | + await waitForNoLocks(createdDatasetIdentifiers.numericId, 10) |
| 72 | + |
| 73 | + expect(response).toBeUndefined() |
| 74 | + |
| 75 | + const testDeaccessionDataset: DatasetDeaccessionDTO = { |
| 76 | + deaccessionReason: 'Description of the deaccession reason.', |
| 77 | + deaccessionForwardURL: 'https://demo.dataverse.org' |
| 78 | + } |
| 79 | + |
| 80 | + const actual = await deaccessionDataset.execute( |
| 81 | + createdDatasetIdentifiers.numericId, |
| 82 | + '1.0', |
| 83 | + testDeaccessionDataset |
| 84 | + ) |
| 85 | + |
| 86 | + expect(actual).toBeUndefined() |
| 87 | + |
| 88 | + await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId) |
| 89 | + }) |
| 90 | + |
| 91 | + test('should throw an error when the dataset id is incorrect', async () => { |
| 92 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 93 | + |
| 94 | + const testDeaccessionDataset: DatasetDeaccessionDTO = { |
| 95 | + deaccessionReason: 'Description of the deaccession reason.', |
| 96 | + deaccessionForwardURL: 'https://demo.dataverse.org' |
| 97 | + } |
| 98 | + |
| 99 | + await expect( |
| 100 | + deaccessionDataset.execute( |
| 101 | + createdDatasetIdentifiers.numericId, |
| 102 | + ':latest-published', |
| 103 | + testDeaccessionDataset |
| 104 | + ) |
| 105 | + ).rejects.toThrow(Error) |
| 106 | + |
| 107 | + await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId) |
| 108 | + }) |
| 109 | + |
| 110 | + test('should not deaccession a dataset when it is not published', async () => { |
| 111 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 112 | + const testDeaccessionDataset: DatasetDeaccessionDTO = { |
| 113 | + deaccessionReason: 'Description of the deaccession reason.' |
| 114 | + } |
| 115 | + |
| 116 | + await expect( |
| 117 | + deaccessionDataset.execute( |
| 118 | + createdDatasetIdentifiers.numericId, |
| 119 | + ':latest-published', |
| 120 | + testDeaccessionDataset |
| 121 | + ) |
| 122 | + ).rejects.toBeInstanceOf(WriteError) |
| 123 | + }) |
| 124 | + |
| 125 | + test('should deaccession a dataset when it is deaccessioned once', async () => { |
| 126 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 127 | + |
| 128 | + const response = await publishDataset.execute( |
| 129 | + createdDatasetIdentifiers.persistentId, |
| 130 | + VersionUpdateType.MAJOR |
| 131 | + ) |
| 132 | + await waitForNoLocks(createdDatasetIdentifiers.numericId, 10) |
| 133 | + |
| 134 | + expect(response).toBeUndefined() |
| 135 | + |
| 136 | + const testDeaccessionDataset: DatasetDeaccessionDTO = { |
| 137 | + deaccessionReason: 'Description of the deaccession reason.', |
| 138 | + deaccessionForwardURL: 'https://demo.dataverse.org' |
| 139 | + } |
| 140 | + |
| 141 | + const actual = await deaccessionDataset.execute( |
| 142 | + createdDatasetIdentifiers.numericId, |
| 143 | + '1.0', |
| 144 | + testDeaccessionDataset |
| 145 | + ) |
| 146 | + |
| 147 | + expect(actual).toBeUndefined() |
| 148 | + |
| 149 | + await expect( |
| 150 | + deaccessionDataset.execute(createdDatasetIdentifiers.numericId, '1.0', testDeaccessionDataset) |
| 151 | + ).rejects.toThrow(Error) |
| 152 | + |
| 153 | + await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId) |
| 154 | + }) |
| 155 | +}) |
0 commit comments