|
| 1 | +import { |
| 2 | + getDatasetDownloadCount, |
| 3 | + createDataset, |
| 4 | + publishDataset, |
| 5 | + VersionUpdateType |
| 6 | +} from '../../../src/datasets' |
| 7 | +import { ApiConfig, ReadError } from '../../../src' |
| 8 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 9 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 10 | +import { |
| 11 | + deletePublishedDatasetViaApi, |
| 12 | + deleteUnpublishedDatasetViaApi |
| 13 | +} from '../../testHelpers/datasets/datasetHelper' |
| 14 | + |
| 15 | +const testDataset = { |
| 16 | + license: { |
| 17 | + name: 'CC0 1.0', |
| 18 | + uri: 'http://creativecommons.org/publicdomain/zero/1.0', |
| 19 | + iconUri: 'https://licensebuttons.net/p/zero/1.0/88x31.png' |
| 20 | + }, |
| 21 | + metadataBlockValues: [ |
| 22 | + { |
| 23 | + name: 'citation', |
| 24 | + fields: { |
| 25 | + title: 'Dataset for download count testing', |
| 26 | + author: [ |
| 27 | + { |
| 28 | + authorName: 'Test, User', |
| 29 | + authorAffiliation: 'Dataverse.org' |
| 30 | + } |
| 31 | + ], |
| 32 | + datasetContact: [ |
| 33 | + { |
| 34 | + datasetContactEmail: '[email protected]', |
| 35 | + datasetContactName: 'User, Test' |
| 36 | + } |
| 37 | + ], |
| 38 | + dsDescription: [ |
| 39 | + { |
| 40 | + dsDescriptionValue: 'This dataset is used for testing the download count API.' |
| 41 | + } |
| 42 | + ], |
| 43 | + subject: ['Computer Science'] |
| 44 | + } |
| 45 | + } |
| 46 | + ] |
| 47 | +} |
| 48 | + |
| 49 | +describe('execute', () => { |
| 50 | + beforeEach(async () => { |
| 51 | + ApiConfig.init( |
| 52 | + TestConstants.TEST_API_URL, |
| 53 | + DataverseApiAuthMechanism.API_KEY, |
| 54 | + process.env.TEST_API_KEY |
| 55 | + ) |
| 56 | + }) |
| 57 | + |
| 58 | + test('should return download count for a dataset', async () => { |
| 59 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 60 | + |
| 61 | + await publishDataset.execute(createdDatasetIdentifiers.persistentId, VersionUpdateType.MAJOR) |
| 62 | + |
| 63 | + const downloadCount = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId) |
| 64 | + |
| 65 | + expect(downloadCount).toBe(0) |
| 66 | + |
| 67 | + await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId) |
| 68 | + }) |
| 69 | + |
| 70 | + test('should return download count including MDC data', async () => { |
| 71 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 72 | + |
| 73 | + await publishDataset.execute(createdDatasetIdentifiers.persistentId, VersionUpdateType.MAJOR) |
| 74 | + |
| 75 | + const downloadCount = await getDatasetDownloadCount.execute( |
| 76 | + createdDatasetIdentifiers.numericId, |
| 77 | + true |
| 78 | + ) |
| 79 | + |
| 80 | + expect(downloadCount).toBe(0) |
| 81 | + |
| 82 | + await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId) |
| 83 | + }) |
| 84 | + |
| 85 | + test('should throw an error when dataset ID is invalid', async () => { |
| 86 | + await expect(getDatasetDownloadCount.execute(999999)).rejects.toBeInstanceOf(ReadError) |
| 87 | + }) |
| 88 | + |
| 89 | + test('should return zero if dataset has no downloads', async () => { |
| 90 | + const createdDatasetIdentifiers = await createDataset.execute(testDataset) |
| 91 | + |
| 92 | + const downloadCount = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId) |
| 93 | + |
| 94 | + expect(downloadCount).toBe(0) |
| 95 | + |
| 96 | + await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId) |
| 97 | + }) |
| 98 | +}) |
0 commit comments