Skip to content

Commit 8e3c0be

Browse files
committed
Refactor: try catch blocks moved to helper test classes
1 parent bef2358 commit 8e3c0be

File tree

4 files changed

+117
-175
lines changed

4 files changed

+117
-175
lines changed

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
deleteCollectionViaApi
99
} from '../../testHelpers/collections/collectionHelper'
1010
import { ROOT_COLLECTION_ALIAS } from '../../../src/collections/domain/models/Collection'
11+
import { CollectionPayload } from '../../../src/collections/infra/repositories/transformers/CollectionPayload'
1112

1213
describe('CollectionsRepository', () => {
1314
const testGetCollection: CollectionsRepository = new CollectionsRepository()
@@ -19,14 +20,9 @@ describe('CollectionsRepository', () => {
1920
DataverseApiAuthMechanism.API_KEY,
2021
process.env.TEST_API_KEY
2122
)
22-
try {
23-
await createCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2).then(
24-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25-
(response: any) => (testCollectionId = response.data.data.id)
26-
)
27-
} catch (error) {
28-
throw new Error('Tests beforeAll(): Error while creating test collection')
29-
}
23+
await createCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2).then(
24+
(collectionPayload: CollectionPayload) => (testCollectionId = collectionPayload.id)
25+
)
3026
})
3127

3228
afterAll(async () => {
@@ -35,11 +31,7 @@ describe('CollectionsRepository', () => {
3531
DataverseApiAuthMechanism.API_KEY,
3632
process.env.TEST_API_KEY
3733
)
38-
try {
39-
await deleteCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2)
40-
} catch (error) {
41-
throw new Error('Tests afterAll(): Error while deleting test collection')
42-
}
34+
await deleteCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2)
4335
})
4436

4537
describe('getCollection', () => {

test/integration/datasets/DatasetsRepository.test.ts

Lines changed: 42 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -71,58 +71,42 @@ describe('DatasetsRepository', () => {
7171
})
7272

7373
const createCollection = async () => {
74-
try {
75-
await createCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_1)
76-
} catch (error) {
77-
throw new Error('Tests beforeAll(): Error while creating test collection')
78-
}
74+
await createCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_1)
7975
}
8076

8177
const createDatasets = async () => {
82-
try {
83-
firstDatasetIds = await createDataset.execute(
84-
TestConstants.TEST_NEW_DATASET_DTO,
85-
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
86-
)
87-
secondDatasetIds = await createDataset.execute(
88-
TestConstants.TEST_NEW_DATASET_DTO,
89-
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
90-
)
91-
thirdDatasetIds = await createDataset.execute(
92-
TestConstants.TEST_NEW_DATASET_DTO,
93-
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
94-
)
95-
fourthDatasetIds = await createDataset.execute(
96-
TestConstants.TEST_NEW_DATASET_DTO,
97-
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
98-
)
78+
firstDatasetIds = await createDataset.execute(
79+
TestConstants.TEST_NEW_DATASET_DTO,
80+
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
81+
)
82+
secondDatasetIds = await createDataset.execute(
83+
TestConstants.TEST_NEW_DATASET_DTO,
84+
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
85+
)
86+
thirdDatasetIds = await createDataset.execute(
87+
TestConstants.TEST_NEW_DATASET_DTO,
88+
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
89+
)
90+
fourthDatasetIds = await createDataset.execute(
91+
TestConstants.TEST_NEW_DATASET_DTO,
92+
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
93+
)
9994

100-
await waitForDatasetsIndexedInSolr(
101-
expectedTotalDatasetCount,
102-
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
103-
)
104-
} catch (error) {
105-
throw new Error('Tests beforeAll(): Error while creating test datasets')
106-
}
95+
await waitForDatasetsIndexedInSolr(
96+
expectedTotalDatasetCount,
97+
TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
98+
)
10799
}
108100

109101
const deleteDatasets = async () => {
110-
try {
111-
await deleteUnpublishedDatasetViaApi(firstDatasetIds.numericId)
112-
await deleteUnpublishedDatasetViaApi(secondDatasetIds.numericId)
113-
await deleteUnpublishedDatasetViaApi(thirdDatasetIds.numericId)
114-
await deleteUnpublishedDatasetViaApi(fourthDatasetIds.numericId)
115-
} catch (error) {
116-
throw new Error('Tests afterAll(): Error while deleting test datasets')
117-
}
102+
await deleteUnpublishedDatasetViaApi(firstDatasetIds.numericId)
103+
await deleteUnpublishedDatasetViaApi(secondDatasetIds.numericId)
104+
await deleteUnpublishedDatasetViaApi(thirdDatasetIds.numericId)
105+
await deleteUnpublishedDatasetViaApi(fourthDatasetIds.numericId)
118106
}
119107

120108
const deleteCollection = async () => {
121-
try {
122-
await deleteCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_1)
123-
} catch (error) {
124-
throw new Error('Tests afterAll(): Error while deleting test collection')
125-
}
109+
await deleteCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_1)
126110
}
127111

128112
test('should return all dataset previews when no pagination params are defined', async () => {
@@ -193,19 +177,11 @@ describe('DatasetsRepository', () => {
193177
let testDatasetIds: CreatedDatasetIdentifiers
194178

195179
beforeAll(async () => {
196-
try {
197-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
198-
} catch (error) {
199-
throw new Error('Tests beforeAll(): Error while creating test dataset')
200-
}
180+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
201181
})
202182

203183
afterAll(async () => {
204-
try {
205-
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
206-
} catch (error) {
207-
throw new Error('Tests afterAll(): Error while deleting test dataset')
208-
}
184+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
209185
})
210186

211187
test('should return dataset when it exists filtering by id and version id', async () => {
@@ -270,19 +246,11 @@ describe('DatasetsRepository', () => {
270246
let testDatasetIds: CreatedDatasetIdentifiers
271247

272248
beforeAll(async () => {
273-
try {
274-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
275-
} catch (error) {
276-
throw new Error('Tests beforeAll(): Error while creating test dataset')
277-
}
249+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
278250
})
279251

280252
afterAll(async () => {
281-
try {
282-
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
283-
} catch (error) {
284-
throw new Error('Tests afterAll(): Error while deleting test dataset')
285-
}
253+
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
286254
})
287255

288256
test('should return dataset when it exists filtering by persistent id and version id', async () => {
@@ -317,25 +285,13 @@ describe('DatasetsRepository', () => {
317285
let privateUrlToken: string
318286

319287
beforeAll(async () => {
320-
try {
321-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
322-
} catch (error) {
323-
throw new Error('Tests beforeAll(): Error while creating test dataset')
324-
}
325-
try {
326-
const response = await createPrivateUrlViaApi(testDatasetIds.numericId)
327-
privateUrlToken = response.data.data.token
328-
} catch (error) {
329-
throw new Error('Tests beforeAll(): Error while creating Dataset private URL')
330-
}
288+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
289+
const response = await createPrivateUrlViaApi(testDatasetIds.numericId)
290+
privateUrlToken = response.data.data.token
331291
})
332292

333293
afterAll(async () => {
334-
try {
335-
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
336-
} catch (error) {
337-
throw new Error('Tests afterAll(): Error while deleting test dataset')
338-
}
294+
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
339295
})
340296

341297
describe('getPrivateUrlDataset', () => {
@@ -369,19 +325,11 @@ describe('DatasetsRepository', () => {
369325
let testDatasetIds: CreatedDatasetIdentifiers
370326

371327
beforeAll(async () => {
372-
try {
373-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
374-
} catch (error) {
375-
throw new Error('Tests beforeAll(): Error while creating test dataset')
376-
}
328+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
377329
})
378330

379331
afterAll(async () => {
380-
try {
381-
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
382-
} catch (error) {
383-
throw new Error('Tests afterAll(): Error while deleting test dataset')
384-
}
332+
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
385333
})
386334

387335
test('should return user permissions filtering by dataset id', async () => {
@@ -408,27 +356,15 @@ describe('DatasetsRepository', () => {
408356
let testDatasetIds: CreatedDatasetIdentifiers
409357

410358
beforeAll(async () => {
411-
try {
412-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
413-
} catch (error) {
414-
throw new Error('Tests beforeAll(): Error while creating test dataset')
415-
}
359+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
416360
})
417361

418362
afterAll(async () => {
419-
try {
420-
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
421-
} catch (error) {
422-
throw new Error('Tests afterAll(): Error while deleting test dataset')
423-
}
363+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
424364
})
425365

426366
test('should return list of dataset locks by dataset id for a dataset while publishing', async () => {
427367
await publishDatasetViaApi(testDatasetIds.numericId)
428-
.then()
429-
.catch((error) => {
430-
console.log(JSON.stringify(error))
431-
})
432368
const actual = await sut.getDatasetLocks(testDatasetIds.numericId)
433369
expect(actual.length).toBe(1)
434370
expect(actual[0].lockType).toBe(DatasetLockType.FINALIZE_PUBLICATION)
@@ -449,19 +385,11 @@ describe('DatasetsRepository', () => {
449385
let testDatasetIds: CreatedDatasetIdentifiers
450386

451387
beforeAll(async () => {
452-
try {
453-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
454-
} catch (error) {
455-
throw new Error('Tests beforeAll(): Error while creating test dataset')
456-
}
388+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
457389
})
458390

459391
afterAll(async () => {
460-
try {
461-
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
462-
} catch (error) {
463-
throw new Error('Tests afterAll(): Error while deleting test dataset')
464-
}
392+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
465393
})
466394

467395
test('should return citation when dataset exists', async () => {
@@ -585,19 +513,11 @@ describe('DatasetsRepository', () => {
585513
let testDatasetIds: CreatedDatasetIdentifiers
586514

587515
beforeAll(async () => {
588-
try {
589-
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
590-
} catch (error) {
591-
throw new Error('Tests beforeAll(): Error while creating test dataset')
592-
}
516+
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
593517
})
594518

595519
afterAll(async () => {
596-
try {
597-
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
598-
} catch (error) {
599-
throw new Error('Tests afterAll(): Error while deleting test dataset')
600-
}
520+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
601521
})
602522

603523
test('should publish a new dataset version', async () => {

test/testHelpers/collections/collectionHelper.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,29 @@ export const createCollectionPayload = (): CollectionPayload => {
4040
return collectionPayload
4141
}
4242

43-
export async function createCollectionViaApi(collectionAlias: string): Promise<void> {
44-
return await axios.post(
45-
`${TestConstants.TEST_API_URL}/dataverses/root`,
46-
collectionAlias == TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
47-
? collectionJson1
48-
: collectionJson2,
49-
DATAVERSE_API_REQUEST_HEADERS
50-
)
43+
export async function createCollectionViaApi(collectionAlias: string): Promise<CollectionPayload> {
44+
try {
45+
return await axios
46+
.post(
47+
`${TestConstants.TEST_API_URL}/dataverses/root`,
48+
collectionAlias == TestConstants.TEST_CREATED_COLLECTION_ALIAS_1
49+
? collectionJson1
50+
: collectionJson2,
51+
DATAVERSE_API_REQUEST_HEADERS
52+
)
53+
.then((response) => response.data.data)
54+
} catch (error) {
55+
throw new Error(`Error while creating test collection ${collectionAlias}`)
56+
}
5157
}
5258

5359
export async function deleteCollectionViaApi(collectionAlias: string): Promise<void> {
54-
return await axios.delete(
55-
`${TestConstants.TEST_API_URL}/dataverses/${collectionAlias}`,
56-
DATAVERSE_API_REQUEST_HEADERS
57-
)
60+
try {
61+
return await axios.delete(
62+
`${TestConstants.TEST_API_URL}/dataverses/${collectionAlias}`,
63+
DATAVERSE_API_REQUEST_HEADERS
64+
)
65+
} catch (error) {
66+
throw new Error(`Error while deleting test collection ${collectionAlias}`)
67+
}
5868
}

0 commit comments

Comments
 (0)