Skip to content

Commit fa0684a

Browse files
committed
feat: add more testcases
1 parent 783f8ad commit fa0684a

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

docs/useCases.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,8 +1336,8 @@ const updateFileMetadataDTO = {
13361336
restrict: false
13371337
}
13381338

1339-
await updateFileMetadata.execute(fileId, updateFileMetadataDTO).then((fileId) => {
1340-
console.log(`File updated successfully with file ID: ${fileId}`)
1339+
await updateFileMetadata.execute(fileId, updateFileMetadataDTO).then(() => {
1340+
console.log(`File updated successfully`)
13411341
})
13421342
```
13431343

@@ -1360,8 +1360,8 @@ const fileId: number | string = 123
13601360
const categories = ['category 1', 'category 1']
13611361
const replace = true
13621362

1363-
await updateFileCategories.execute(fileId, categories, replace).then((fileId) => {
1364-
console.log(`File updated successfully with file ID: ${fileId}`)
1363+
await updateFileCategories.execute(fileId, categories, replace).then(() => {
1364+
console.log(`File updated successfully`)
13651365
})
13661366
```
13671367

@@ -1383,8 +1383,8 @@ import { updateFileTabularTags } from '@iqss/dataverse-client-javascript'
13831383
const fileId: number | string = 123
13841384
const tabularTags = ['Surveys']
13851385

1386-
await updateFileTabularTags.execute(fileId, tabularTags, replace).then((fileId) => {
1387-
console.log(`File updated successfully with file ID: ${fileId}`)
1386+
await updateFileTabularTags.execute(fileId, tabularTags, replace).then(() => {
1387+
console.log(`File updated successfully`)
13881388
})
13891389
```
13901390

test/functional/files/UpdateFileCategories.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ describe('execute', () => {
9696
}
9797
})
9898

99+
test('should not duplicate categories when merging', async () => {
100+
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId)
101+
const fileId = datasetFiles.files[0].id
102+
103+
const initialCategories = ['Category 1', 'Category 2']
104+
const newCategories = ['Category 2', 'Category 3']
105+
const expectedMergedCategories = ['Category 1', 'Category 2', 'Category 3']
106+
107+
await updateFileCategories.execute(fileId, initialCategories, true)
108+
await updateFileCategories.execute(fileId, newCategories, false)
109+
110+
const fileInfo = (await getFile.execute(fileId, DatasetNotNumberedVersion.LATEST)) as FileModel
111+
112+
expect(fileInfo.categories?.sort()).toEqual(expectedMergedCategories.sort())
113+
})
114+
115+
test('should replace categories when replace = true', async () => {
116+
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId)
117+
const fileId = datasetFiles.files[0].id
118+
119+
const initialCategories = ['Category 1', 'Category 2', 'Category 3']
120+
const newCategories = ['Category 4', 'Category 5', 'Category 6']
121+
122+
await updateFileCategories.execute(fileId, initialCategories, true)
123+
await updateFileCategories.execute(fileId, newCategories, true)
124+
125+
const fileInfo = (await getFile.execute(fileId, DatasetNotNumberedVersion.LATEST)) as FileModel
126+
127+
expect(fileInfo.categories?.sort()).toEqual(newCategories.sort())
128+
})
129+
99130
test('should throw an error when the file id does not exist', async () => {
100131
let writeError: WriteError | undefined = undefined
101132
const nonExistentFileId = 5

test/functional/files/UpdateFileTabularTags.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,67 @@ describe('execute', () => {
9797
}
9898
})
9999

100+
test('should not duplicate tabular tags when merging', async () => {
101+
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId)
102+
const fileId = datasetFiles.files[0].id
103+
104+
const initialTags = ['Survey', 'Panel']
105+
const newTags = ['Panel', 'Event']
106+
const expectedMergedTags = ['Survey', 'Panel', 'Event']
107+
108+
await updateFileTabularTags.execute(fileId, initialTags, true)
109+
await updateFileTabularTags.execute(fileId, newTags, false)
110+
111+
const fileInfo = (await getFile.execute(fileId, DatasetNotNumberedVersion.LATEST)) as FileModel
112+
113+
expect(fileInfo.tabularTags?.sort()).toEqual(expectedMergedTags.sort())
114+
})
115+
116+
test('should replace tabular tags when replace = true', async () => {
117+
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId)
118+
const fileId = datasetFiles.files[0].id
119+
120+
const initialTags = ['Survey', 'Panel', 'Event']
121+
const newTags = ['Survey', 'Network']
122+
123+
await updateFileTabularTags.execute(fileId, initialTags, true)
124+
await updateFileTabularTags.execute(fileId, newTags, true)
125+
126+
const fileInfo = (await getFile.execute(fileId, DatasetNotNumberedVersion.LATEST)) as FileModel
127+
128+
expect(fileInfo.tabularTags?.sort()).toEqual(newTags.sort())
129+
})
130+
131+
test('should throw an error when updating tabular tags on a non-tabular file', async () => {
132+
const nonTabularFileName = 'test-file-1.txt'
133+
134+
try {
135+
await uploadFileViaApi(testDatasetIds.numericId, nonTabularFileName)
136+
} catch {
137+
throw new Error(`Error uploading non-tabular file: ${nonTabularFileName}`)
138+
}
139+
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId)
140+
const matchingFile = datasetFiles.files.find((f) => f.name === nonTabularFileName)
141+
if (!matchingFile) {
142+
throw new Error('Uploaded non-tabular file not found in dataset')
143+
}
144+
145+
const fileId = matchingFile.id
146+
147+
let writeError: WriteError | undefined = undefined
148+
149+
try {
150+
await updateFileTabularTags.execute(fileId, ['Survey'], false)
151+
} catch (error) {
152+
writeError = error as WriteError
153+
} finally {
154+
expect(writeError).toBeInstanceOf(WriteError)
155+
expect(writeError?.message).toEqual(
156+
`There was an error when writing the resource. Reason was: [400] This operation is only available for tabular files.`
157+
)
158+
}
159+
})
160+
100161
test('should throw an error when the file id does not exist', async () => {
101162
let writeError: WriteError | undefined = undefined
102163
const nonExistentFileId = 5

0 commit comments

Comments
 (0)