From e1ed09aaf118192fbf26e21b933b1ead2c98da92 Mon Sep 17 00:00:00 2001 From: MellyGray Date: Tue, 19 Dec 2023 15:36:52 +0100 Subject: [PATCH] refactor: pass datasetVersionNumber as files use cases argument --- .../domain/repositories/FileRepository.ts | 6 +-- .../getFilesCountInfoByDatasetPersistentId.ts | 6 +-- .../useCases/getFilesTotalDownloadSize.ts | 10 +++-- .../FileJSDataverseRepository.ts | 12 +++--- .../dataset/dataset-files/useFiles.tsx | 4 +- .../files/FileMockLoadingRepository.ts | 4 +- src/stories/files/FileMockNoDataRepository.ts | 6 +-- .../files/FileMockNoFiltersRepository.ts | 4 +- src/stories/files/FileMockRepository.ts | 6 +-- .../dataset/domain/models/DatasetMother.ts | 5 ++- .../dataset-files/DatasetFiles.spec.tsx | 4 +- .../dataset/dataset-files/useFiles.spec.tsx | 22 ++-------- .../files/FileJSDataverseRepository.spec.ts | 42 +++++-------------- 13 files changed, 50 insertions(+), 81 deletions(-) diff --git a/src/files/domain/repositories/FileRepository.ts b/src/files/domain/repositories/FileRepository.ts index 8e69a0713..765b1c2d3 100644 --- a/src/files/domain/repositories/FileRepository.ts +++ b/src/files/domain/repositories/FileRepository.ts @@ -4,7 +4,7 @@ import { FileCriteria } from '../models/FileCriteria' import { FilesCountInfo } from '../models/FilesCountInfo' import { FilePaginationInfo } from '../models/FilePaginationInfo' import { FileUserPermissions } from '../models/FileUserPermissions' -import { DatasetVersion } from '../../../dataset/domain/models/Dataset' +import { DatasetVersion, DatasetVersionNumber } from '../../../dataset/domain/models/Dataset' export interface FileRepository { getAllByDatasetPersistentId: ( @@ -15,12 +15,12 @@ export interface FileRepository { ) => Promise getFilesCountInfoByDatasetPersistentId: ( datasetPersistentId: string, - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, criteria: FileCriteria ) => Promise getFilesTotalDownloadSizeByDatasetPersistentId: ( datasetPersistentId: string, - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, criteria?: FileCriteria ) => Promise getUserPermissionsById: (id: number) => Promise diff --git a/src/files/domain/useCases/getFilesCountInfoByDatasetPersistentId.ts b/src/files/domain/useCases/getFilesCountInfoByDatasetPersistentId.ts index 13b5dfda7..897c3030b 100644 --- a/src/files/domain/useCases/getFilesCountInfoByDatasetPersistentId.ts +++ b/src/files/domain/useCases/getFilesCountInfoByDatasetPersistentId.ts @@ -1,16 +1,16 @@ import { FileRepository } from '../repositories/FileRepository' import { FilesCountInfo } from '../models/FilesCountInfo' -import { DatasetVersion } from '../../../dataset/domain/models/Dataset' +import { DatasetVersionNumber } from '../../../dataset/domain/models/Dataset' import { FileCriteria } from '../models/FileCriteria' export async function getFilesCountInfoByDatasetPersistentId( fileRepository: FileRepository, datasetPersistentId: string, - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, criteria: FileCriteria = new FileCriteria() ): Promise { return fileRepository - .getFilesCountInfoByDatasetPersistentId(datasetPersistentId, datasetVersion, criteria) + .getFilesCountInfoByDatasetPersistentId(datasetPersistentId, datasetVersionNumber, criteria) .catch((error: Error) => { throw new Error(error.message) }) diff --git a/src/files/domain/useCases/getFilesTotalDownloadSize.ts b/src/files/domain/useCases/getFilesTotalDownloadSize.ts index c95bb93ba..b69479f09 100644 --- a/src/files/domain/useCases/getFilesTotalDownloadSize.ts +++ b/src/files/domain/useCases/getFilesTotalDownloadSize.ts @@ -1,15 +1,19 @@ import { FileRepository } from '../repositories/FileRepository' -import { DatasetVersion } from '../../../dataset/domain/models/Dataset' +import { DatasetVersionNumber } from '../../../dataset/domain/models/Dataset' import { FileCriteria } from '../models/FileCriteria' export async function getFilesTotalDownloadSize( fileRepository: FileRepository, datasetPersistentId: string, - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, criteria?: FileCriteria ): Promise { return fileRepository - .getFilesTotalDownloadSizeByDatasetPersistentId(datasetPersistentId, datasetVersion, criteria) + .getFilesTotalDownloadSizeByDatasetPersistentId( + datasetPersistentId, + datasetVersionNumber, + criteria + ) .catch((error: Error) => { throw new Error(error.message) }) diff --git a/src/files/infrastructure/FileJSDataverseRepository.ts b/src/files/infrastructure/FileJSDataverseRepository.ts index 3177801a9..ac2e797ba 100644 --- a/src/files/infrastructure/FileJSDataverseRepository.ts +++ b/src/files/infrastructure/FileJSDataverseRepository.ts @@ -18,7 +18,7 @@ import { import { FileCriteria } from '../domain/models/FileCriteria' import { DomainFileMapper } from './mappers/DomainFileMapper' import { JSFileMapper } from './mappers/JSFileMapper' -import { DatasetVersion } from '../../dataset/domain/models/Dataset' +import { DatasetVersion, DatasetVersionNumber } from '../../dataset/domain/models/Dataset' import { File } from '../domain/models/File' import { FileMother } from '../../../tests/component/files/domain/models/FileMother' @@ -38,7 +38,7 @@ export class FileJSDataverseRepository implements FileRepository { return getDatasetFiles .execute( datasetPersistentId, - datasetVersion.toString(), + datasetVersion.number.toString(), includeDeaccessioned, jsPagination.limit, jsPagination.offset, @@ -111,13 +111,13 @@ export class FileJSDataverseRepository implements FileRepository { getFilesCountInfoByDatasetPersistentId( datasetPersistentId: string, - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, criteria: FileCriteria ): Promise { return getDatasetFileCounts .execute( datasetPersistentId, - datasetVersion.toString(), + datasetVersionNumber.toString(), includeDeaccessioned, DomainFileMapper.toJSFileSearchCriteria(criteria) ) @@ -131,13 +131,13 @@ export class FileJSDataverseRepository implements FileRepository { getFilesTotalDownloadSizeByDatasetPersistentId( datasetPersistentId: string, - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, criteria: FileCriteria = new FileCriteria() ): Promise { return getDatasetFilesTotalDownloadSize .execute( datasetPersistentId, - datasetVersion.toString(), + datasetVersionNumber.toString(), FileDownloadSizeMode.ARCHIVAL, DomainFileMapper.toJSFileSearchCriteria(criteria), includeDeaccessioned diff --git a/src/sections/dataset/dataset-files/useFiles.tsx b/src/sections/dataset/dataset-files/useFiles.tsx index a013be21b..40208a177 100644 --- a/src/sections/dataset/dataset-files/useFiles.tsx +++ b/src/sections/dataset/dataset-files/useFiles.tsx @@ -28,7 +28,7 @@ export function useFiles( return getFilesCountInfoByDatasetPersistentId( filesRepository, datasetPersistentId, - datasetVersion, + datasetVersion.number, criteria ) .then((filesCountInfo: FilesCountInfo) => { @@ -88,7 +88,7 @@ export function useFiles( ]) useEffect(() => { - getFilesTotalDownloadSize(filesRepository, datasetPersistentId, datasetVersion, criteria) + getFilesTotalDownloadSize(filesRepository, datasetPersistentId, datasetVersion.number, criteria) .then((filesTotalDownloadSize: number) => { setFilesTotalDownloadSize(filesTotalDownloadSize) }) diff --git a/src/stories/files/FileMockLoadingRepository.ts b/src/stories/files/FileMockLoadingRepository.ts index e7eaf02bb..b57bd0305 100644 --- a/src/stories/files/FileMockLoadingRepository.ts +++ b/src/stories/files/FileMockLoadingRepository.ts @@ -1,7 +1,7 @@ import { FileRepository } from '../../files/domain/repositories/FileRepository' import { FilePreview } from '../../files/domain/models/FilePreview' import { FilesCountInfo } from '../../files/domain/models/FilesCountInfo' -import { DatasetVersion } from '../../dataset/domain/models/Dataset' +import { DatasetVersion, DatasetVersionNumber } from '../../dataset/domain/models/Dataset' import { FileCriteria } from '../../files/domain/models/FileCriteria' import { FileMockRepository } from './FileMockRepository' import { File } from '../../files/domain/models/File' @@ -24,7 +24,7 @@ export class FileMockLoadingRepository extends FileMockRepository implements Fil // eslint-disable-next-line unused-imports/no-unused-vars datasetPersistentId: string, // eslint-disable-next-line unused-imports/no-unused-vars - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, // eslint-disable-next-line unused-imports/no-unused-vars criteria?: FileCriteria ): Promise { diff --git a/src/stories/files/FileMockNoDataRepository.ts b/src/stories/files/FileMockNoDataRepository.ts index 0db3ae4e3..bfd993666 100644 --- a/src/stories/files/FileMockNoDataRepository.ts +++ b/src/stories/files/FileMockNoDataRepository.ts @@ -2,7 +2,7 @@ import { FileRepository } from '../../files/domain/repositories/FileRepository' import { FilePreview } from '../../files/domain/models/FilePreview' import { FilesCountInfo } from '../../files/domain/models/FilesCountInfo' import { FilesCountInfoMother } from '../../../tests/component/files/domain/models/FilesCountInfoMother' -import { DatasetVersion } from '../../dataset/domain/models/Dataset' +import { DatasetVersion, DatasetVersionNumber } from '../../dataset/domain/models/Dataset' import { FileCriteria } from '../../files/domain/models/FileCriteria' import { FileMockRepository } from './FileMockRepository' import { File } from '../../files/domain/models/File' @@ -25,7 +25,7 @@ export class FileMockNoDataRepository extends FileMockRepository implements File // eslint-disable-next-line unused-imports/no-unused-vars datasetPersistentId: string, // eslint-disable-next-line unused-imports/no-unused-vars - datasetVersion: DatasetVersion + datasetVersionNumber: DatasetVersionNumber ): Promise { return new Promise((resolve) => { setTimeout(() => { @@ -38,7 +38,7 @@ export class FileMockNoDataRepository extends FileMockRepository implements File // eslint-disable-next-line unused-imports/no-unused-vars datasetPersistentId: string, // eslint-disable-next-line unused-imports/no-unused-vars - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, // eslint-disable-next-line unused-imports/no-unused-vars criteria?: FileCriteria ): Promise { diff --git a/src/stories/files/FileMockNoFiltersRepository.ts b/src/stories/files/FileMockNoFiltersRepository.ts index cdddea3ef..845aa45d8 100644 --- a/src/stories/files/FileMockNoFiltersRepository.ts +++ b/src/stories/files/FileMockNoFiltersRepository.ts @@ -3,7 +3,7 @@ import { FilePreview } from '../../files/domain/models/FilePreview' import { FilesCountInfo } from '../../files/domain/models/FilesCountInfo' import { FilesCountInfoMother } from '../../../tests/component/files/domain/models/FilesCountInfoMother' import { FilesMockData } from './FileMockData' -import { DatasetVersion } from '../../dataset/domain/models/Dataset' +import { DatasetVersion, DatasetVersionNumber } from '../../dataset/domain/models/Dataset' import { FileCriteria } from '../../files/domain/models/FileCriteria' import { FileMockRepository } from './FileMockRepository' @@ -25,7 +25,7 @@ export class FileMockNoFiltersRepository extends FileMockRepository implements F // eslint-disable-next-line unused-imports/no-unused-vars datasetPersistentId: string, // eslint-disable-next-line unused-imports/no-unused-vars - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, // eslint-disable-next-line unused-imports/no-unused-vars criteria?: FileCriteria ): Promise { diff --git a/src/stories/files/FileMockRepository.ts b/src/stories/files/FileMockRepository.ts index 853c47d46..fb03cfc96 100644 --- a/src/stories/files/FileMockRepository.ts +++ b/src/stories/files/FileMockRepository.ts @@ -6,7 +6,7 @@ import { FilesCountInfoMother } from '../../../tests/component/files/domain/mode import { FilePaginationInfo } from '../../files/domain/models/FilePaginationInfo' import { FileUserPermissionsMother } from '../../../tests/component/files/domain/models/FileUserPermissionsMother' import { FileUserPermissions } from '../../files/domain/models/FileUserPermissions' -import { DatasetVersion } from '../../dataset/domain/models/Dataset' +import { DatasetVersion, DatasetVersionNumber } from '../../dataset/domain/models/Dataset' import { FileCriteria } from '../../files/domain/models/FileCriteria' import { FilePreviewMother } from '../../../tests/component/files/domain/models/FilePreviewMother' import { FileMother } from '../../../tests/component/files/domain/models/FileMother' @@ -30,7 +30,7 @@ export class FileMockRepository implements FileRepository { // eslint-disable-next-line unused-imports/no-unused-vars datasetPersistentId: string, // eslint-disable-next-line unused-imports/no-unused-vars - datasetVersion: DatasetVersion + datasetVersionNumber: DatasetVersionNumber ): Promise { return new Promise((resolve) => { setTimeout(() => { @@ -43,7 +43,7 @@ export class FileMockRepository implements FileRepository { // eslint-disable-next-line unused-imports/no-unused-vars datasetPersistentId: string, // eslint-disable-next-line unused-imports/no-unused-vars - datasetVersion: DatasetVersion, + datasetVersionNumber: DatasetVersionNumber, // eslint-disable-next-line unused-imports/no-unused-vars criteria?: FileCriteria ): Promise { diff --git a/tests/component/dataset/domain/models/DatasetMother.ts b/tests/component/dataset/domain/models/DatasetMother.ts index 1e0b9a338..1ff9a88c8 100644 --- a/tests/component/dataset/domain/models/DatasetMother.ts +++ b/tests/component/dataset/domain/models/DatasetMother.ts @@ -35,13 +35,14 @@ export class DatasetVersionMother { ) } - static createDeaccessioned(): DatasetVersion { + static createDeaccessioned(props?: Partial): DatasetVersion { return this.create({ publishingStatus: DatasetPublishingStatus.DEACCESSIONED, isLatest: false, citation: 'Admin, Dataverse, 2023, "Dataset Title", https://doi.org/10.5072/FK2/BUDNRV, Root, V1 DEACCESSIONED VERSION', - someDatasetVersionHasBeenReleased: true + someDatasetVersionHasBeenReleased: true, + ...props }) } diff --git a/tests/component/sections/dataset/dataset-files/DatasetFiles.spec.tsx b/tests/component/sections/dataset/dataset-files/DatasetFiles.spec.tsx index 5da621756..238fde4c4 100644 --- a/tests/component/sections/dataset/dataset-files/DatasetFiles.spec.tsx +++ b/tests/component/sections/dataset/dataset-files/DatasetFiles.spec.tsx @@ -402,7 +402,7 @@ describe('DatasetFiles', () => { cy.wrap(fileRepository.getFilesCountInfoByDatasetPersistentId).should( 'be.calledWith', datasetPersistentId, - datasetVersion + datasetVersion.number ) }) @@ -545,7 +545,7 @@ describe('DatasetFiles', () => { cy.wrap(fileRepository.getFilesTotalDownloadSizeByDatasetPersistentId).should( 'be.calledWith', datasetPersistentId, - datasetVersion, + datasetVersion.number, new FileCriteria().withFilterByType('image/png') ) }) diff --git a/tests/component/sections/dataset/dataset-files/useFiles.spec.tsx b/tests/component/sections/dataset/dataset-files/useFiles.spec.tsx index b0e21302d..498f1fa44 100644 --- a/tests/component/sections/dataset/dataset-files/useFiles.spec.tsx +++ b/tests/component/sections/dataset/dataset-files/useFiles.spec.tsx @@ -6,27 +6,13 @@ import { FileUserPermissionsMother } from '../../../files/domain/models/FileUser import { FilePermissionsProvider } from '../../../../../src/sections/file/file-permissions/FilePermissionsProvider' import { useState } from 'react' import { FilePaginationInfo } from '../../../../../src/files/domain/models/FilePaginationInfo' -import { - DatasetPublishingStatus, - DatasetVersion -} from '../../../../../src/dataset/domain/models/Dataset' import { FileCriteria, FileSortByOption } from '../../../../../src/files/domain/models/FileCriteria' +import { DatasetVersionMother } from '../../../dataset/domain/models/DatasetMother' const files = FilePreviewMother.createMany(100) const filesCountInfo = FilesCountInfoMother.create({ total: 100 }) const fileRepository: FileRepository = {} as FileRepository -const datasetVersion = new DatasetVersion( - 1, - '', - [], - true, - false, - DatasetPublishingStatus.RELEASED, - '', - 1, - 0, - false -) +const datasetVersion = DatasetVersionMother.createReleased() const FilesTableTestComponent = ({ datasetPersistentId }: { datasetPersistentId: string }) => { const [paginationInfo, setPaginationInfo] = useState(new FilePaginationInfo()) @@ -169,7 +155,7 @@ describe('useFiles', () => { cy.wrap(fileRepository.getFilesCountInfoByDatasetPersistentId).should( 'be.calledOnceWith', 'persistentId', - datasetVersion, + datasetVersion.number, new FileCriteria() ) @@ -179,7 +165,7 @@ describe('useFiles', () => { cy.wrap(fileRepository.getFilesCountInfoByDatasetPersistentId).should( 'be.calledWith', 'persistentId', - datasetVersion, + datasetVersion.number, new FileCriteria().withSortBy(FileSortByOption.NAME_ZA) ) }) diff --git a/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts b/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts index ea7d3107f..774bea907 100644 --- a/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts +++ b/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts @@ -14,10 +14,6 @@ import { import chai from 'chai' import chaiAsPromised from 'chai-as-promised' import { DatasetJSDataverseRepository } from '../../../../src/dataset/infrastructure/repositories/DatasetJSDataverseRepository' -import { - DatasetPublishingStatus, - DatasetVersion -} from '../../../../src/dataset/domain/models/Dataset' import { FilePaginationInfo } from '../../../../src/files/domain/models/FilePaginationInfo' import { FileAccessOption, @@ -28,6 +24,7 @@ import { import { DatasetHelper } from '../../shared/datasets/DatasetHelper' import { FileData, FileHelper } from '../../shared/files/FileHelper' import { FilesCountInfo } from '../../../../src/files/domain/models/FilesCountInfo' +import { DatasetVersionMother } from '../../../component/dataset/domain/models/DatasetMother' chai.use(chaiAsPromised) const expect = chai.expect @@ -144,18 +141,7 @@ describe('File JSDataverse Repository', () => { await fileRepository .getAllByDatasetPersistentId( dataset.persistentId, - new DatasetVersion( - dataset.version.id, - '', - [], - true, - false, - DatasetPublishingStatus.RELEASED, - '', - 1, - 0, - false - ) + DatasetVersionMother.createReleased({ id: dataset.version.id }) ) .then((files) => { const expectedPublishedFile = fileData(files[0].id) @@ -183,18 +169,7 @@ describe('File JSDataverse Repository', () => { await fileRepository .getAllByDatasetPersistentId( dataset.persistentId, - new DatasetVersion( - dataset.version.id, - '', - [], - true, - false, - DatasetPublishingStatus.DEACCESSIONED, - '', - 1, - 0, - false - ) + DatasetVersionMother.createDeaccessioned({ id: dataset.version.id }) ) .then((files) => { const expectedDeaccessionedFile = fileData(files[0].id) @@ -578,7 +553,7 @@ describe('File JSDataverse Repository', () => { await fileRepository .getFilesCountInfoByDatasetPersistentId( dataset.persistentId, - dataset.version, + dataset.version.number, new FileCriteria() ) .then((filesCountInfo) => { @@ -651,7 +626,7 @@ describe('File JSDataverse Repository', () => { await fileRepository .getFilesCountInfoByDatasetPersistentId( dataset.persistentId, - dataset.version, + dataset.version.number, new FileCriteria().withFilterByType('text/csv') ) .then((filesCountInfo) => { @@ -721,7 +696,10 @@ describe('File JSDataverse Repository', () => { }, 0) }) await fileRepository - .getFilesTotalDownloadSizeByDatasetPersistentId(dataset.persistentId, dataset.version) + .getFilesTotalDownloadSizeByDatasetPersistentId( + dataset.persistentId, + dataset.version.number + ) .then((totalDownloadSize) => { expect(totalDownloadSize).to.deep.equal(expectedTotalDownloadSize) }) @@ -766,7 +744,7 @@ describe('File JSDataverse Repository', () => { await fileRepository .getFilesTotalDownloadSizeByDatasetPersistentId( dataset.persistentId, - dataset.version, + dataset.version.number, new FileCriteria().withFilterByType('csv') ) .then((totalDownloadSize) => {