Skip to content

Commit 5695023

Browse files
committed
test: add unit and integration test
1 parent 76fa636 commit 5695023

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,63 @@ describe('CollectionsRepository', () => {
804804
})
805805
})
806806

807+
describe('getCollectionsForLinking', () => {
808+
const linkingTargetAlias = 'collectionsRepositoryLinkTarget'
809+
810+
beforeAll(async () => {
811+
await createCollectionViaApi(linkingTargetAlias)
812+
})
813+
814+
afterAll(async () => {
815+
await deleteCollectionViaApi(linkingTargetAlias)
816+
})
817+
818+
test('should list collections for linking for a given collection alias', async () => {
819+
const results = await sut.getCollectionsForLinking(
820+
'collection',
821+
testCollectionAlias,
822+
'Scientific'
823+
)
824+
825+
expect(Array.isArray(results)).toBe(true)
826+
// Should contain the newly created linking target collection among candidates
827+
const found = results.find((c) => c.alias === linkingTargetAlias)
828+
expect(found).toBeDefined()
829+
expect(found?.id).toBeGreaterThan(0)
830+
expect(found?.displayName).toBe('Scientific Research')
831+
})
832+
833+
test('should list collections for linking for a given dataset persistentId', async () => {
834+
// Create a temporary dataset to query linking candidates
835+
const { persistentId, numericId } = await createDataset.execute(
836+
TestConstants.TEST_NEW_DATASET_DTO,
837+
testCollectionAlias
838+
)
839+
840+
const results = await sut.getCollectionsForLinking('dataset', persistentId, 'Scientific')
841+
842+
// Cleanup dataset (unpublished)
843+
await deleteUnpublishedDatasetViaApi(numericId)
844+
845+
expect(Array.isArray(results)).toBe(true)
846+
const found = results.find((c) => c.alias === linkingTargetAlias)
847+
expect(found).toBeDefined()
848+
expect(found?.displayName).toBe('Scientific Research')
849+
})
850+
851+
it('should return error when collection does not exist', async () => {
852+
await expect(
853+
sut.getCollectionsForLinking('collection', TestConstants.TEST_DUMMY_COLLECTION_ALIAS, '')
854+
).rejects.toThrow(ReadError)
855+
})
856+
857+
it('should return error when dataset does not exist', async () => {
858+
await expect(
859+
sut.getCollectionsForLinking('dataset', TestConstants.TEST_DUMMY_PERSISTENT_ID, '')
860+
).rejects.toThrow(ReadError)
861+
})
862+
})
863+
807864
describe('getCollectionItems for published tabular file', () => {
808865
let testDatasetIds: CreatedDatasetIdentifiers
809866
const testTextFile4Name = 'test-file-4.tab'

test/unit/collections/CollectionsRepository.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,55 @@ describe('CollectionsRepository', () => {
567567
})
568568
})
569569

570+
describe('getCollectionsForLinking', () => {
571+
test('should call dataverse variant with numeric id and search term', async () => {
572+
const payload = {
573+
data: {
574+
status: 'OK',
575+
data: [
576+
{ id: 1, alias: 'dv1', name: 'DV 1' },
577+
{ id: 2, alias: 'dv2', name: 'DV 2' }
578+
]
579+
}
580+
}
581+
jest.spyOn(axios, 'get').mockResolvedValue(payload)
582+
583+
const actual = await sut.getCollectionsForLinking('collection', 99, 'abc')
584+
const expectedEndpoint = `${TestConstants.TEST_API_URL}/dataverses/99/dataverse/linkingDataverses`
585+
const expectedParams = new URLSearchParams({ searchTerm: 'abc' })
586+
const expectedConfig = {
587+
params: expectedParams,
588+
headers: TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY.headers
589+
}
590+
expect(axios.get).toHaveBeenCalledWith(expectedEndpoint, expectedConfig)
591+
expect(actual).toEqual([
592+
{ id: 1, alias: 'dv1', displayName: 'DV 1' },
593+
{ id: 2, alias: 'dv2', displayName: 'DV 2' }
594+
])
595+
})
596+
597+
test('should call dataset variant using persistentId and map results', async () => {
598+
const payload = {
599+
data: {
600+
status: 'OK',
601+
data: [{ id: 3, alias: 'dv3', name: 'DV 3' }]
602+
}
603+
}
604+
jest.spyOn(axios, 'get').mockResolvedValue(payload)
605+
606+
const pid = 'doi:10.5072/FK2/J8SJZB'
607+
const actual = await sut.getCollectionsForLinking('dataset', pid, '')
608+
const expectedEndpoint = `${TestConstants.TEST_API_URL}/dataverses/:persistentId/dataset/linkingDataverses`
609+
const expectedParams = new URLSearchParams({ persistentId: pid })
610+
const expectedConfig = {
611+
params: expectedParams,
612+
headers: TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY.headers
613+
}
614+
expect(axios.get).toHaveBeenCalledWith(expectedEndpoint, expectedConfig)
615+
expect(actual).toEqual([{ id: 3, alias: 'dv3', displayName: 'DV 3' }])
616+
})
617+
})
618+
570619
describe('deleteCollection', () => {
571620
const deleteTestCollectionAlias = 'deleteCollection-unit-test'
572621
const deleteTestCollectionId = 123
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ICollectionsRepository } from '../../../src/collections/domain/repositories/ICollectionsRepository'
2+
import { GetCollectionsForLinking } from '../../../src/collections/domain/useCases/GetCollectionsForLinking'
3+
import { CollectionSummary, ReadError } from '../../../src'
4+
5+
const sample: CollectionSummary[] = [
6+
{ id: 1, alias: 'col1', displayName: 'Collection 1' },
7+
{ id: 2, alias: 'col2', displayName: 'Collection 2' }
8+
]
9+
10+
describe('GetCollectionsForLinking', () => {
11+
test('should return collections for linking on success', async () => {
12+
const repo: ICollectionsRepository = {} as ICollectionsRepository
13+
repo.getCollectionsForLinking = jest.fn().mockResolvedValue(sample)
14+
15+
const uc = new GetCollectionsForLinking(repo)
16+
await expect(uc.execute('collection', 123, 'foo')).resolves.toEqual(sample)
17+
expect(repo.getCollectionsForLinking).toHaveBeenCalledWith('collection', 123, 'foo')
18+
})
19+
20+
test('should return error result on repository error', async () => {
21+
const repo: ICollectionsRepository = {} as ICollectionsRepository
22+
repo.getCollectionsForLinking = jest.fn().mockRejectedValue(new ReadError('x'))
23+
24+
const uc = new GetCollectionsForLinking(repo)
25+
await expect(uc.execute('dataset', 'doi:10.123/ABC')).rejects.toThrow(ReadError)
26+
expect(repo.getCollectionsForLinking).toHaveBeenCalledWith('dataset', 'doi:10.123/ABC', '')
27+
})
28+
})

0 commit comments

Comments
 (0)