|
| 1 | +import { |
| 2 | + ApiConfig, |
| 3 | + createDataset, |
| 4 | + CreatedDatasetIdentifiers, |
| 5 | + WriteError, |
| 6 | + updateFileCategories, |
| 7 | + getFile, |
| 8 | + DatasetNotNumberedVersion, |
| 9 | + getDatasetFiles |
| 10 | +} from '../../../src' |
| 11 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 12 | +import { |
| 13 | + createCollectionViaApi, |
| 14 | + deleteCollectionViaApi |
| 15 | +} from '../../testHelpers/collections/collectionHelper' |
| 16 | +import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' |
| 17 | +import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' |
| 18 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 19 | +import { FileModel } from '../../../src/files/domain/models/FileModel' |
| 20 | + |
| 21 | +describe('execute', () => { |
| 22 | + const testCollectionAlias = 'updateFileMetadataFunctionalTest-categories' |
| 23 | + let testDatasetIds: CreatedDatasetIdentifiers |
| 24 | + const testTextFile1Name = 'test-file-1.txt' |
| 25 | + const metadataUpdate = ['file'] |
| 26 | + |
| 27 | + beforeAll(async () => { |
| 28 | + ApiConfig.init( |
| 29 | + TestConstants.TEST_API_URL, |
| 30 | + DataverseApiAuthMechanism.API_KEY, |
| 31 | + process.env.TEST_API_KEY |
| 32 | + ) |
| 33 | + await createCollectionViaApi(testCollectionAlias) |
| 34 | + |
| 35 | + try { |
| 36 | + testDatasetIds = await createDataset.execute( |
| 37 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 38 | + testCollectionAlias |
| 39 | + ) |
| 40 | + } catch (error) { |
| 41 | + throw new Error('Tests beforeAll(): Error while creating test dataset') |
| 42 | + } |
| 43 | + |
| 44 | + await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => { |
| 45 | + throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + afterAll(async () => { |
| 50 | + try { |
| 51 | + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) |
| 52 | + } catch (error) { |
| 53 | + throw new Error('Tests afterAll(): Error while deleting test dataset') |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + await deleteCollectionViaApi(testCollectionAlias) |
| 58 | + } catch (error) { |
| 59 | + throw new Error('Tests afterAll(): Error while deleting test collection') |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + test('should successfully update categories of a file', async () => { |
| 64 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 65 | + const fileId = datasetFiles.files[0].id |
| 66 | + |
| 67 | + try { |
| 68 | + await updateFileCategories.execute(fileId, metadataUpdate) |
| 69 | + } catch (error) { |
| 70 | + throw new Error('File metadata should be updated') |
| 71 | + } finally { |
| 72 | + const fileInfo: FileModel = (await getFile.execute( |
| 73 | + fileId, |
| 74 | + DatasetNotNumberedVersion.LATEST |
| 75 | + )) as FileModel |
| 76 | + |
| 77 | + expect(fileInfo.categories).toEqual(metadataUpdate) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + test('should successfully update categories of a file with replace parameter', async () => { |
| 82 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 83 | + const fileId = datasetFiles.files[0].id |
| 84 | + const newCategories = ['new Category'] |
| 85 | + try { |
| 86 | + await updateFileCategories.execute(fileId, newCategories, true) |
| 87 | + } catch (error) { |
| 88 | + throw new Error('File metadata should be updated') |
| 89 | + } finally { |
| 90 | + const fileInfo: FileModel = (await getFile.execute( |
| 91 | + fileId, |
| 92 | + DatasetNotNumberedVersion.LATEST |
| 93 | + )) as FileModel |
| 94 | + |
| 95 | + expect(fileInfo.categories).toEqual(newCategories) |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + test('should not duplicate categories when merging', async () => { |
| 100 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 101 | + const fileId = datasetFiles.files[0].id |
| 102 | + |
| 103 | + const initialCategories = ['Category 1', 'Category 2'] |
| 104 | + const newCategories = ['Category 2', 'Category 3'] |
| 105 | + const expectedMergedCategories = ['Category 1', 'Category 2', 'Category 3'] |
| 106 | + |
| 107 | + await updateFileCategories.execute(fileId, initialCategories, true) |
| 108 | + await updateFileCategories.execute(fileId, newCategories, false) |
| 109 | + |
| 110 | + const fileInfo = (await getFile.execute(fileId, DatasetNotNumberedVersion.LATEST)) as FileModel |
| 111 | + |
| 112 | + expect(fileInfo.categories?.sort()).toEqual(expectedMergedCategories.sort()) |
| 113 | + }) |
| 114 | + |
| 115 | + test('should replace categories when replace = true', async () => { |
| 116 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 117 | + const fileId = datasetFiles.files[0].id |
| 118 | + |
| 119 | + const initialCategories = ['Category 1', 'Category 2', 'Category 3'] |
| 120 | + const newCategories = ['Category 4', 'Category 5', 'Category 6'] |
| 121 | + |
| 122 | + await updateFileCategories.execute(fileId, initialCategories, true) |
| 123 | + await updateFileCategories.execute(fileId, newCategories, true) |
| 124 | + |
| 125 | + const fileInfo = (await getFile.execute(fileId, DatasetNotNumberedVersion.LATEST)) as FileModel |
| 126 | + |
| 127 | + expect(fileInfo.categories?.sort()).toEqual(newCategories.sort()) |
| 128 | + }) |
| 129 | + |
| 130 | + test('should throw an error when the file id does not exist', async () => { |
| 131 | + let writeError: WriteError | undefined = undefined |
| 132 | + const nonExistentFileId = 5 |
| 133 | + |
| 134 | + try { |
| 135 | + await updateFileCategories.execute(nonExistentFileId, metadataUpdate) |
| 136 | + throw new Error('Use case should throw an error') |
| 137 | + } catch (error) { |
| 138 | + writeError = error as WriteError |
| 139 | + } finally { |
| 140 | + expect(writeError).toBeInstanceOf(WriteError) |
| 141 | + expect(writeError?.message).toEqual( |
| 142 | + `There was an error when writing the resource. Reason was: [404] File with ID ${nonExistentFileId} not found.` |
| 143 | + ) |
| 144 | + } |
| 145 | + }) |
| 146 | +}) |
0 commit comments