Skip to content
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

Update GetDatasetFiles and GetDatasetFileCounts use cases with the latest API changes #91

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface File {
datasetVersionId?: number;
categories?: string[];
contentType: string;
friendlyType: string;
embargo?: FileEmbargo;
storageIdentifier?: string;
originalFormat?: string;
Expand All @@ -27,6 +28,9 @@ export interface File {
tabularTags?: string[];
creationDate?: Date;
publicationDate?: Date;
deleted: boolean;
tabularData: boolean;
fileAccessRequest?: boolean;
}

export interface FileEmbargo {
Expand Down
7 changes: 6 additions & 1 deletion src/files/domain/repositories/IFilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ export interface IFilesRepository {
getDatasetFiles(
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
): Promise<File[]>;

getDatasetFileCounts(datasetId: number | string, datasetVersionId: string): Promise<FileCounts>;
getDatasetFileCounts(
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
): Promise<FileCounts>;

getFileDownloadCount(fileId: number | string): Promise<number>;

Expand Down
3 changes: 2 additions & 1 deletion src/files/domain/useCases/GetDatasetFileCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class GetDatasetFileCounts implements UseCase<FileCounts> {
async execute(
datasetId: number | string,
datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST,
includeDeaccessioned: boolean = false,
): Promise<FileCounts> {
return await this.filesRepository.getDatasetFileCounts(datasetId, datasetVersionId);
return await this.filesRepository.getDatasetFileCounts(datasetId, datasetVersionId, includeDeaccessioned);
}
}
10 changes: 9 additions & 1 deletion src/files/domain/useCases/GetDatasetFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ export class GetDatasetFiles implements UseCase<File[]> {
async execute(
datasetId: number | string,
datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST,
includeDeaccessioned: boolean = false,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
): Promise<File[]> {
return await this.filesRepository.getDatasetFiles(datasetId, datasetVersionId, limit, offset, fileCriteria);
return await this.filesRepository.getDatasetFiles(
datasetId,
datasetVersionId,
includeDeaccessioned,
limit,
offset,
fileCriteria,
);
}
}
15 changes: 13 additions & 2 deletions src/files/infra/repositories/FilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FileCounts } from '../../domain/models/FileCounts';
import { transformFileCountsResponseToFileCounts } from './transformers/fileCountsTransformers';

export interface GetFilesQueryParams {
includeDeaccessioned: boolean;
limit?: number;
offset?: number;
orderCriteria?: string;
Expand All @@ -28,11 +29,14 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
public async getDatasetFiles(
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
): Promise<File[]> {
const queryParams: GetFilesQueryParams = {};
const queryParams: GetFilesQueryParams = {
includeDeaccessioned: includeDeaccessioned,
};
if (limit !== undefined) {
queryParams.limit = limit;
}
Expand All @@ -53,10 +57,17 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
});
}

public async getDatasetFileCounts(datasetId: string | number, datasetVersionId: string): Promise<FileCounts> {
public async getDatasetFileCounts(
datasetId: string | number,
datasetVersionId: string,
includeDeaccessioned: boolean,
): Promise<FileCounts> {
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/files/counts`, datasetId),
true,
{
includeDeaccessioned: includeDeaccessioned,
},
)
.then((response) => transformFileCountsResponseToFileCounts(response))
.catch((error) => {
Expand Down
4 changes: 4 additions & 0 deletions src/files/infra/repositories/transformers/fileTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const transformFilePayloadToFile = (filePayload: any): File => {
...(filePayload.dataFile.datasetVersionId && { datasetVersionId: filePayload.dataFile.datasetVersionId }),
...(filePayload.dataFile.categories && { categories: filePayload.dataFile.categories }),
contentType: filePayload.dataFile.contentType,
friendlyType: filePayload.dataFile.friendlyType,
...(filePayload.dataFile.embargo && { embargo: transformEmbargoPayloadToEmbargo(filePayload.dataFile.embargo) }),
...(filePayload.dataFile.storageIdentifier && { storageIdentifier: filePayload.dataFile.storageIdentifier }),
...(filePayload.dataFile.originalFormat && { originalFormat: filePayload.dataFile.originalFormat }),
Expand All @@ -44,6 +45,9 @@ const transformFilePayloadToFile = (filePayload: any): File => {
...(filePayload.dataFile.tabularTags && { tabularTags: filePayload.dataFile.tabularTags }),
...(filePayload.dataFile.creationDate && { creationDate: new Date(filePayload.dataFile.creationDate) }),
...(filePayload.dataFile.publicationDate && { publicationDate: new Date(filePayload.dataFile.publicationDate) }),
deleted: filePayload.dataFile.deleted,
tabularData: filePayload.dataFile.tabularData,
...(filePayload.dataFile.fileAccessRequest && { fileAccessRequest: filePayload.dataFile.fileAccessRequest }),
};
};

Expand Down
2 changes: 1 addition & 1 deletion test/integration/environment/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ POSTGRES_VERSION=13
DATAVERSE_DB_USER=dataverse
SOLR_VERSION=8.11.1
DATAVERSE_IMAGE_REGISTRY=ghcr.io
DATAVERSE_IMAGE_TAG=9834-files-api-extension-file-counts
DATAVERSE_IMAGE_TAG=9852-files-api-deaccessioned-datasets
DATAVERSE_BOOTSTRAP_TIMEOUT=5m
60 changes: 47 additions & 13 deletions test/integration/files/FilesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ describe('FilesRepository', () => {
fail(`Tests beforeAll(): Error while uploading file ${testTabFile4Name}`);
});
// Categorize one of the uploaded test files
const currentTestFiles = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const currentTestFiles = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
);
const testFile = currentTestFiles[0];
setFileCategoriesViaApi(testFile.id, [testCategoryName])
.then()
Expand All @@ -80,7 +84,7 @@ describe('FilesRepository', () => {

describe('by numeric id', () => {
test('should return all files filtering by dataset id and version id', async () => {
const actual = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const actual = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId, false);
assert.match(actual.length, 4);
assert.match(actual[0].name, testTextFile1Name);
assert.match(actual[1].name, testTextFile2Name);
Expand All @@ -92,6 +96,7 @@ describe('FilesRepository', () => {
const actual = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
3,
3,
undefined,
Expand All @@ -104,6 +109,7 @@ describe('FilesRepository', () => {
let actual = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
undefined,
undefined,
testFileCriteria,
Expand All @@ -118,7 +124,7 @@ describe('FilesRepository', () => {
let error: ReadError = undefined;

const nonExistentTestDatasetId = 100;
await sut.getDatasetFiles(nonExistentTestDatasetId, latestDatasetVersionId).catch((e) => (error = e));
await sut.getDatasetFiles(nonExistentTestDatasetId, latestDatasetVersionId, false).catch((e) => (error = e));

assert.match(
error.message,
Expand All @@ -133,7 +139,7 @@ describe('FilesRepository', () => {
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
);
const actual = await sut.getDatasetFiles(testDataset.persistentId, latestDatasetVersionId);
const actual = await sut.getDatasetFiles(testDataset.persistentId, latestDatasetVersionId, false);
assert.match(actual.length, 4);
assert.match(actual[0].name, testTextFile1Name);
assert.match(actual[1].name, testTextFile2Name);
Expand All @@ -146,7 +152,14 @@ describe('FilesRepository', () => {
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
);
const actual = await sut.getDatasetFiles(testDataset.persistentId, latestDatasetVersionId, 3, 3, undefined);
const actual = await sut.getDatasetFiles(
testDataset.persistentId,
latestDatasetVersionId,
false,
3,
3,
undefined,
);
assert.match(actual.length, 1);
assert.match(actual[0].name, testTabFile4Name);
});
Expand All @@ -159,6 +172,7 @@ describe('FilesRepository', () => {
let actual = await sut.getDatasetFiles(
testDataset.persistentId,
latestDatasetVersionId,
false,
undefined,
undefined,
testFileCriteria,
Expand All @@ -173,7 +187,7 @@ describe('FilesRepository', () => {
let error: ReadError = undefined;

const testWrongPersistentId = 'wrongPersistentId';
await sut.getDatasetFiles(testWrongPersistentId, latestDatasetVersionId).catch((e) => (error = e));
await sut.getDatasetFiles(testWrongPersistentId, latestDatasetVersionId, false).catch((e) => (error = e));

assert.match(
error.message,
Expand Down Expand Up @@ -211,7 +225,11 @@ describe('FilesRepository', () => {
};

test('should return file count filtering by numeric id', async () => {
const actual = await sut.getDatasetFileCounts(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const actual = await sut.getDatasetFileCounts(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
);
assert.match(actual.total, expectedFileCounts.total);
expect(actual.perContentType).to.have.deep.members(expectedFileCounts.perContentType);
expect(actual.perAccessStatus).to.have.deep.members(expectedFileCounts.perAccessStatus);
Expand All @@ -223,7 +241,7 @@ describe('FilesRepository', () => {
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
);
const actual = await sut.getDatasetFileCounts(testDataset.persistentId, latestDatasetVersionId);
const actual = await sut.getDatasetFileCounts(testDataset.persistentId, latestDatasetVersionId, false);
assert.match(actual.total, expectedFileCounts.total);
expect(actual.perContentType).to.have.deep.members(expectedFileCounts.perContentType);
expect(actual.perAccessStatus).to.have.deep.members(expectedFileCounts.perAccessStatus);
Expand All @@ -233,7 +251,11 @@ describe('FilesRepository', () => {

describe('getFileDownloadCount', () => {
test('should return count filtering by file id and version id', async () => {
const currentTestFiles = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const currentTestFiles = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
);
const testFile = currentTestFiles[0];
const actual = await sut.getFileDownloadCount(testFile.id);
assert.match(actual, 0);
Expand All @@ -253,7 +275,11 @@ describe('FilesRepository', () => {

describe('getFileUserPermissions', () => {
test('should return user permissions filtering by file id and version id', async () => {
const currentTestFiles = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const currentTestFiles = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
);
const testFile = currentTestFiles[0];
const actual = await sut.getFileUserPermissions(testFile.id);
assert.match(actual.canDownloadFile, true);
Expand All @@ -274,14 +300,22 @@ describe('FilesRepository', () => {

describe('getFileDataTables', () => {
test('should return data tables filtering by tabular file id and version id', async () => {
const currentTestFiles = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const currentTestFiles = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
);
const testFile = currentTestFiles[3];
const actual = await sut.getFileDataTables(testFile.id);
assert.match(actual[0].varQuantity, 3);
});

test('should return error when file is not tabular and version id', async () => {
const currentTestFiles = await sut.getDatasetFiles(TestConstants.TEST_CREATED_DATASET_ID, latestDatasetVersionId);
const currentTestFiles = await sut.getDatasetFiles(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
);
const testFile = currentTestFiles[0];

let error: ReadError = undefined;
Expand All @@ -301,7 +335,7 @@ describe('FilesRepository', () => {

assert.match(
error.message,
`There was an error when reading the resource. Reason was: [404] File with ID ${nonExistentFiledId} not found.`,
`There was an error when reading the resource. Reason was: [404] File not found for given id.`,
);
});
});
Expand Down
8 changes: 8 additions & 0 deletions test/testHelpers/files/filesHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createFileModel = (): File => {
restricted: false,
latestRestricted: false,
contentType: 'image/png',
friendlyType: 'PNG Image',
storageIdentifier: 'local://18945a85439-9fa52783e5cb',
rootDataFileId: 4,
previousDataFileId: 4,
Expand All @@ -27,6 +28,9 @@ export const createFileModel = (): File => {
type: 'MD5',
value: '29e413e0c881e17314ce8116fed4d1a7',
},
deleted: false,
tabularData: false,
fileAccessRequest: true,
};
};

Expand All @@ -41,6 +45,7 @@ export const createFilePayload = (): any => {
persistentId: '',
filename: 'test',
contentType: 'image/png',
friendlyType: 'PNG Image',
filesize: 127426,
storageIdentifier: 'local://18945a85439-9fa52783e5cb',
restricted: false,
Expand All @@ -58,6 +63,9 @@ export const createFilePayload = (): any => {
type: 'MD5',
value: '29e413e0c881e17314ce8116fed4d1a7',
},
deleted: false,
tabularData: false,
fileAccessRequest: true,
},
};
};
Expand Down
Loading