Skip to content

Commit d85d286

Browse files
committed
test: add unit, functional and integration test cases
1 parent da4a167 commit d85d286

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
ApiConfig,
3+
ReadError,
4+
WriteError,
5+
createCollection,
6+
deleteCollection,
7+
getCollection
8+
} from '../../../src'
9+
import { TestConstants } from '../../testHelpers/TestConstants'
10+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
11+
import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper'
12+
13+
describe('execute', () => {
14+
beforeEach(async () => {
15+
ApiConfig.init(
16+
TestConstants.TEST_API_URL,
17+
DataverseApiAuthMechanism.API_KEY,
18+
process.env.TEST_API_KEY
19+
)
20+
})
21+
22+
test('should successfully delete a collection', async () => {
23+
const testCollectionAlias = 'deleteCollection-functional-test'
24+
const testNewCollection = createCollectionDTO(testCollectionAlias)
25+
await createCollection.execute(testNewCollection)
26+
27+
expect.assertions(1)
28+
try {
29+
await deleteCollection.execute(testCollectionAlias)
30+
} catch (error) {
31+
throw new Error('Collection should be deleted')
32+
} finally {
33+
const expectedError = new ReadError(
34+
`[404] Can't find dataverse with identifier='${testCollectionAlias}'`
35+
)
36+
await expect(getCollection.execute(testCollectionAlias)).rejects.toThrow(expectedError)
37+
}
38+
})
39+
40+
test('should throw an error when the collection does not exist', async () => {
41+
expect.assertions(2)
42+
let writeError: WriteError | undefined = undefined
43+
try {
44+
await deleteCollection.execute(TestConstants.TEST_DUMMY_COLLECTION_ID)
45+
throw new Error('Use case should throw an error')
46+
} catch (error) {
47+
writeError = error as WriteError
48+
} finally {
49+
expect(writeError).toBeInstanceOf(WriteError)
50+
expect(writeError?.message).toEqual(
51+
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
52+
)
53+
}
54+
})
55+
})

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('CollectionsRepository', () => {
4747
const sut: CollectionsRepository = new CollectionsRepository()
4848
let testCollectionId: number
4949
const currentYear = new Date().getFullYear()
50+
5051
beforeAll(async () => {
5152
ApiConfig.init(
5253
TestConstants.TEST_API_URL,
@@ -146,6 +147,7 @@ describe('CollectionsRepository', () => {
146147
expect(createdCollection.name).toBe(newCollectionDTO.name)
147148
})
148149
})
150+
149151
describe('createCollection', () => {
150152
const testCreateCollectionAlias1 = 'createCollection-test-1'
151153
const testCreateCollectionAlias2 = 'createCollection-test-2'
@@ -247,6 +249,35 @@ describe('CollectionsRepository', () => {
247249
})
248250
})
249251

252+
describe('deleteCollection', () => {
253+
test('should delete collection successfully', async () => {
254+
const collectionAlias = 'deleteCollection-test'
255+
const collectionDTO = createCollectionDTO(collectionAlias)
256+
await sut.createCollection(collectionDTO)
257+
258+
const createdCollection = await sut.getCollection(collectionAlias)
259+
260+
expect(createdCollection.alias).toBe(collectionAlias)
261+
262+
const deleteResult = await sut.deleteCollection(collectionAlias)
263+
const expectedError = new ReadError(
264+
`[404] Can't find dataverse with identifier='${collectionAlias}'`
265+
)
266+
267+
expect(deleteResult).toBeUndefined()
268+
await expect(sut.getCollection(collectionAlias)).rejects.toThrow(expectedError)
269+
})
270+
271+
test('should return error when collection does not exist', async () => {
272+
const expectedError = new WriteError(
273+
`[404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
274+
)
275+
await expect(sut.deleteCollection(TestConstants.TEST_DUMMY_COLLECTION_ID)).rejects.toThrow(
276+
expectedError
277+
)
278+
})
279+
})
280+
250281
describe('getCollectionFacets', () => {
251282
test('should return collection facets given a valid collection alias', async () => {
252283
const actual = await sut.getCollectionFacets(testCollectionAlias)

test/unit/collections/CollectionsRepository.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,87 @@ describe('CollectionsRepository', () => {
569569
expect(error).toBeInstanceOf(Error)
570570
})
571571
})
572+
573+
describe('deleteCollection', () => {
574+
const deleteTestCollectionAlias = 'deleteCollection-unit-test'
575+
const deleteTestCollectionId = 123
576+
577+
describe('by numeric id', () => {
578+
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/dataverses/${deleteTestCollectionId}`
579+
580+
test('should delete a collection when providing a valid id', async () => {
581+
jest.spyOn(axios, 'delete').mockResolvedValue({ data: { status: 'OK' } })
582+
583+
// API Key auth
584+
await sut.deleteCollection(deleteTestCollectionId)
585+
586+
expect(axios.delete).toHaveBeenCalledWith(
587+
expectedApiEndpoint,
588+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
589+
)
590+
591+
// Session cookie auth
592+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE)
593+
594+
await sut.deleteCollection(deleteTestCollectionId)
595+
596+
expect(axios.delete).toHaveBeenCalledWith(
597+
expectedApiEndpoint,
598+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
599+
)
600+
})
601+
602+
test('should return error result on error response', async () => {
603+
jest.spyOn(axios, 'delete').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
604+
605+
let error = undefined as unknown as WriteError
606+
await sut.deleteCollection(deleteTestCollectionId).catch((e) => (error = e))
607+
608+
expect(axios.delete).toHaveBeenCalledWith(
609+
expectedApiEndpoint,
610+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
611+
)
612+
expect(error).toBeInstanceOf(WriteError)
613+
})
614+
})
615+
616+
describe('by alias id', () => {
617+
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/dataverses/${deleteTestCollectionAlias}`
618+
619+
test('should delete a collection when providing a valid alias', async () => {
620+
jest.spyOn(axios, 'delete').mockResolvedValue({ data: { status: 'OK' } })
621+
622+
// API Key auth
623+
await sut.deleteCollection(deleteTestCollectionAlias)
624+
625+
expect(axios.delete).toHaveBeenCalledWith(
626+
expectedApiEndpoint,
627+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
628+
)
629+
630+
// Session cookie auth
631+
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE)
632+
633+
await sut.deleteCollection(deleteTestCollectionAlias)
634+
635+
expect(axios.delete).toHaveBeenCalledWith(
636+
expectedApiEndpoint,
637+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
638+
)
639+
})
640+
641+
test('should return error result on error response', async () => {
642+
jest.spyOn(axios, 'delete').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
643+
644+
let error = undefined as unknown as WriteError
645+
await sut.deleteCollection(deleteTestCollectionAlias).catch((e) => (error = e))
646+
647+
expect(axios.delete).toHaveBeenCalledWith(
648+
expectedApiEndpoint,
649+
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
650+
)
651+
expect(error).toBeInstanceOf(WriteError)
652+
})
653+
})
654+
})
572655
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ICollectionsRepository } from '../../../src/collections/domain/repositories/ICollectionsRepository'
2+
import { WriteError } from '../../../src'
3+
import { DeleteCollection } from '../../../src/collections/domain/useCases/DeleteCollection'
4+
5+
describe('execute', () => {
6+
test('should return undefined on repository success', async () => {
7+
const collectionRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository
8+
collectionRepositoryStub.deleteCollection = jest.fn().mockResolvedValue(undefined)
9+
const testDeleteCollection = new DeleteCollection(collectionRepositoryStub)
10+
11+
const actual = await testDeleteCollection.execute(1)
12+
13+
expect(actual).toEqual(undefined)
14+
})
15+
16+
test('should return error result on repository error', async () => {
17+
const collectionRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository
18+
collectionRepositoryStub.deleteCollection = jest.fn().mockRejectedValue(new WriteError())
19+
const testDeleteCollection = new DeleteCollection(collectionRepositoryStub)
20+
21+
await expect(testDeleteCollection.execute(1)).rejects.toThrow(WriteError)
22+
})
23+
})

0 commit comments

Comments
 (0)