-
Notifications
You must be signed in to change notification settings - Fork 10
Implement updateFileMetadata use case #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ofahimIQSS
merged 14 commits into
develop
from
213-implement-use-case-for-edit-files-metadata-on-file-page
Mar 5, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cdc198c
feat: updateFileMetadata use case
ChengShi-1 6064d60
feat: fix testcase, and add updateFileMetadata to usecases.md
ChengShi-1 78d2719
fix: testcase
ChengShi-1 32b47de
fix: change the order of a testcase
ChengShi-1 6440fbd
feat: update testcase
ChengShi-1 2353a8f
feat: update order of testcase
ChengShi-1 661d81d
feat: update file update metadata test
ChengShi-1 af00b1a
feat: update functional test
ChengShi-1 5c76eb4
Merge branch 'develop' into 213-implement-use-case-for-edit-files-met…
ChengShi-1 b8a8478
fix: update documentation
ChengShi-1 ca9f86c
chore: a typo fix
ChengShi-1 2d18f21
update solr
ChengShi-1 df10ace
Merge branch 'develop' into 213-implement-use-case-for-edit-files-met…
ChengShi-1 1922064
CHORE: fix lint
ChengShi-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export interface UpdateFileMetadataDTO { | ||
| description?: string | ||
| prevFreeform?: string | ||
| categories?: string[] | ||
| dataFileTags?: string[] | ||
| restrict?: boolean | ||
| } | ||
g-saracca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { IFilesRepository } from '../repositories/IFilesRepository' | ||
| import { UpdateFileMetadataDTO } from '../dtos/UpdateFileMetadataDTO' | ||
|
|
||
| export class UpdateFileMetadata implements UseCase<void> { | ||
| private filesRepository: IFilesRepository | ||
|
|
||
| constructor(filesRepository: IFilesRepository) { | ||
| this.filesRepository = filesRepository | ||
| } | ||
|
|
||
| /** | ||
| * Updates the metadata for a particular File. | ||
ChengShi-1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * More detailed information about updating a file's metadata behavior can be found in https://guides.dataverse.org/en/latest/api/native-api.html#updating-file-metadata | ||
| * | ||
| * @param {number | string} [fileId] - The file identifier, which can be a string (for persistent identifiers), or a number (for numeric identifiers). | ||
| * @param {UpdateFileMetadataDTO} [updateFileMetadataDTO] - The DTO containing the metadata updates. | ||
| * @returns {Promise<void>} | ||
| */ | ||
| async execute( | ||
| fileId: number | string, | ||
| updateFileMetadataDTO: UpdateFileMetadataDTO | ||
| ): Promise<void> { | ||
| await this.filesRepository.updateFileMetadata(fileId, updateFileMetadataDTO) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { | ||
| ApiConfig, | ||
| createDataset, | ||
| CreatedDatasetIdentifiers, | ||
| WriteError, | ||
| updateFileMetadata, | ||
| getFile, | ||
| DatasetNotNumberedVersion, | ||
| getDatasetFiles | ||
| } from '../../../src' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { | ||
| createCollectionViaApi, | ||
| deleteCollectionViaApi | ||
| } from '../../testHelpers/collections/collectionHelper' | ||
| import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' | ||
| import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { UpdateFileMetadataDTO } from '../../../src/files/domain/dtos/UpdateFileMetadataDTO' | ||
| import { FileModel } from '../../../src/files/domain/models/FileModel' | ||
|
|
||
| describe('execute', () => { | ||
| const testCollectionAlias = 'updateFileMetadatFunctionalTest' | ||
| let testDatasetIds: CreatedDatasetIdentifiers | ||
| const testTextFile1Name = 'test-file-1.txt' | ||
| const metadataUpdate: UpdateFileMetadataDTO = { | ||
| description: 'This is a test file', | ||
| categories: ['file'], | ||
| restrict: true | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| await createCollectionViaApi(testCollectionAlias) | ||
|
|
||
| try { | ||
| testDatasetIds = await createDataset.execute( | ||
| TestConstants.TEST_NEW_DATASET_DTO, | ||
| testCollectionAlias | ||
| ) | ||
| } catch (error) { | ||
| throw new Error('Tests beforeAll(): Error while creating test dataset') | ||
| } | ||
|
|
||
| await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => { | ||
| throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`) | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| try { | ||
| await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) | ||
| } catch (error) { | ||
| throw new Error('Tests afterAll(): Error while deleting test dataset') | ||
| } | ||
|
|
||
| try { | ||
| await deleteCollectionViaApi(testCollectionAlias) | ||
| } catch (error) { | ||
| throw new Error('Tests afterAll(): Error while deleting test collection') | ||
| } | ||
| }) | ||
|
|
||
| test('should successfully update metadata of a file', async () => { | ||
| const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) | ||
| const fileId = datasetFiles.files[0].id | ||
|
|
||
| try { | ||
| await updateFileMetadata.execute(fileId, metadataUpdate) | ||
| } catch (error) { | ||
| throw new Error('File metadata should be updated') | ||
| } finally { | ||
| const fileInfo: FileModel = (await getFile.execute( | ||
| fileId, | ||
| DatasetNotNumberedVersion.LATEST | ||
| )) as FileModel | ||
|
|
||
| expect(fileInfo.description).toEqual(metadataUpdate.description) | ||
| expect(fileInfo.categories).toEqual(metadataUpdate.categories) | ||
| expect(fileInfo.restricted).toEqual(metadataUpdate.restrict) | ||
| } | ||
| }) | ||
|
|
||
| test('should throw an error when the file id does not exist', async () => { | ||
| let writeError: WriteError | undefined = undefined | ||
| const nonExistentFileId = 5 | ||
|
|
||
| try { | ||
| await updateFileMetadata.execute(nonExistentFileId, metadataUpdate) | ||
| throw new Error('Use case should throw an error') | ||
| } catch (error) { | ||
| writeError = error as WriteError | ||
| } finally { | ||
| expect(writeError).toBeInstanceOf(WriteError) | ||
| expect(writeError?.message).toEqual( | ||
| `There was an error when writing the resource. Reason was: [400] Error attempting get the requested data file.` | ||
| ) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { UpdateFileMetadata } from '../../../src/files/domain/useCases/UpdateFileMetadata' | ||
| import { IFilesRepository } from '../../../src/files/domain/repositories/IFilesRepository' | ||
| import { WriteError } from '../../../src/core/domain/repositories/WriteError' | ||
| import { createFileMetadataWithCategories } from '../../testHelpers/files/filesHelper' | ||
|
|
||
| describe('UpdateFileMetadata', () => { | ||
| const testFileMetadata = createFileMetadataWithCategories() | ||
| test('should updated file metadata with correct parameters and id', async () => { | ||
| const filesRepositoryStub: IFilesRepository = {} as IFilesRepository | ||
| filesRepositoryStub.updateFileMetadata = jest.fn().mockResolvedValue(testFileMetadata) | ||
|
|
||
| const sut = new UpdateFileMetadata(filesRepositoryStub) | ||
|
|
||
| await sut.execute(1, testFileMetadata) | ||
|
|
||
| expect(filesRepositoryStub.updateFileMetadata).toHaveBeenCalledWith(1, testFileMetadata) | ||
| expect(filesRepositoryStub.updateFileMetadata).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('should return the updated file metadata with correct parameters and persisten Id', async () => { | ||
| const filesRepositoryStub: IFilesRepository = { | ||
| updateFileMetadata: jest.fn().mockResolvedValue(testFileMetadata) | ||
| } as unknown as IFilesRepository | ||
|
|
||
| const sut = new UpdateFileMetadata(filesRepositoryStub) | ||
|
|
||
| await sut.execute('doi:10.5072/FK2/HC6KTB', testFileMetadata) | ||
|
|
||
| expect(filesRepositoryStub.updateFileMetadata).toHaveBeenCalledWith( | ||
| 'doi:10.5072/FK2/HC6KTB', | ||
| testFileMetadata | ||
| ) | ||
| expect(filesRepositoryStub.updateFileMetadata).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('should throw an error if the repository throws an error', async () => { | ||
| const filesRepositoryStub: IFilesRepository = { | ||
| updateFileMetadata: jest.fn().mockRejectedValue(new WriteError()) | ||
| } as unknown as IFilesRepository | ||
|
|
||
| const sut = new UpdateFileMetadata(filesRepositoryStub) | ||
|
|
||
| await expect(sut.execute(1, testFileMetadata)).rejects.toThrow(WriteError) | ||
| expect(filesRepositoryStub.updateFileMetadata).toHaveBeenCalledWith(1, testFileMetadata) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.