Skip to content

Commit

Permalink
feat: add TestUtils.waitForNoLocks()
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Nov 1, 2023
1 parent 7d2bce2 commit 8681f80
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ describe('Dataset JSDataverse Repository', () => {
it('gets the dataset by persistentId and version number', async () => {
const datasetResponse = await DatasetHelper.create()
await DatasetHelper.publish(datasetResponse.persistentId)

await TestsUtils.wait(1500)

await TestsUtils.waitForNoLocks(datasetResponse.persistentId)
await datasetRepository
.getByPersistentId(datasetResponse.persistentId, '1.0')
.then((dataset) => {
Expand Down
30 changes: 30 additions & 0 deletions tests/e2e-integration/shared/TestsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiConfig } from '@iqss/dataverse-client-javascript/dist/core'
import { DataverseApiHelper } from './DataverseApiHelper'
import { DataverseApiAuthMechanism } from '@iqss/dataverse-client-javascript/dist/core/infra/repositories/ApiConfig'
import { UserJSDataverseRepository } from '../../../src/users/infrastructure/repositories/UserJSDataverseRepository'
import { DatasetHelper } from './datasets/DatasetHelper'

export class TestsUtils {
static readonly DATAVERSE_BACKEND_URL =
Expand All @@ -25,4 +26,33 @@ export class TestsUtils {
static logout() {
return new UserJSDataverseRepository().removeAuthenticated()
}

static async waitForNoLocks(persistentId: string, maxRetries = 20, delay = 1000): Promise<void> {
await this.checkForLocks(persistentId, maxRetries, delay)
}

private static async checkForLocks(
persistentId: string,
maxRetries: number,
delay: number
): Promise<void> {
let retry = 0

while (retry < maxRetries) {
const response = await DatasetHelper.getLocks(persistentId)
console.log('Checking locks: ' + JSON.stringify(response))

// Check if response has a property "0"
if (Object.keys(response).length === 1) {
console.log('No locks found.')
return
}

retry++
await this.wait(delay)
}

console.log('Max retries reached.')
throw new Error('Max retries reached.')
}
}
60 changes: 45 additions & 15 deletions tests/e2e-integration/shared/datasets/DatasetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ export class DatasetHelper extends DataverseApiHelper {
return this.request<DatasetResponse>(`/dataverses/root/datasets`, 'POST', newDatasetData)
}

static async publish(persistentId: string): Promise<{ status: string; persistentId: string }> {
const response = await this.request<{ status: string }>(
`/datasets/:persistentId/actions/:publish?persistentId=${persistentId}&type=major`,
'POST'
)
static async publish(persistentId: string): Promise<{
status: string
persistentId: string
}> {
const response = await this.request<{
status: string
}>(`/datasets/:persistentId/actions/:publish?persistentId=${persistentId}&type=major`, 'POST')

return { ...response, persistentId }
}

static async getLocks(persistentId: string): Promise<{
status: string
persistentId: string
}> {
const response = await this.request<{
status: string
}>(`/datasets/:persistentId/locks?persistentId=${persistentId}`, 'GET')

return { ...response, persistentId }
}
Expand All @@ -45,15 +58,20 @@ export class DatasetHelper extends DataverseApiHelper {
.click()
}

static async createPrivateUrl(id: string): Promise<{ token: string }> {
return this.request<{ token: string }>(`/datasets/${id}/privateUrl`, 'POST')
static async createPrivateUrl(id: string): Promise<{
token: string
}> {
return this.request<{
token: string
}>(`/datasets/${id}/privateUrl`, 'POST')
}

static async createPrivateUrlAnonymized(id: string): Promise<{ token: string }> {
return this.request<{ token: string }>(
`/datasets/${id}/privateUrl?anonymizedAccess=true`,
'POST'
)
static async createPrivateUrlAnonymized(id: string): Promise<{
token: string
}> {
return this.request<{
token: string
}>(`/datasets/${id}/privateUrl?anonymizedAccess=true`, 'POST')
}

static async createWithFiles(filesData: FileData[]): Promise<DatasetResponse> {
Expand Down Expand Up @@ -92,7 +110,15 @@ export class DatasetHelper extends DataverseApiHelper {
datasetPersistentId: string,
fileData: FileData
): Promise<DatasetFileResponse> {
const { files } = await this.request<{ files: [{ dataFile: { id: number } }] }>(
const { files } = await this.request<{
files: [
{
dataFile: {
id: number
}
}
]
}>(
`/datasets/:persistentId/add?persistentId=${datasetPersistentId}`,
'POST',
fileData,
Expand All @@ -108,8 +134,12 @@ export class DatasetHelper extends DataverseApiHelper {
static async setCitationDateFieldType(
persistentId: string,
fieldType: string
): Promise<{ status: string }> {
return this.request<{ status: string }>(
): Promise<{
status: string
}> {
return this.request<{
status: string
}>(
`/datasets/:persistentId/citationdate?persistentId=${persistentId}`,
'PUT',
fieldType,
Expand Down

0 comments on commit 8681f80

Please sign in to comment.