Skip to content

Commit 548ca2c

Browse files
committed
add functional tests
1 parent f199086 commit 548ca2c

File tree

7 files changed

+169
-6
lines changed

7 files changed

+169
-6
lines changed

.DS_Store

0 Bytes
Binary file not shown.

src/collections/domain/useCases/LinkCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class LinkCollection implements UseCase<void> {
1616
* @returns {Promise<void>} -This method does not return anything upon successful completion.
1717
*/
1818
async execute(
19-
linkedCollectionIdOrAlias: string,
20-
linkingCollectionIdOrAlias: string
19+
linkedCollectionIdOrAlias: number | string,
20+
linkingCollectionIdOrAlias: number | string
2121
): Promise<void> {
2222
return await this.collectionsRepository.linkCollection(
2323
linkedCollectionIdOrAlias,

src/collections/domain/useCases/UnLinkCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class UnLinkCollection implements UseCase<void> {
1616
* @returns {Promise<void>} -This method does not return anything upon successful completion.
1717
*/
1818
async execute(
19-
linkedCollectionIdOrAlias: string,
20-
linkingCollectionIdOrAlias: string
19+
linkedCollectionIdOrAlias: number | string,
20+
linkingCollectionIdOrAlias: number | string
2121
): Promise<void> {
2222
return await this.collectionsRepository.unlinkCollection(
2323
linkedCollectionIdOrAlias,

src/collections/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { DeleteCollectionFeaturedItems } from './domain/useCases/DeleteCollectio
1212
import { DeleteCollection } from './domain/useCases/DeleteCollection'
1313
import { GetMyDataCollectionItems } from './domain/useCases/GetMyDataCollectionItems'
1414
import { DeleteCollectionFeaturedItem } from './domain/useCases/DeleteCollectionFeaturedItem'
15+
import { LinkCollection } from './domain/useCases/LinkCollection'
16+
import { UnLinkCollection } from './domain/useCases/UnLinkCollection'
1517

1618
const collectionsRepository = new CollectionsRepository()
1719

@@ -28,6 +30,8 @@ const updateCollectionFeaturedItems = new UpdateCollectionFeaturedItems(collecti
2830
const deleteCollectionFeaturedItems = new DeleteCollectionFeaturedItems(collectionsRepository)
2931
const deleteCollection = new DeleteCollection(collectionsRepository)
3032
const deleteCollectionFeaturedItem = new DeleteCollectionFeaturedItem(collectionsRepository)
33+
const linkCollection = new LinkCollection(collectionsRepository)
34+
const unlinkCollection = new UnLinkCollection(collectionsRepository)
3135

3236
export {
3337
getCollection,
@@ -42,7 +46,9 @@ export {
4246
updateCollectionFeaturedItems,
4347
deleteCollectionFeaturedItems,
4448
deleteCollection,
45-
deleteCollectionFeaturedItem
49+
deleteCollectionFeaturedItem,
50+
linkCollection,
51+
unlinkCollection
4652
}
4753
export { Collection, CollectionInputLevel } from './domain/models/Collection'
4854
export { CollectionFacet } from './domain/models/CollectionFacet'

src/collections/infra/repositories/CollectionsRepository.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ export class CollectionsRepository extends ApiRepository implements ICollections
450450
linkedCollectionIdOrAlias: number | string,
451451
linkingCollectionIdOrAlias: number | string
452452
): Promise<void> {
453-
console.log(linkedCollectionIdOrAlias, linkingCollectionIdOrAlias)
454453
return this.doPut(
455454
`/dataverses/${linkedCollectionIdOrAlias}` + `/link/${linkingCollectionIdOrAlias}`,
456455
{} // No data is needed for this operation
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
ApiConfig,
3+
WriteError,
4+
createCollection,
5+
getCollection,
6+
linkCollection,
7+
deleteCollection,
8+
getCollectionItems
9+
} from '../../../src'
10+
import { TestConstants } from '../../testHelpers/TestConstants'
11+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
12+
import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper'
13+
14+
describe('execute', () => {
15+
const firstCollectionAlias = 'linkCollection-functional-test-first'
16+
const secondCollectionAlias = 'linkCollection-functional-test-second'
17+
18+
beforeEach(async () => {
19+
ApiConfig.init(
20+
TestConstants.TEST_API_URL,
21+
DataverseApiAuthMechanism.API_KEY,
22+
process.env.TEST_API_KEY
23+
)
24+
const firstCollection = createCollectionDTO(firstCollectionAlias)
25+
const secondCollection = createCollectionDTO(secondCollectionAlias)
26+
await createCollection.execute(firstCollection)
27+
await createCollection.execute(secondCollection)
28+
})
29+
30+
afterEach(async () => {
31+
await Promise.all([
32+
getCollection
33+
.execute(firstCollectionAlias)
34+
.then((collection) =>
35+
collection && collection.id ? deleteCollection.execute(collection.id) : null
36+
),
37+
getCollection
38+
.execute(secondCollectionAlias)
39+
.then((collection) =>
40+
collection && collection.id ? deleteCollection.execute(collection.id) : null
41+
)
42+
])
43+
})
44+
45+
test('should successfully link two collections', async () => {
46+
const firstCollection = await getCollection.execute(firstCollectionAlias)
47+
const secondCollection = await getCollection.execute(secondCollectionAlias)
48+
expect.assertions(1)
49+
try {
50+
await linkCollection.execute(secondCollection.alias, firstCollection.alias)
51+
} catch (error) {
52+
throw new Error('Collections should be linked successfully')
53+
} finally {
54+
await new Promise((resolve) => setTimeout(resolve, 5000))
55+
const collectionItemSubset = await getCollectionItems.execute(firstCollectionAlias)
56+
57+
expect(collectionItemSubset.items.length).toBe(1)
58+
}
59+
})
60+
61+
test('should throw an error when linking a non-existent collection', async () => {
62+
const invalidCollectionId = 99999
63+
const firstCollection = await getCollection.execute(firstCollectionAlias)
64+
65+
expect.assertions(2)
66+
let writeError: WriteError | undefined = undefined
67+
try {
68+
await linkCollection.execute(invalidCollectionId, firstCollection.id)
69+
throw new Error('Use case should throw an error')
70+
} catch (error) {
71+
writeError = error as WriteError
72+
} finally {
73+
expect(writeError).toBeInstanceOf(WriteError)
74+
expect(writeError?.message).toEqual(
75+
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${invalidCollectionId}'`
76+
)
77+
}
78+
})
79+
})
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
ApiConfig,
3+
WriteError,
4+
createCollection,
5+
getCollection,
6+
linkCollection,
7+
deleteCollection,
8+
getCollectionItems,
9+
unlinkCollection
10+
} from '../../../src'
11+
import { TestConstants } from '../../testHelpers/TestConstants'
12+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
13+
import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper'
14+
15+
describe('execute', () => {
16+
const firstCollectionAlias = 'unlinkCollection-functional-test-first'
17+
const secondCollectionAlias = 'unlinkCollection-functional-test-second'
18+
19+
beforeEach(async () => {
20+
ApiConfig.init(
21+
TestConstants.TEST_API_URL,
22+
DataverseApiAuthMechanism.API_KEY,
23+
process.env.TEST_API_KEY
24+
)
25+
const firstCollection = createCollectionDTO(firstCollectionAlias)
26+
const secondCollection = createCollectionDTO(secondCollectionAlias)
27+
await createCollection.execute(firstCollection)
28+
await createCollection.execute(secondCollection)
29+
await linkCollection.execute(secondCollection.alias, firstCollection.alias)
30+
})
31+
32+
afterEach(async () => {
33+
await Promise.all([
34+
getCollection
35+
.execute(firstCollectionAlias)
36+
.then((collection) =>
37+
collection && collection.id ? deleteCollection.execute(collection.id) : null
38+
),
39+
getCollection
40+
.execute(secondCollectionAlias)
41+
.then((collection) =>
42+
collection && collection.id ? deleteCollection.execute(collection.id) : null
43+
)
44+
])
45+
})
46+
47+
test('should successfully unlink two collections', async () => {
48+
const firstCollection = await getCollection.execute(firstCollectionAlias)
49+
const secondCollection = await getCollection.execute(secondCollectionAlias)
50+
// Give enough time to Solr for indexing
51+
await new Promise((resolve) => setTimeout(resolve, 5000))
52+
const collectionItemSubset = await getCollectionItems.execute(firstCollectionAlias)
53+
expect(collectionItemSubset.items.length).toBe(1)
54+
55+
await unlinkCollection.execute(secondCollection.alias, firstCollection.alias)
56+
await new Promise((resolve) => setTimeout(resolve, 5000))
57+
const collectionItemSubset2 = await getCollectionItems.execute(firstCollectionAlias)
58+
expect(collectionItemSubset2.items.length).toBe(0)
59+
})
60+
61+
test('should throw an error when linking a non-existent collection', async () => {
62+
const invalidCollectionId = 99999
63+
const firstCollection = await getCollection.execute(firstCollectionAlias)
64+
65+
expect.assertions(2)
66+
let writeError: WriteError | undefined = undefined
67+
try {
68+
await unlinkCollection.execute(invalidCollectionId, firstCollection.id)
69+
throw new Error('Use case should throw an error')
70+
} catch (error) {
71+
writeError = error as WriteError
72+
} finally {
73+
expect(writeError).toBeInstanceOf(WriteError)
74+
expect(writeError?.message).toEqual(
75+
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${invalidCollectionId}'`
76+
)
77+
}
78+
})
79+
})

0 commit comments

Comments
 (0)