Skip to content

Commit 8ae3ac7

Browse files
committed
test: add integration cases
1 parent ac44d47 commit 8ae3ac7

File tree

6 files changed

+248
-112
lines changed

6 files changed

+248
-112
lines changed

src/datasets/domain/models/DatasetVersionSummary.ts

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export type Summary = {
1111
}
1212

1313
export interface SummaryUpdates {
14-
added: string // deberia ser number chequear
15-
deleted: string
16-
changed: string
14+
added: number
15+
deleted: number
16+
changed: number
1717
}
1818

1919
export interface SummaryUpdatesWithFields {
@@ -39,77 +39,3 @@ export enum SummaryKnownFields {
3939
files = 'files',
4040
termsAccessChanged = 'termsAccessChanged'
4141
}
42-
43-
// SummaryUpdates Example:
44-
/*
45-
summary: {
46-
"Geospatial Metadata": {
47-
added: 1,
48-
deleted: 0,
49-
changed: 0
50-
},
51-
}
52-
*/
53-
54-
// SummaryUpdatesWithFields Example:
55-
/*
56-
summary: {
57-
'Citation Metadata': {
58-
Description: {
59-
added: 0,
60-
deleted: 0,
61-
changed: 1
62-
},
63-
Title: {
64-
added: 0,
65-
deleted: 0,
66-
changed: 1
67-
}
68-
}
69-
}
70-
*/
71-
72-
// FilesSummaryUpdates Example:
73-
/*
74-
summary: {
75-
files: {
76-
added: 1,
77-
removed: 0,
78-
replaced: 0,
79-
changedFileMetaData: 2,
80-
changedVariableMetadata: 0
81-
},
82-
}
83-
*/
84-
85-
const version: DatasetVersionSummary = {
86-
id: 1,
87-
versionNumber: '1.0',
88-
contributors: 'John Doe',
89-
publishedOn: '2021-06-01',
90-
summary: {
91-
files: {
92-
added: 1,
93-
changedFileMetaData: 2,
94-
removed: 0,
95-
replaced: 0,
96-
changedVariableMetadata: 0
97-
},
98-
'Citation Metadata': {
99-
Title: {
100-
added: '0',
101-
deleted: '0',
102-
changed: '1'
103-
}
104-
},
105-
termsAccessChanged: true
106-
}
107-
}
108-
109-
console.log({
110-
typeof: typeof (
111-
(version.summary as Summary)?.['Citation Metadata'] as SummaryUpdatesWithFields
112-
)?.['Title'].changed
113-
})
114-
115-
// console.log({ typeof: typeof (version.summary as SummaryChanges)?.['Title'].changed })

test/integration/datasets/DatasetsRepository.test.ts

Lines changed: 198 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
deleteUnpublishedDatasetViaApi,
88
waitForDatasetsIndexedInSolr,
99
deletePublishedDatasetViaApi,
10-
deaccessionDatasetViaApi
10+
deaccessionDatasetViaApi,
11+
createDatasetLicenseModel
1112
} from '../../testHelpers/datasets/datasetHelper'
1213
import { ReadError } from '../../../src/core/domain/repositories/ReadError'
1314
import {
@@ -18,7 +19,8 @@ import {
1819
createDataset,
1920
CreatedDatasetIdentifiers,
2021
DatasetDTO,
21-
DatasetDeaccessionDTO
22+
DatasetDeaccessionDTO,
23+
publishDataset
2224
} from '../../../src/datasets'
2325
import { ApiConfig, WriteError } from '../../../src'
2426
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
@@ -31,9 +33,23 @@ import {
3133
import {
3234
createCollectionViaApi,
3335
deleteCollectionViaApi,
34-
ROOT_COLLECTION_ALIAS
36+
publishCollectionViaApi,
37+
ROOT_COLLECTION_ALIAS,
38+
setStorageDriverViaApi
3539
} from '../../testHelpers/collections/collectionHelper'
36-
import { testTextFile1Name, uploadFileViaApi } from '../../testHelpers/files/filesHelper'
40+
import {
41+
calculateBlobChecksum,
42+
createSinglepartFileBlob,
43+
testTextFile1Name,
44+
uploadFileViaApi
45+
} from '../../testHelpers/files/filesHelper'
46+
import {
47+
Summary,
48+
SummaryStringValues
49+
} from '../../../src/datasets/domain/models/DatasetVersionSummary'
50+
import { FilesRepository } from '../../../src/files/infra/repositories/FilesRepository'
51+
import { DirectUploadClient } from '../../../src/files/infra/clients/DirectUploadClient'
52+
import { createTestFileUploadDestination } from '../../testHelpers/files/fileUploadDestinationHelper'
3753

3854
const TEST_DIFF_DATASET_DTO: DatasetDTO = {
3955
license: {
@@ -79,6 +95,9 @@ describe('DatasetsRepository', () => {
7995
const sut: DatasetsRepository = new DatasetsRepository()
8096
const nonExistentTestDatasetId = 100
8197

98+
const filesRepositorySut = new FilesRepository()
99+
const directUploadSut: DirectUploadClient = new DirectUploadClient(filesRepositorySut)
100+
82101
beforeAll(async () => {
83102
ApiConfig.init(
84103
TestConstants.TEST_API_URL,
@@ -925,4 +944,179 @@ describe('DatasetsRepository', () => {
925944
).rejects.toBeInstanceOf(WriteError)
926945
})
927946
})
947+
948+
describe('getDatasetVersions', () => {
949+
const testDatasetVersionsCollectionAlias = 'testDatasetVersionsCollection'
950+
951+
beforeAll(async () => {
952+
await createCollectionViaApi(testDatasetVersionsCollectionAlias)
953+
await publishCollectionViaApi(testDatasetVersionsCollectionAlias)
954+
await setStorageDriverViaApi(testDatasetVersionsCollectionAlias, 'LocalStack')
955+
})
956+
957+
afterAll(async () => {
958+
await deleteCollectionViaApi(testDatasetVersionsCollectionAlias)
959+
})
960+
961+
test('should return dataset versions when dataset exists', async () => {
962+
const testDatasetIds = await createDataset.execute(
963+
TestConstants.TEST_NEW_DATASET_DTO,
964+
testDatasetVersionsCollectionAlias
965+
)
966+
967+
const actual = await sut.getDatasetVersions(testDatasetIds.numericId)
968+
969+
expect(actual.length).toBeGreaterThan(0)
970+
expect(actual[0].versionNumber).toBe('DRAFT')
971+
expect(actual[0].summary).toBe(SummaryStringValues.firstDraft)
972+
973+
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
974+
})
975+
976+
test('should return dataset versions correctly after first publish', async () => {
977+
const testDatasetIds = await createDataset.execute(
978+
TestConstants.TEST_NEW_DATASET_DTO,
979+
testDatasetVersionsCollectionAlias
980+
)
981+
await publishDataset.execute(testDatasetIds.numericId, VersionUpdateType.MAJOR)
982+
983+
await waitForNoLocks(testDatasetIds.numericId, 10)
984+
985+
const actual = await sut.getDatasetVersions(testDatasetIds.numericId)
986+
987+
expect(actual.length).toBeGreaterThan(0)
988+
expect(actual[0].versionNumber).toBe('1.0')
989+
expect(actual[0].summary).toBe(SummaryStringValues.firstPublished)
990+
991+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
992+
})
993+
994+
test('should return dataset versions correctly after 1st publish and metadata fields update', async () => {
995+
const testDatasetIds = await createDataset.execute(
996+
TestConstants.TEST_NEW_DATASET_DTO,
997+
testDatasetVersionsCollectionAlias
998+
)
999+
await publishDataset.execute(testDatasetIds.numericId, VersionUpdateType.MAJOR)
1000+
1001+
await waitForNoLocks(testDatasetIds.numericId, 10)
1002+
1003+
const metadataBlocksRepository = new MetadataBlocksRepository()
1004+
const citationMetadataBlock = await metadataBlocksRepository.getMetadataBlockByName(
1005+
'citation'
1006+
)
1007+
1008+
await sut.updateDataset(
1009+
testDatasetIds.numericId,
1010+
{
1011+
license: createDatasetLicenseModel(true),
1012+
metadataBlockValues: [
1013+
{
1014+
name: 'citation',
1015+
fields: {
1016+
title: 'Updated Dataset Title'
1017+
}
1018+
}
1019+
]
1020+
},
1021+
[citationMetadataBlock]
1022+
)
1023+
1024+
const actual = await sut.getDatasetVersions(testDatasetIds.numericId)
1025+
1026+
expect(actual.length).toEqual(2)
1027+
1028+
expect(actual[0].versionNumber).toBe('DRAFT')
1029+
expect(actual[0].summary).toMatchObject<Summary>({
1030+
'Citation Metadata': {
1031+
Title: {
1032+
added: 0,
1033+
deleted: 0,
1034+
changed: 1
1035+
}
1036+
},
1037+
files: {
1038+
added: 0,
1039+
removed: 0,
1040+
replaced: 0,
1041+
changedFileMetaData: 0,
1042+
changedVariableMetadata: 0
1043+
},
1044+
termsAccessChanged: false
1045+
})
1046+
1047+
expect(actual[1].versionNumber).toBe('1.0')
1048+
expect(actual[1].summary).toBe(SummaryStringValues.firstPublished)
1049+
1050+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
1051+
})
1052+
1053+
test('should return correct files summary', async () => {
1054+
const testDatasetIds = await createDataset.execute(
1055+
TestConstants.TEST_NEW_DATASET_DTO,
1056+
testDatasetVersionsCollectionAlias
1057+
)
1058+
await publishDataset.execute(testDatasetIds.numericId, VersionUpdateType.MAJOR)
1059+
1060+
await waitForNoLocks(testDatasetIds.numericId, 10)
1061+
1062+
const singlepartFile = await createSinglepartFileBlob()
1063+
1064+
const destination = await createTestFileUploadDestination(
1065+
singlepartFile,
1066+
testDatasetIds.numericId
1067+
)
1068+
1069+
const actualStorageId = await directUploadSut.uploadFile(
1070+
testDatasetIds.numericId,
1071+
singlepartFile,
1072+
jest.fn(),
1073+
new AbortController(),
1074+
destination
1075+
)
1076+
1077+
const fileArrayBuffer = await singlepartFile.arrayBuffer()
1078+
const fileBuffer = Buffer.from(fileArrayBuffer)
1079+
1080+
const uploadedFileDTO = {
1081+
fileName: singlepartFile.name,
1082+
storageId: actualStorageId,
1083+
checksumType: 'md5',
1084+
checksumValue: calculateBlobChecksum(fileBuffer, 'md5'),
1085+
mimeType: singlepartFile.type
1086+
}
1087+
1088+
await filesRepositorySut.addUploadedFilesToDataset(testDatasetIds.numericId, [
1089+
uploadedFileDTO
1090+
])
1091+
1092+
const actual = await sut.getDatasetVersions(testDatasetIds.numericId)
1093+
1094+
expect(actual.length).toEqual(2)
1095+
1096+
expect(actual[0].versionNumber).toBe('DRAFT')
1097+
expect(actual[0].summary).toMatchObject<Summary>({
1098+
files: {
1099+
added: 1,
1100+
removed: 0,
1101+
replaced: 0,
1102+
changedFileMetaData: 0,
1103+
changedVariableMetadata: 0
1104+
},
1105+
termsAccessChanged: false
1106+
})
1107+
1108+
expect(actual[1].versionNumber).toBe('1.0')
1109+
expect(actual[1].summary).toBe(SummaryStringValues.firstPublished)
1110+
1111+
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
1112+
})
1113+
1114+
test('should return error when dataset does not exist', async () => {
1115+
const expectedError = new ReadError(
1116+
`[404] Dataset with ID ${nonExistentTestDatasetId} not found.`
1117+
)
1118+
1119+
await expect(sut.getDatasetVersions(nonExistentTestDatasetId)).rejects.toThrow(expectedError)
1120+
})
1121+
})
9281122
})

test/integration/files/DirectUpload.test.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import {
1515
setStorageDriverViaApi
1616
} from '../../testHelpers/collections/collectionHelper'
1717
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
18-
import axios from 'axios'
18+
1919
import {
20+
calculateBlobChecksum,
2021
createMultipartFileBlob,
21-
createSinglepartFileBlob
22+
createSinglepartFileBlob,
23+
singlepartFileExistsInBucket
2224
} from '../../testHelpers/files/filesHelper'
2325
import { FileUploadCancelError } from '../../../src/files/infra/clients/errors/FileUploadCancelError'
24-
import * as crypto from 'crypto'
26+
import { createTestFileUploadDestination } from '../../testHelpers/files/fileUploadDestinationHelper'
2527

2628
describe('Direct Upload', () => {
2729
const testCollectionAlias = 'directUploadTestCollection'
@@ -113,7 +115,7 @@ describe('Direct Upload', () => {
113115
fileName: singlepartFile.name,
114116
storageId: actualStorageId,
115117
checksumType: checksumAlgorithm,
116-
checksumValue: calculateBlobChecksum(fileBuffer),
118+
checksumValue: calculateBlobChecksum(fileBuffer, checksumAlgorithm),
117119
mimeType: singlepartFile.type
118120
}
119121

@@ -170,7 +172,7 @@ describe('Direct Upload', () => {
170172
fileName: multipartFile.name,
171173
storageId: actualStorageId,
172174
checksumType: checksumAlgorithm,
173-
checksumValue: calculateBlobChecksum(fileBuffer),
175+
checksumValue: calculateBlobChecksum(fileBuffer, checksumAlgorithm),
174176
mimeType: multipartFile.type
175177
}
176178

@@ -212,30 +214,4 @@ describe('Direct Upload', () => {
212214
)
213215
).rejects.toThrow(FileUploadCancelError)
214216
})
215-
216-
const createTestFileUploadDestination = async (file: File, testDatasetId: number) => {
217-
const filesRepository = new FilesRepository()
218-
const destination = await filesRepository.getFileUploadDestination(testDatasetId, file)
219-
destination.urls.forEach((destinationUrl, index) => {
220-
destination.urls[index] = destinationUrl.replace('localstack', 'localhost')
221-
})
222-
return destination
223-
}
224-
225-
const singlepartFileExistsInBucket = async (fileUrl: string): Promise<boolean> => {
226-
return axios
227-
.get(fileUrl)
228-
.then(() => {
229-
return true
230-
})
231-
.catch(() => {
232-
return false
233-
})
234-
}
235-
236-
const calculateBlobChecksum = (blob: Buffer): string => {
237-
const hash = crypto.createHash(checksumAlgorithm)
238-
hash.update(blob)
239-
return hash.digest('hex')
240-
}
241217
})

0 commit comments

Comments
 (0)