@@ -28,7 +28,7 @@ import {
2828} from '../../../src/datasets'
2929import { FileModel } from '../../../src/files/domain/models/FileModel'
3030import { FileCounts } from '../../../src/files/domain/models/FileCounts'
31- import { FileDownloadSizeMode } from '../../../src'
31+ import { FileDownloadSizeMode , WriteError } from '../../../src'
3232import {
3333 deaccessionDatasetViaApi ,
3434 publishDatasetViaApi ,
@@ -646,4 +646,83 @@ describe('FilesRepository', () => {
646646 ) . rejects . toThrow ( errorExpected )
647647 } )
648648 } )
649+
650+ describe ( 'deleteFile' , ( ) => {
651+ let deleFileTestDatasetIds : CreatedDatasetIdentifiers
652+ const testTextFile1Name = 'test-file-1.txt'
653+
654+ beforeEach ( async ( ) => {
655+ try {
656+ deleFileTestDatasetIds = await createDataset . execute ( TestConstants . TEST_NEW_DATASET_DTO )
657+ } catch ( error ) {
658+ throw new Error ( 'Tests beforeEach(): Error while creating test dataset' )
659+ }
660+ await uploadFileViaApi ( deleFileTestDatasetIds . numericId , testTextFile1Name ) . catch ( ( ) => {
661+ throw new Error ( `Tests beforeEach(): Error while uploading file ${ testTextFile1Name } ` )
662+ } )
663+ } )
664+
665+ test ( 'should successfully delete a file' , async ( ) => {
666+ const datasetFiles = await sut . getDatasetFiles (
667+ deleFileTestDatasetIds . numericId ,
668+ latestDatasetVersionId ,
669+ false ,
670+ FileOrderCriteria . NAME_AZ
671+ )
672+ await sut . deleteFile ( datasetFiles . files [ 0 ] . id )
673+
674+ const datasetFileCounts = await sut . getDatasetFileCounts (
675+ deleFileTestDatasetIds . numericId ,
676+ latestDatasetVersionId ,
677+ false
678+ )
679+ expect ( datasetFileCounts . total ) . toEqual ( 0 )
680+
681+ await deleteUnpublishedDatasetViaApi ( deleFileTestDatasetIds . numericId )
682+ } )
683+
684+ test ( 'should delete a file from the draft dataset but not from the published dataset' , async ( ) => {
685+ await publishDatasetViaApi ( deleFileTestDatasetIds . numericId ) . catch ( ( ) => {
686+ throw new Error ( 'Error while publishing test Dataset' )
687+ } )
688+
689+ await waitForNoLocks ( deleFileTestDatasetIds . numericId , 10 ) . catch ( ( ) => {
690+ throw new Error ( 'Error while waiting for no locks' )
691+ } )
692+
693+ const datasetFiles = await sut . getDatasetFiles (
694+ deleFileTestDatasetIds . numericId ,
695+ latestDatasetVersionId ,
696+ false ,
697+ FileOrderCriteria . NAME_AZ
698+ )
699+ await sut . deleteFile ( datasetFiles . files [ 0 ] . id )
700+
701+ const datasetFileCounts = await sut . getDatasetFileCounts (
702+ deleFileTestDatasetIds . numericId ,
703+ DatasetNotNumberedVersion . DRAFT ,
704+ false
705+ )
706+
707+ expect ( datasetFileCounts . total ) . toEqual ( 0 )
708+
709+ const publishedDatasetFileCounts = await sut . getDatasetFileCounts (
710+ deleFileTestDatasetIds . numericId ,
711+ DatasetNotNumberedVersion . LATEST_PUBLISHED ,
712+ false
713+ )
714+
715+ expect ( publishedDatasetFileCounts . total ) . toBeGreaterThan ( 0 )
716+
717+ await deletePublishedDatasetViaApi ( deleFileTestDatasetIds . persistentId ) . catch ( ( ) => {
718+ throw new Error ( 'Error while deleting published test Dataset' )
719+ } )
720+ } )
721+
722+ test ( 'should return error when file does not exist' , async ( ) => {
723+ const expectedError = new WriteError ( `[404] File with ID ${ nonExistentFiledId } not found.` )
724+
725+ await expect ( sut . deleteFile ( nonExistentFiledId ) ) . rejects . toThrow ( expectedError )
726+ } )
727+ } )
649728} )
0 commit comments