Skip to content

Commit

Permalink
Merge pull request #104 from IQSS/fix/101-timing-issue-in-filesreposi…
Browse files Browse the repository at this point in the history
…torytestts

101 - Fix timing issue in FileRepository integration tests
  • Loading branch information
GPortas authored Dec 11, 2023
2 parents af43e45 + aeba8ff commit 78dabf6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
23 changes: 4 additions & 19 deletions test/integration/files/FilesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { assert } from 'sinon';
import { expect } from 'chai';
import { TestConstants } from '../../testHelpers/TestConstants';
import { createDatasetViaApi } from '../../testHelpers/datasets/datasetHelper';
import { uploadFileViaApi, setFileCategoriesViaApi } from '../../testHelpers/files/filesHelper';
import { uploadFileViaApi } from '../../testHelpers/files/filesHelper';
import { DatasetsRepository } from '../../../src/datasets/infra/repositories/DatasetsRepository';
import { ReadError } from '../../../src/core/domain/repositories/ReadError';
import { FileSearchCriteria, FileAccessStatus, FileOrderCriteria } from '../../../src/files/domain/models/FileCriteria';
import { DatasetNotNumberedVersion } from '../../../src/datasets';
import { FileCounts } from '../../../src/files/domain/models/FileCounts';
import { FileDownloadSizeMode } from '../../../src';

// TODO: Test skipped once we fix it on https://github.com/IQSS/dataverse-client-javascript/issues/101
xdescribe('FilesRepository', () => {
describe('FilesRepository', () => {
const sut: FilesRepository = new FilesRepository();

const testTextFile1Name = 'test-file-1.txt';
Expand All @@ -35,8 +34,8 @@ xdescribe('FilesRepository', () => {
.catch(() => {
fail('Test beforeAll(): Error while creating test Dataset');
});
// Uploading test file 1
await uploadFileViaApi(TestConstants.TEST_CREATED_DATASET_ID, testTextFile1Name)
// Uploading test file 1 with some categories
await uploadFileViaApi(TestConstants.TEST_CREATED_DATASET_ID, testTextFile1Name, {categories: [testCategoryName]})
.then()
.catch((e) => {
console.log(e);
Expand All @@ -63,20 +62,6 @@ xdescribe('FilesRepository', () => {
console.log(e);
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,
false,
FileOrderCriteria.NAME_AZ,
);
const testFile = currentTestFiles[0];
setFileCategoriesViaApi(testFile.id, [testCategoryName])
.then()
.catch((e) => {
console.log(e);
fail(`Tests beforeAll(): Error while setting file categories to ${testFile.name}`);
});
});

describe('getDatasetFiles', () => {
Expand Down
19 changes: 11 additions & 8 deletions test/testHelpers/files/filesHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import axios, { AxiosResponse } from 'axios';
import { TestConstants } from '../TestConstants';
import { readFile } from 'fs/promises';

interface FileMetadata {
categories?: string[]
}

export const createFileModel = (): File => {
return {
id: 1,
Expand Down Expand Up @@ -70,21 +74,20 @@ export const createFilePayload = (): any => {
};
};

export const uploadFileViaApi = async (datasetId: number, fileName: string): Promise<AxiosResponse> => {
export const uploadFileViaApi = async (datasetId: number, fileName: string, fileMetadata?: FileMetadata): Promise<AxiosResponse> => {
const formData = new FormData();
const file = await readFile(`${__dirname}/${fileName}`);

formData.append('file', new Blob([file]), fileName);

if(fileMetadata){
formData.append('jsonData', JSON.stringify(fileMetadata));
}

return await axios.post(`${TestConstants.TEST_API_URL}/datasets/${datasetId}/add`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
'X-Dataverse-Key': process.env.TEST_API_KEY,
},
});
};

export const setFileCategoriesViaApi = async (fileId: number, fileCategoryNames: string[]): Promise<AxiosResponse> => {
const data = { categories: fileCategoryNames };
return await axios.post(`${TestConstants.TEST_API_URL}/files/${fileId}/metadata/categories`, JSON.stringify(data), {
headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY },
});
};

0 comments on commit 78dabf6

Please sign in to comment.