Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create/destroy datasets on each IT/functional run #143

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 0 additions & 93 deletions test/environment/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import * as fs from 'fs'
import { DockerComposeEnvironment, Wait } from 'testcontainers'
import axios from 'axios'
import { TestConstants } from '../testHelpers/TestConstants'
import datasetJson1 from '../testHelpers/datasets/test-dataset-1.json'
import datasetJson2 from '../testHelpers/datasets/test-dataset-2.json'
import datasetJson3 from '../testHelpers/datasets/test-dataset-3.json'
import datasetJson4 from '../testHelpers/datasets/test-dataset-4.json'
import collectionJson from '../testHelpers/collections/test-collection-1.json'
import { ROOT_COLLECTION_ALIAS } from '../../src/collections/domain/models/Collection'

const NUMBER_OF_DATASETS = 4
const COMPOSE_FILE = 'docker-compose.yml'

const CONTAINER_DATAVERSE_BOOTSTRAP_NAME = 'test_dataverse_bootstrap'
Expand All @@ -24,7 +17,6 @@ const API_KEY_USER_PASSWORD = 'admin1'
export default async function setupTestEnvironment(): Promise<void> {
await setupContainers()
await setupApiKey()
await setupTestFixtures()
}

async function setupContainers(): Promise<void> {
Expand Down Expand Up @@ -54,88 +46,3 @@ async function setupApiKey(): Promise<void> {
})
console.log('API key obtained')
}

async function setupTestFixtures(): Promise<void> {
console.log('Creating test datasets...')
await createDatasetViaApi(datasetJson1)
.then()
.catch(() => {
console.error('Tests setup: Error while creating test Dataset 1')
})
await createDatasetViaApi(datasetJson2).catch(() => {
console.error('Tests setup: Error while creating test Dataset 2')
})
await createDatasetViaApi(datasetJson4).catch(() => {
console.error('Tests setup: Error while creating test Dataset 4')
})
await createCollectionViaApi(collectionJson)
.then()
.catch(() => {
console.error('Tests setup: Error while creating test Collection 1')
})
await createDatasetViaApi(datasetJson3, collectionJson.alias)
.then()
.catch(() => {
console.error('Tests setup: Error while creating test Dataset 3')
})
console.log('Test datasets created')
await waitForDatasetsIndexingInSolr()
}

/* eslint-disable @typescript-eslint/no-explicit-any */
async function createCollectionViaApi(collectionJson: any): Promise<any> {
return await axios.post(
`${TestConstants.TEST_API_URL}/dataverses/root`,
collectionJson,
buildRequestHeaders()
)
}

/* eslint-disable @typescript-eslint/no-explicit-any */
async function createDatasetViaApi(
datasetJson: any,
collectionId = ROOT_COLLECTION_ALIAS
): Promise<any> {
return await axios.post(
`${TestConstants.TEST_API_URL}/dataverses/${collectionId}/datasets`,
datasetJson,
buildRequestHeaders()
)
}

/* eslint-disable @typescript-eslint/no-explicit-any */
async function waitForDatasetsIndexingInSolr(): Promise<void> {
console.log('Waiting for datasets indexing in Solr...')
let datasetsIndexed = false
let retry = 0
while (!datasetsIndexed && retry < 10) {
await axios
.get(`${TestConstants.TEST_API_URL}/search?q=*&type=dataset`, buildRequestHeaders())
.then((response) => {
const nDatasets = response.data.data.items.length
if (nDatasets === NUMBER_OF_DATASETS) {
datasetsIndexed = true
}
})
.catch((error) => {
console.error(
`Tests setup: Error while waiting for datasets indexing in Solr: [${
error.response.status
}]${error.response.data ? ` ${error.response.data.message}` : ''}`
)
})
await new Promise((resolve) => setTimeout(resolve, 1000))
retry++
}
if (!datasetsIndexed) {
throw new Error('Tests setup: Timeout reached while waiting for datasets indexing in Solr')
}
console.log('Datasets indexed in Solr')
}

/* eslint-disable @typescript-eslint/no-explicit-any */
function buildRequestHeaders(): any {
return {
headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY }
}
}
2 changes: 2 additions & 0 deletions test/functional/datasets/CreateDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiConfig } from '../../../src'
import { TestConstants } from '../../testHelpers/TestConstants'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import { FieldValidationError } from '../../../src/datasets/domain/useCases/validators/errors/FieldValidationError'
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'

describe('execute', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -56,6 +57,7 @@ describe('execute', () => {
expect(createdDatasetIdentifiers).not.toBeNull()
expect(createdDatasetIdentifiers.numericId).not.toBeNull()
expect(createdDatasetIdentifiers.persistentId).not.toBeNull()
await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId)
}
})

Expand Down
15 changes: 11 additions & 4 deletions test/functional/datasets/PublishDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
} from '../../../src'
import { TestConstants } from '../../testHelpers/TestConstants'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import { waitForNoLocks } from '../../testHelpers/datasets/datasetHelper'
import {
waitForNoLocks,
deletePublishedDatasetViaApi
} from '../../testHelpers/datasets/datasetHelper'

const testNewDataset = {
license: {
Expand Down Expand Up @@ -57,12 +60,16 @@ describe('execute', () => {
})

test('should successfully publish a dataset', async () => {
const dataset = await createDataset.execute(testNewDataset)
const createdDatasetIdentifiers = await createDataset.execute(testNewDataset)

const response = await publishDataset.execute(dataset.persistentId, VersionUpdateType.MAJOR)
await waitForNoLocks(dataset.numericId, 10)
const response = await publishDataset.execute(
createdDatasetIdentifiers.persistentId,
VersionUpdateType.MAJOR
)
await waitForNoLocks(createdDatasetIdentifiers.numericId, 10)

expect(response).toBeUndefined()
await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId)
})

test('should throw an error when trying to publish a dataset that does not exist', async () => {
Expand Down
27 changes: 18 additions & 9 deletions test/integration/collections/CollectionsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,51 @@ import { TestConstants } from '../../testHelpers/TestConstants'
import { ReadError } from '../../../src'
import { ApiConfig } from '../../../src'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import {
createCollectionViaApi,
deleteCollectionViaApi
} from '../../testHelpers/collections/collectionHelper'
import { ROOT_COLLECTION_ALIAS } from '../../../src/collections/domain/models/Collection'
import { CollectionPayload } from '../../../src/collections/infra/repositories/transformers/CollectionPayload'

describe('CollectionsRepository', () => {
const testGetCollection: CollectionsRepository = new CollectionsRepository()
let testCollectionId: number

beforeEach(async () => {
beforeAll(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.API_KEY,
process.env.TEST_API_KEY
)
await createCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2).then(
(collectionPayload: CollectionPayload) => (testCollectionId = collectionPayload.id)
)
})

afterEach(async () => {
afterAll(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.API_KEY,
process.env.TEST_API_KEY
)
await deleteCollectionViaApi(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2)
})

describe('getCollection', () => {
describe('by default `root` Id', () => {
test('should return the root collection of the Dataverse installation if no parameter is passed AS `root`', async () => {
const actual = await testGetCollection.getCollection()
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ROOT)
expect(actual.alias).toBe(ROOT_COLLECTION_ALIAS)
})
})

describe('by string alias', () => {
test('should return collection when it exists filtering by id AS (alias)', async () => {
const actual = await testGetCollection.getCollection(
TestConstants.TEST_CREATED_COLLECTION_1_ALIAS
TestConstants.TEST_CREATED_COLLECTION_ALIAS_2
)
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ALIAS)
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_ALIAS_2)
})

test('should return error when collection does not exist', async () => {
Expand All @@ -51,10 +62,8 @@ describe('CollectionsRepository', () => {
})
describe('by numeric id', () => {
test('should return collection when it exists filtering by id AS (id)', async () => {
const actual = await testGetCollection.getCollection(
TestConstants.TEST_CREATED_COLLECTION_1_ID
)
expect(actual.id).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ID)
const actual = await testGetCollection.getCollection(testCollectionId)
expect(actual.id).toBe(testCollectionId)
})

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