Skip to content

Commit

Permalink
feat: test case for tabular data
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengShi-1 committed Jan 8, 2025
1 parent 4321ffd commit 1826b46
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 5 deletions.
163 changes: 158 additions & 5 deletions test/integration/collections/CollectionsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import {
ROOT_COLLECTION_ALIAS
} from '../../testHelpers/collections/collectionHelper'
import { CollectionPayload } from '../../../src/collections/infra/repositories/transformers/CollectionPayload'
import { uploadFileViaApi } from '../../testHelpers/files/filesHelper'
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
import { updateFileTabularTags, uploadFileViaApi } from '../../testHelpers/files/filesHelper'
import {
deletePublishedDatasetViaApi,
deleteUnpublishedDatasetViaApi,
publishDatasetViaApi
} from '../../testHelpers/datasets/datasetHelper'
import { PublicationStatus } from '../../../src/core/domain/models/PublicationStatus'
import { CollectionType } from '../../../src/collections/domain/models/CollectionType'
import {
Expand All @@ -33,7 +37,7 @@ describe('CollectionsRepository', () => {
const testCollectionAlias = 'collectionsRepositoryTestCollection'
const sut: CollectionsRepository = new CollectionsRepository()
let testCollectionId: number

const currentYear = new Date().getFullYear()
beforeAll(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
Expand Down Expand Up @@ -292,8 +296,7 @@ describe('CollectionsRepository', () => {
const actualCollectionPreview = actual.items[2] as CollectionPreview

const expectedFileMd5 = '68b22040025784da775f55cfcb6dee2e'
const expectedDatasetCitationFragment =
'Admin, Dataverse; Owner, Dataverse, 2025, "Dataset created using the createDataset use case'
const expectedDatasetCitationFragment = `Admin, Dataverse; Owner, Dataverse, ${currentYear}, "Dataset created using the createDataset use case"`
const expectedDatasetDescription = 'Dataset created using the createDataset use case'
const expectedFileName = 'test-file-1.txt'
const expectedCollectionsName = 'Scientific Research'
Expand Down Expand Up @@ -660,6 +663,156 @@ describe('CollectionsRepository', () => {
})
})

describe('getCollectionItems for published tabular file', () => {
let testDatasetIds: CreatedDatasetIdentifiers
const testTextFile4Name = 'test-file-4.tab'
const testSubCollectionAlias = 'collectionsRepositoryTestSubCollection'

beforeAll(async () => {
await sut.publishCollection(testCollectionId).catch(() => {
throw new Error(`Tests beforeAll(): Error while publishing collection ${testCollectionId}`)
})

const collectionPayload = await createCollectionViaApi(
testSubCollectionAlias,
testCollectionAlias
).catch(() => {
throw new Error(
`Tests beforeAll(): Error while creating subcollection ${testSubCollectionAlias}`
)
})

await sut.publishCollection(collectionPayload.id).catch(() => {
throw new Error(`Tests beforeAll(): Error while publishing collection ${testCollectionId}`)
})

try {
testDatasetIds = await createDataset.execute(
TestConstants.TEST_NEW_DATASET_DTO,
testSubCollectionAlias
)
} catch (error) {
throw new Error('Tests beforeAll(): Error while creating test dataset')
}
const uploadFileViaApiResult = await uploadFileViaApi(
testDatasetIds.numericId,
testTextFile4Name,
{
categories: ['tabular data']
}
).catch(() => {
throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile4Name}`)
})
await new Promise((resolve) => setTimeout(resolve, 5000))

await updateFileTabularTags(uploadFileViaApiResult.data.data.files[0].dataFile.id, [
'Survey',
'Genomics'
]).catch(() => {
throw new Error(
`Tests beforeAll(): Error while updating file tabular tags ${uploadFileViaApiResult.data.data.files[0].dataFile.id}`
)
})

await publishDatasetViaApi(testDatasetIds.numericId).catch(() => {
throw new Error(
`Tests beforeAll(): Error while publishing dataset ${testDatasetIds.numericId}`
)
})
})

afterAll(async () => {
try {
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
} catch (error) {
throw new Error(
`Tests afterAll(): Error while deleting test dataset ${testDatasetIds.persistentId}`
)
}
try {
await deleteCollectionViaApi(testSubCollectionAlias)
} catch (error) {
throw new Error(
`Tests afterAll(): Error while deleting subcollection ${testSubCollectionAlias}`
)
}
})

test('should return collection items given a valid collection alias', async () => {
// Give enough time to Solr for indexing
await new Promise((resolve) => setTimeout(resolve, 5000))

const actual = await sut.getCollectionItems(testCollectionAlias)
const actualFilePreview = actual.items[1] as FilePreview
const actualDatasetPreview = actual.items[0] as DatasetPreview
const actualCollectionPreview = actual.items[2] as CollectionPreview

const expectedFileMd5 = '77c7f03a7d7772907b43f0b322cef723'

const expectedDatasetCitationFragment = `Admin, Dataverse; Owner, Dataverse, ${currentYear}, "Dataset created using the createDataset use case`
const expectedDatasetDescription = 'Dataset created using the createDataset use case'
const expectedFileName = 'test-file-4.tab'
const expectedCollectionsName = 'Scientific Research'

expect(actualFilePreview.checksum?.type).toBe('MD5')
expect(actualFilePreview.checksum?.value).toBe(expectedFileMd5)
expect(actualFilePreview.datasetCitation).toContain(expectedDatasetCitationFragment)
expect(actualFilePreview.datasetId).toBe(testDatasetIds.numericId)
expect(actualFilePreview.datasetName).toBe(expectedDatasetDescription)
expect(actualFilePreview.datasetPersistentId).toBe(testDatasetIds.persistentId)
expect(actualFilePreview.description).toBe('')
expect(actualFilePreview.fileContentType).toBe('text/tab-separated-values')
expect(actualFilePreview.fileId).not.toBeUndefined()
expect(actualFilePreview.fileType).toBe('Tab-Delimited')
expect(actualFilePreview.md5).toBe(expectedFileMd5)
expect(actualFilePreview.name).toBe(expectedFileName)
expect(actualFilePreview.publicationStatuses[0]).toBe(PublicationStatus.Published)
expect(actualFilePreview.sizeInBytes).toBe(137)
expect(actualFilePreview.url).not.toBeUndefined()
expect(actualFilePreview.releaseOrCreateDate).not.toBeUndefined()
expect(actualFilePreview.type).toBe(CollectionItemType.FILE)
expect(actualFilePreview.restricted).toBe(false)
expect(actualFilePreview.canDownloadFile).toBe(true)
expect(actualFilePreview.categories).toEqual(['tabular data'])
expect(actualFilePreview.tabularTags).toEqual(['Genomics', 'Survey'])
expect(actualFilePreview.observations).toBe(10)
expect(actualFilePreview.variables).toBe(3)

expect(actualDatasetPreview.title).toBe(expectedDatasetDescription)
expect(actualDatasetPreview.citation).toContain(expectedDatasetCitationFragment)
expect(actualDatasetPreview.description).toBe('This is the description of the dataset.')
expect(actualDatasetPreview.persistentId).not.toBeUndefined()
expect(actualDatasetPreview.persistentId).not.toBeUndefined()
expect(actualDatasetPreview.publicationStatuses[0]).toBe(PublicationStatus.Published)
expect(actualDatasetPreview.versionId).not.toBeUndefined()
expect(actualDatasetPreview.versionInfo.createTime).not.toBeUndefined()
expect(actualDatasetPreview.versionInfo.lastUpdateTime).not.toBeUndefined()
expect(actualDatasetPreview.versionInfo.majorNumber).toBe(1)
expect(actualDatasetPreview.versionInfo.minorNumber).toBe(0)
expect(actualDatasetPreview.versionInfo.state).toBe('RELEASED')
expect(actualDatasetPreview.parentCollectionAlias).toBe(
'collectionsRepositoryTestSubCollection'
)
expect(actualDatasetPreview.parentCollectionName).toBe(expectedCollectionsName)
expect(actualDatasetPreview.type).toBe(CollectionItemType.DATASET)

expect(actualCollectionPreview.name).toBe(expectedCollectionsName)
expect(actualCollectionPreview.alias).toBe(testSubCollectionAlias)
expect(actualCollectionPreview.description).toBe('We do all the science.')
expect(actualCollectionPreview.imageUrl).toBe(undefined)
expect(actualCollectionPreview.parentAlias).toBe(testCollectionAlias)
expect(actualCollectionPreview.parentName).toBe(expectedCollectionsName)
expect(actualCollectionPreview.publicationStatuses[0]).toBe(PublicationStatus.Published)
expect(actualCollectionPreview.releaseOrCreateDate).not.toBeUndefined()
expect(actualCollectionPreview.affiliation).toBe('Scientific Research University')
expect(actualCollectionPreview.parentAlias).toBe('collectionsRepositoryTestCollection')
expect(actualCollectionPreview.parentName).toBe(expectedCollectionsName)
expect(actualCollectionPreview.type).toBe(CollectionItemType.COLLECTION)

expect(actual.totalItemCount).toBe(3)
})
})

describe('updateCollection', () => {
const testUpdatedCollectionAlias = 'updateCollection-test-updatedAlias'

Expand Down
16 changes: 16 additions & 0 deletions test/testHelpers/files/filesHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,19 @@ async function createBlobWithSize(size: number): Promise<Blob> {
const arrayBuffer = new ArrayBuffer(size)
return new Blob([arrayBuffer])
}

export const updateFileTabularTags = async (
fileId: number,
tabularTags: string[]
): Promise<AxiosResponse> => {
return await axios.post(
`${TestConstants.TEST_API_URL}/files/${fileId}/metadata/tabularTags`,
{ tabularTags },
{
headers: {
'Content-type': 'application/json',
'X-Dataverse-Key': process.env.TEST_API_KEY
}
}
)
}

0 comments on commit 1826b46

Please sign in to comment.