Skip to content

Commit 33a3760

Browse files
authored
Merge pull request #143 from IQSS/138-dataset-cleanup-before-after-functional-and-it-run
Create/destroy datasets on each IT/functional run
2 parents 5b0b8cd + 561fed3 commit 33a3760

File tree

16 files changed

+482
-627
lines changed

16 files changed

+482
-627
lines changed

test/environment/setup.ts

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import * as fs from 'fs'
22
import { DockerComposeEnvironment, Wait } from 'testcontainers'
33
import axios from 'axios'
44
import { TestConstants } from '../testHelpers/TestConstants'
5-
import datasetJson1 from '../testHelpers/datasets/test-dataset-1.json'
6-
import datasetJson2 from '../testHelpers/datasets/test-dataset-2.json'
7-
import datasetJson3 from '../testHelpers/datasets/test-dataset-3.json'
8-
import datasetJson4 from '../testHelpers/datasets/test-dataset-4.json'
9-
import collectionJson from '../testHelpers/collections/test-collection-1.json'
10-
import { ROOT_COLLECTION_ALIAS } from '../../src/collections/domain/models/Collection'
115

12-
const NUMBER_OF_DATASETS = 4
136
const COMPOSE_FILE = 'docker-compose.yml'
147

158
const CONTAINER_DATAVERSE_BOOTSTRAP_NAME = 'test_dataverse_bootstrap'
@@ -24,7 +17,6 @@ const API_KEY_USER_PASSWORD = 'admin1'
2417
export default async function setupTestEnvironment(): Promise<void> {
2518
await setupContainers()
2619
await setupApiKey()
27-
await setupTestFixtures()
2820
}
2921

3022
async function setupContainers(): Promise<void> {
@@ -54,88 +46,3 @@ async function setupApiKey(): Promise<void> {
5446
})
5547
console.log('API key obtained')
5648
}
57-
58-
async function setupTestFixtures(): Promise<void> {
59-
console.log('Creating test datasets...')
60-
await createDatasetViaApi(datasetJson1)
61-
.then()
62-
.catch(() => {
63-
console.error('Tests setup: Error while creating test Dataset 1')
64-
})
65-
await createDatasetViaApi(datasetJson2).catch(() => {
66-
console.error('Tests setup: Error while creating test Dataset 2')
67-
})
68-
await createDatasetViaApi(datasetJson4).catch(() => {
69-
console.error('Tests setup: Error while creating test Dataset 4')
70-
})
71-
await createCollectionViaApi(collectionJson)
72-
.then()
73-
.catch(() => {
74-
console.error('Tests setup: Error while creating test Collection 1')
75-
})
76-
await createDatasetViaApi(datasetJson3, collectionJson.alias)
77-
.then()
78-
.catch(() => {
79-
console.error('Tests setup: Error while creating test Dataset 3')
80-
})
81-
console.log('Test datasets created')
82-
await waitForDatasetsIndexingInSolr()
83-
}
84-
85-
/* eslint-disable @typescript-eslint/no-explicit-any */
86-
async function createCollectionViaApi(collectionJson: any): Promise<any> {
87-
return await axios.post(
88-
`${TestConstants.TEST_API_URL}/dataverses/root`,
89-
collectionJson,
90-
buildRequestHeaders()
91-
)
92-
}
93-
94-
/* eslint-disable @typescript-eslint/no-explicit-any */
95-
async function createDatasetViaApi(
96-
datasetJson: any,
97-
collectionId = ROOT_COLLECTION_ALIAS
98-
): Promise<any> {
99-
return await axios.post(
100-
`${TestConstants.TEST_API_URL}/dataverses/${collectionId}/datasets`,
101-
datasetJson,
102-
buildRequestHeaders()
103-
)
104-
}
105-
106-
/* eslint-disable @typescript-eslint/no-explicit-any */
107-
async function waitForDatasetsIndexingInSolr(): Promise<void> {
108-
console.log('Waiting for datasets indexing in Solr...')
109-
let datasetsIndexed = false
110-
let retry = 0
111-
while (!datasetsIndexed && retry < 10) {
112-
await axios
113-
.get(`${TestConstants.TEST_API_URL}/search?q=*&type=dataset`, buildRequestHeaders())
114-
.then((response) => {
115-
const nDatasets = response.data.data.items.length
116-
if (nDatasets === NUMBER_OF_DATASETS) {
117-
datasetsIndexed = true
118-
}
119-
})
120-
.catch((error) => {
121-
console.error(
122-
`Tests setup: Error while waiting for datasets indexing in Solr: [${
123-
error.response.status
124-
}]${error.response.data ? ` ${error.response.data.message}` : ''}`
125-
)
126-
})
127-
await new Promise((resolve) => setTimeout(resolve, 1000))
128-
retry++
129-
}
130-
if (!datasetsIndexed) {
131-
throw new Error('Tests setup: Timeout reached while waiting for datasets indexing in Solr')
132-
}
133-
console.log('Datasets indexed in Solr')
134-
}
135-
136-
/* eslint-disable @typescript-eslint/no-explicit-any */
137-
function buildRequestHeaders(): any {
138-
return {
139-
headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY }
140-
}
141-
}

test/functional/datasets/CreateDataset.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ApiConfig } from '../../../src'
33
import { TestConstants } from '../../testHelpers/TestConstants'
44
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
55
import { FieldValidationError } from '../../../src/datasets/domain/useCases/validators/errors/FieldValidationError'
6+
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
67

78
describe('execute', () => {
89
beforeEach(async () => {
@@ -56,6 +57,7 @@ describe('execute', () => {
5657
expect(createdDatasetIdentifiers).not.toBeNull()
5758
expect(createdDatasetIdentifiers.numericId).not.toBeNull()
5859
expect(createdDatasetIdentifiers.persistentId).not.toBeNull()
60+
await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId)
5961
}
6062
})
6163

test/functional/datasets/PublishDataset.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
} from '../../../src'
88
import { TestConstants } from '../../testHelpers/TestConstants'
99
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
10-
import { waitForNoLocks } from '../../testHelpers/datasets/datasetHelper'
10+
import {
11+
waitForNoLocks,
12+
deletePublishedDatasetViaApi
13+
} from '../../testHelpers/datasets/datasetHelper'
1114

1215
const testNewDataset = {
1316
license: {
@@ -57,12 +60,16 @@ describe('execute', () => {
5760
})
5861

5962
test('should successfully publish a dataset', async () => {
60-
const dataset = await createDataset.execute(testNewDataset)
63+
const createdDatasetIdentifiers = await createDataset.execute(testNewDataset)
6164

62-
const response = await publishDataset.execute(dataset.persistentId, VersionUpdateType.MAJOR)
63-
await waitForNoLocks(dataset.numericId, 10)
65+
const response = await publishDataset.execute(
66+
createdDatasetIdentifiers.persistentId,
67+
VersionUpdateType.MAJOR
68+
)
69+
await waitForNoLocks(createdDatasetIdentifiers.numericId, 10)
6470

6571
expect(response).toBeUndefined()
72+
await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId)
6673
})
6774

6875
test('should throw an error when trying to publish a dataset that does not exist', async () => {

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,51 @@ import { TestConstants } from '../../testHelpers/TestConstants'
33
import { ReadError } from '../../../src'
44
import { ApiConfig } from '../../../src'
55
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
6+
import {
7+
createCollectionViaApi,
8+
deleteCollectionViaApi
9+
} from '../../testHelpers/collections/collectionHelper'
10+
import { ROOT_COLLECTION_ALIAS } from '../../../src/collections/domain/models/Collection'
11+
import { CollectionPayload } from '../../../src/collections/infra/repositories/transformers/CollectionPayload'
612

713
describe('CollectionsRepository', () => {
814
const testGetCollection: CollectionsRepository = new CollectionsRepository()
15+
let testCollectionId: number
916

10-
beforeEach(async () => {
17+
beforeAll(async () => {
1118
ApiConfig.init(
1219
TestConstants.TEST_API_URL,
1320
DataverseApiAuthMechanism.API_KEY,
1421
process.env.TEST_API_KEY
1522
)
23+
await createCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2).then(
24+
(collectionPayload: CollectionPayload) => (testCollectionId = collectionPayload.id)
25+
)
1626
})
1727

18-
afterEach(async () => {
28+
afterAll(async () => {
1929
ApiConfig.init(
2030
TestConstants.TEST_API_URL,
2131
DataverseApiAuthMechanism.API_KEY,
2232
process.env.TEST_API_KEY
2333
)
34+
await deleteCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2)
2435
})
2536

2637
describe('getCollection', () => {
2738
describe('by default `root` Id', () => {
2839
test('should return the root collection of the Dataverse installation if no parameter is passed AS `root`', async () => {
2940
const actual = await testGetCollection.getCollection()
30-
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ROOT)
41+
expect(actual.alias).toBe(ROOT_COLLECTION_ALIAS)
3142
})
3243
})
3344

3445
describe('by string alias', () => {
3546
test('should return collection when it exists filtering by id AS (alias)', async () => {
3647
const actual = await testGetCollection.getCollection(
37-
TestConstants.TEST_CREATED_COLLECTION_1_ALIAS
48+
TestConstants.TEST_CREATED_COLLECTION_ALIAS_2
3849
)
39-
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ALIAS)
50+
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2)
4051
})
4152

4253
test('should return error when collection does not exist', async () => {
@@ -51,10 +62,8 @@ describe('CollectionsRepository', () => {
5162
})
5263
describe('by numeric id', () => {
5364
test('should return collection when it exists filtering by id AS (id)', async () => {
54-
const actual = await testGetCollection.getCollection(
55-
TestConstants.TEST_CREATED_COLLECTION_1_ID
56-
)
57-
expect(actual.id).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ID)
65+
const actual = await testGetCollection.getCollection(testCollectionId)
66+
expect(actual.id).toBe(testCollectionId)
5867
})
5968

6069
test('should return error when collection does not exist', async () => {

0 commit comments

Comments
 (0)