-
Notifications
You must be signed in to change notification settings - Fork 10
245 delete file use case #253
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26da8e6
refactor: allow undefined operation for deleting file by persistent id
g-saracca 84c923c
feat: add use case
g-saracca 524aede
docs: use case documentation
g-saracca 53b1704
test: add unit, integration and functional tests
g-saracca f59f53b
remove logs and add doc line
g-saracca 2d585b0
doc: extra doc from native api guide
g-saracca 9646936
test: add filesRepository cases
g-saracca 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
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,17 @@ | ||
| import { IFilesRepository } from '../repositories/IFilesRepository' | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
|
|
||
| export class DeleteFile implements UseCase<void> { | ||
| constructor(private readonly filesRepository: IFilesRepository) {} | ||
|
|
||
| /** | ||
| * Deletes a file. | ||
| * More detailed information about the file deletion behavior can be found in https://guides.dataverse.org/en/latest/api/native-api.html#deleting-files | ||
| * | ||
| * @param {number | string} [fileId] - The File identifier, which can be a string (for persistent identifiers), or a number (for numeric identifiers). | ||
| * @returns {Promise<void>} -This method does not return anything upon successful completion. | ||
| */ | ||
| async execute(fileId: number | string): Promise<void> { | ||
| return await this.filesRepository.deleteFile(fileId) | ||
| } | ||
| } |
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,89 @@ | ||
| import { | ||
| ApiConfig, | ||
| createDataset, | ||
| CreatedDatasetIdentifiers, | ||
| deleteFile, | ||
| getDatasetFileCounts, | ||
| getDatasetFiles, | ||
| WriteError | ||
| } 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' | ||
|
|
||
| describe('execute', () => { | ||
| const testCollectionAlias = 'deleteFileFunctionalTest' | ||
| let testDatasetIds: CreatedDatasetIdentifiers | ||
| const testTextFile1Name = 'test-file-1.txt' | ||
|
|
||
| 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 delete a file', async () => { | ||
| try { | ||
| const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) | ||
|
|
||
| await deleteFile.execute(datasetFiles.files[0].id) | ||
| } catch (error) { | ||
| throw new Error('File should be deleted') | ||
| } finally { | ||
| const datasetFileCounts = await getDatasetFileCounts.execute(testDatasetIds.numericId) | ||
|
|
||
| expect(datasetFileCounts.total).toEqual(0) | ||
| } | ||
| }) | ||
|
|
||
| test('should throw an error when the file id does not exist', async () => { | ||
| expect.assertions(2) | ||
| let writeError: WriteError | undefined = undefined | ||
| const nonExistentFileId = 5 | ||
|
|
||
| try { | ||
| await deleteFile.execute(nonExistentFileId) | ||
| 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: [404] File with ID ${nonExistentFileId} not found.` | ||
| ) | ||
| } | ||
| }) | ||
| }) |
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,25 @@ | ||
| import { IFilesRepository } from '../../../src/files/domain/repositories/IFilesRepository' | ||
| import { WriteError } from '../../../src' | ||
| import { DeleteFile } from '../../../src/files/domain/useCases/DeleteFile' | ||
|
|
||
| describe('execute', () => { | ||
| test('should return undefined when repository call is successful', async () => { | ||
| const filesRepositoryStub: IFilesRepository = {} as IFilesRepository | ||
| filesRepositoryStub.deleteFile = jest.fn().mockResolvedValue(undefined) | ||
|
|
||
| const sut = new DeleteFile(filesRepositoryStub) | ||
|
|
||
| const actual = await sut.execute(1) | ||
|
|
||
| expect(actual).toEqual(undefined) | ||
| }) | ||
|
|
||
| test('should return error result on repository error', async () => { | ||
| const filesRepositoryStub: IFilesRepository = {} as IFilesRepository | ||
| filesRepositoryStub.deleteFile = jest.fn().mockRejectedValue(new WriteError()) | ||
|
|
||
| const sut = new DeleteFile(filesRepositoryStub) | ||
|
|
||
| await expect(sut.execute(1)).rejects.toThrow(WriteError) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.