Skip to content

Commit

Permalink
Added: FileSearchCriteria added to GetDatasetFileCounts use case
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Oct 10, 2023
1 parent 498358c commit 9929566
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/files/domain/repositories/IFilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IFilesRepository {
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
fileSearchCriteria?: FileSearchCriteria,
): Promise<FileCounts>;

getDatasetFilesTotalDownloadSize(
Expand Down
9 changes: 8 additions & 1 deletion src/files/domain/useCases/GetDatasetFileCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IFilesRepository } from '../repositories/IFilesRepository';
import { DatasetNotNumberedVersion } from '../../../datasets';
import { FileCounts } from '../models/FileCounts';
import { FileSearchCriteria } from '../models/FileCriteria';

export class GetDatasetFileCounts implements UseCase<FileCounts> {
private filesRepository: IFilesRepository;
Expand All @@ -14,7 +15,13 @@ export class GetDatasetFileCounts implements UseCase<FileCounts> {
datasetId: number | string,
datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST,
includeDeaccessioned: boolean = false,
fileSearchCriteria?: FileSearchCriteria,
): Promise<FileCounts> {
return await this.filesRepository.getDatasetFileCounts(datasetId, datasetVersionId, includeDeaccessioned);
return await this.filesRepository.getDatasetFileCounts(
datasetId,
datasetVersionId,
includeDeaccessioned,
fileSearchCriteria,
);
}
}
11 changes: 8 additions & 3 deletions src/files/infra/repositories/FilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
datasetId: string | number,
datasetVersionId: string,
includeDeaccessioned: boolean,
fileSearchCriteria?: FileSearchCriteria,
): Promise<FileCounts> {
const queryParams: GetFilesQueryParams = {
includeDeaccessioned: includeDeaccessioned,
};
if (fileSearchCriteria !== undefined) {
this.applyFileSearchCriteriaToQueryParams(queryParams, fileSearchCriteria);
}
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/files/counts`, datasetId),
true,
{
includeDeaccessioned: includeDeaccessioned,
},
queryParams,
)
.then((response) => transformFileCountsResponseToFileCounts(response))
.catch((error) => {
Expand Down
35 changes: 35 additions & 0 deletions test/integration/files/FilesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,41 @@ describe('FilesRepository', () => {
expect(actual.perCategoryName).to.have.deep.members(expectedFileCounts.perCategoryName);
});

test('should return file count filtering by numeric id and applying criteria', async () => {
const expectedFileCountsForCriteria: FileCounts = {
total: 1,
perContentType: [
{
contentType: 'text/plain',
count: 1,
},
],
perAccessStatus: [
{
accessStatus: FileAccessStatus.PUBLIC,
count: 1,
},
],
perCategoryName: [
{
categoryName: testCategoryName,
count: 1,
},
],
};
const testCriteria = new FileSearchCriteria().withCategoryName(testCategoryName);
const actual = await sut.getDatasetFileCounts(
TestConstants.TEST_CREATED_DATASET_ID,
latestDatasetVersionId,
false,
testCriteria,
);
assert.match(actual.total, expectedFileCountsForCriteria.total);
expect(actual.perContentType).to.have.deep.members(expectedFileCountsForCriteria.perContentType);
expect(actual.perAccessStatus).to.have.deep.members(expectedFileCountsForCriteria.perAccessStatus);
expect(actual.perCategoryName).to.have.deep.members(expectedFileCountsForCriteria.perCategoryName);
});

test('should return file count filtering by persistent id', async () => {
const testDataset = await datasetRepository.getDataset(
TestConstants.TEST_CREATED_DATASET_ID,
Expand Down
43 changes: 37 additions & 6 deletions test/unit/files/FilesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('FilesRepository', () => {
const testDatasetVersionId = DatasetNotNumberedVersion.LATEST;
const testDatasetId = 1;
const testIncludeDeaccessioned = false;
const testCategory = 'testCategory';
const testContentType = 'testContentType';
const testFileCriteria = new FileSearchCriteria()
.withCategoryName(testCategory)
.withContentType(testContentType)
.withAccessStatus(FileAccessStatus.PUBLIC);

beforeEach(() => {
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, TestConstants.TEST_DUMMY_API_KEY);
Expand All @@ -41,12 +47,6 @@ describe('FilesRepository', () => {

const testLimit = 10;
const testOffset = 20;
const testCategory = 'testCategory';
const testContentType = 'testContentType';
const testFileCriteria = new FileSearchCriteria()
.withCategoryName(testCategory)
.withContentType(testContentType)
.withAccessStatus(FileAccessStatus.PUBLIC);
const testFileOrderCriteria = FileOrderCriteria.NAME_ZA;

const expectedRequestConfigApiKey = {
Expand Down Expand Up @@ -236,6 +236,18 @@ describe('FilesRepository', () => {
headers: TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE.headers,
};

const expectedRequestParamsWithOptional = {
includeDeaccessioned: testIncludeDeaccessioned,
contentType: testFileCriteria.contentType,
accessStatus: testFileCriteria.accessStatus.toString(),
categoryName: testFileCriteria.categoryName,
};

const expectedRequestConfigApiKeyWithOptional = {
params: expectedRequestParamsWithOptional,
headers: TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY.headers,
};

const expectedCount = createFileCountsModel();

describe('by numeric id and version id', () => {
Expand All @@ -258,6 +270,25 @@ describe('FilesRepository', () => {
assert.match(actual, expectedCount);
});

test('should return file counts when providing id, version id, optional params, and response is successful', async () => {
const axiosGetStub = sandbox.stub(axios, 'get').resolves(testFileCountsSuccessfulResponse);
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/datasets/${testDatasetId}/versions/${testDatasetVersionId}/files/counts`;

const actual = await sut.getDatasetFileCounts(
testDatasetId,
testDatasetVersionId,
testIncludeDeaccessioned,
testFileCriteria,
);

assert.calledWithExactly(
axiosGetStub,
expectedApiEndpoint,
expectedRequestConfigApiKeyWithOptional,
);
assert.match(actual, expectedCount);
});

test('should return error result on error response', async () => {
const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE);

Expand Down
2 changes: 1 addition & 1 deletion test/unit/files/GetDatasetFileCounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('execute', () => {
const actual = await sut.execute(1);

assert.match(actual, testFileCounts);
assert.calledWithExactly(getDatasetFileCountsStub, 1, DatasetNotNumberedVersion.LATEST, false);
assert.calledWithExactly(getDatasetFileCountsStub, 1, DatasetNotNumberedVersion.LATEST, false, undefined);
});

test('should return error result on repository error', async () => {
Expand Down

0 comments on commit 9929566

Please sign in to comment.