Skip to content

Commit 0beeb41

Browse files
committed
Added: updateCollection use case data access (pending refactor)
1 parent bc02624 commit 0beeb41

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed

src/collections/infra/repositories/CollectionsRepository.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,46 @@ export class CollectionsRepository extends ApiRepository implements ICollections
185185
})
186186
}
187187

188-
public updateCollection(
188+
public async updateCollection(
189189
collectionIdOrAlias: string | number,
190190
updatedCollection: CollectionDTO
191191
): Promise<void> {
192-
throw new Error('Method not implemented ' + collectionIdOrAlias + ' ' + updatedCollection.alias)
192+
const dataverseContacts: NewCollectionContactRequestPayload[] = updatedCollection.contacts.map(
193+
(contact) => ({
194+
contactEmail: contact
195+
})
196+
)
197+
198+
const inputLevelsRequestBody: NewCollectionInputLevelRequestPayload[] =
199+
updatedCollection.inputLevels?.map((inputLevel) => ({
200+
datasetFieldTypeName: inputLevel.datasetFieldName,
201+
include: inputLevel.include,
202+
required: inputLevel.required
203+
}))
204+
205+
const requestBody: NewCollectionRequestPayload = {
206+
alias: updatedCollection.alias,
207+
name: updatedCollection.name,
208+
dataverseContacts: dataverseContacts,
209+
dataverseType: updatedCollection.type,
210+
...(updatedCollection.description && {
211+
description: updatedCollection.description
212+
}),
213+
...(updatedCollection.affiliation && {
214+
affiliation: updatedCollection.affiliation
215+
}),
216+
metadataBlocks: {
217+
metadataBlockNames: updatedCollection.metadataBlockNames,
218+
facetIds: updatedCollection.facetIds,
219+
inputLevels: inputLevelsRequestBody
220+
}
221+
}
222+
223+
return this.doPut(`/${this.collectionsResourceName}/${collectionIdOrAlias}`, requestBody)
224+
.then(() => undefined)
225+
.catch((error) => {
226+
throw error
227+
})
193228
}
194229

195230
private applyCollectionSearchCriteriaToQueryParams(

test/environment/.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
POSTGRES_VERSION=13
22
DATAVERSE_DB_USER=dataverse
33
SOLR_VERSION=9.3.0
4-
DATAVERSE_IMAGE_REGISTRY=docker.io
5-
DATAVERSE_IMAGE_TAG=unstable
4+
DATAVERSE_IMAGE_REGISTRY=ghcr.io
5+
DATAVERSE_IMAGE_TAG=10904-edit-dataverse-collection
66
DATAVERSE_BOOTSTRAP_TIMEOUT=5m

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,67 @@ describe('CollectionsRepository', () => {
464464
).rejects.toThrow(expectedError)
465465
})
466466
})
467+
468+
describe('updateCollection', () => {
469+
const testUpdatedCollectionAlias = 'updateCollection-test-updatedAlias'
470+
471+
afterAll(async () => {
472+
await deleteCollectionViaApi(testUpdatedCollectionAlias)
473+
})
474+
475+
test('should update the collection', async () => {
476+
// First we create a test collection using a CollectionDTO and createCollection method
477+
const collectionDTO = createCollectionDTO('updatedCollection-test-originalAlias')
478+
const testUpdateCollectionId = await sut.createCollection(collectionDTO)
479+
const createdCollection = await sut.getCollection(testUpdateCollectionId)
480+
expect(createdCollection.id).toBe(testUpdateCollectionId)
481+
expect(createdCollection.alias).toBe(collectionDTO.alias)
482+
expect(createdCollection.name).toBe(collectionDTO.name)
483+
expect(createdCollection.affiliation).toBe(collectionDTO.affiliation)
484+
expect(createdCollection.inputLevels?.length).toBe(1)
485+
const inputLevel = createdCollection.inputLevels?.[0]
486+
expect(inputLevel?.datasetFieldName).toBe('geographicCoverage')
487+
expect(inputLevel?.include).toBe(true)
488+
expect(inputLevel?.required).toBe(true)
489+
490+
// Now we update CollectionDTO and verify updates are correctly persisted after calling updateCollection method
491+
collectionDTO.alias = testUpdatedCollectionAlias
492+
const updatedCollectionName = 'updatedCollectionName'
493+
collectionDTO.name = updatedCollectionName
494+
const updatedCollectionAffiliation = 'updatedCollectionAffiliation'
495+
collectionDTO.affiliation = updatedCollectionAffiliation
496+
const updatedInputLevels = [
497+
{
498+
datasetFieldName: 'country',
499+
required: false,
500+
include: true
501+
}
502+
]
503+
collectionDTO.inputLevels = updatedInputLevels
504+
await sut.updateCollection(testUpdateCollectionId, collectionDTO)
505+
const updatedCollection = await sut.getCollection(testUpdateCollectionId)
506+
expect(updatedCollection.id).toBe(testUpdateCollectionId)
507+
expect(updatedCollection.alias).toBe(testUpdatedCollectionAlias)
508+
expect(updatedCollection.name).toBe(updatedCollectionName)
509+
expect(updatedCollection.affiliation).toBe(updatedCollectionAffiliation)
510+
expect(updatedCollection.inputLevels?.length).toBe(1)
511+
const updatedInputLevel = updatedCollection.inputLevels?.[0]
512+
expect(updatedInputLevel?.datasetFieldName).toBe('country')
513+
expect(updatedInputLevel?.include).toBe(true)
514+
expect(updatedInputLevel?.required).toBe(false)
515+
})
516+
517+
test('should return error when collection does not exist', async () => {
518+
const expectedError = new WriteError(
519+
`[404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
520+
)
521+
const testCollectionAlias = 'updateCollection-not-found-test'
522+
await expect(
523+
sut.updateCollection(
524+
TestConstants.TEST_DUMMY_COLLECTION_ID,
525+
createCollectionDTO(testCollectionAlias)
526+
)
527+
).rejects.toThrow(expectedError)
528+
})
529+
})
467530
})

test/unit/collections/CollectionsRepository.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,64 @@ describe('CollectionsRepository', () => {
191191
})
192192
})
193193

194+
describe('updateCollection', () => {
195+
const testUpdatedCollection = createCollectionDTO()
196+
const testAlias = 'testCollectionAlias'
197+
198+
const testCreatedCollectionId = 1
199+
const testCreateCollectionResponse = {
200+
data: {
201+
status: 'OK',
202+
data: {
203+
id: testCreatedCollectionId
204+
}
205+
}
206+
}
207+
208+
const expectedUpdatedCollectionRequestPayloadJson = JSON.stringify(
209+
createNewCollectionRequestPayload()
210+
)
211+
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/dataverses/${testAlias}`
212+
213+
test('should call the API with a correct request payload', async () => {
214+
jest.spyOn(axios, 'put').mockResolvedValue(testCreateCollectionResponse)
215+
216+
// API Key auth
217+
await sut.updateCollection(testAlias, testUpdatedCollection)
218+
219+
expect(axios.put).toHaveBeenCalledWith(
220+
expectedApiEndpoint,
221+
expectedUpdatedCollectionRequestPayloadJson,
222+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
223+
)
224+
225+
// Session cookie auth
226+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE)
227+
228+
await sut.updateCollection(testAlias, testUpdatedCollection)
229+
230+
expect(axios.put).toHaveBeenCalledWith(
231+
expectedApiEndpoint,
232+
expectedUpdatedCollectionRequestPayloadJson,
233+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
234+
)
235+
})
236+
237+
test('should return error result on error response', async () => {
238+
jest.spyOn(axios, 'put').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
239+
240+
let error = undefined as unknown as WriteError
241+
await sut.updateCollection(testAlias, testUpdatedCollection).catch((e) => (error = e))
242+
243+
expect(axios.put).toHaveBeenCalledWith(
244+
expectedApiEndpoint,
245+
expectedUpdatedCollectionRequestPayloadJson,
246+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
247+
)
248+
expect(error).toBeInstanceOf(Error)
249+
})
250+
})
251+
194252
describe('getCollectionFacets', () => {
195253
const testFacetsSuccessfulResponse = {
196254
data: {

0 commit comments

Comments
 (0)