Skip to content

Commit 2d54907

Browse files
committed
Added: getCollectionMetadataBlocks functional tests and tweaks
1 parent 0033085 commit 2d54907

File tree

3 files changed

+111
-28
lines changed

3 files changed

+111
-28
lines changed

docs/useCases.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The different use cases currently available in the package are classified below,
3737
- [Metadata Blocks](#metadata-blocks)
3838
- [Metadata Blocks read use cases](#metadata-blocks-read-use-cases)
3939
- [Get Metadata Block By Name](#get-metadata-block-by-name)
40+
- [Get Collection Metadata Blocks](#get-collection-metadata-blocks)
4041
- [Users](#Users)
4142
- [Users read use cases](#users-read-use-cases)
4243
- [Get Current Authenticated User](#get-current-authenticated-user)
@@ -730,6 +731,32 @@ getMetadataBlockByName.execute(name).then((metadataBlock: MetadataBlock) => {
730731

731732
_See [use case](../src/metadataBlocks/domain/useCases/GetMetadataBlockByName.ts) implementation_.
732733

734+
#### Get Collection Metadata Blocks
735+
736+
Returns a [MetadataBlock](../src/metadataBlocks/domain/models/MetadataBlock.ts) array containing the metadata blocks from the requested collection.
737+
738+
##### Example call:
739+
740+
```typescript
741+
import { getCollectionMetadataBlocks } from '@iqss/dataverse-client-javascript'
742+
743+
/* ... */
744+
745+
const collectionIdOrAlias = 'citation'
746+
747+
getCollectionMetadataBlocks.execute(collectionAlias).then((metadataBlocks: MetadataBlock[]) => {
748+
/* ... */
749+
})
750+
751+
/* ... */
752+
```
753+
754+
_See [use case](../src/metadataBlocks/domain/useCases/GetCollectionMetadataBlocks.ts) implementation_.
755+
756+
The `collectionIdOrAlias` is a generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId).
757+
758+
There is a second optional parameter called `onlyDisplayedOnCreate` which indicates whether or not to return only the metadata blocks that are displayed on dataset creation. The default value is false.
759+
733760
## Users
734761

735762
### Users read use cases
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ApiConfig, getCollectionMetadataBlocks, MetadataBlock, ReadError } from '../../../src'
2+
import { TestConstants } from '../../testHelpers/TestConstants'
3+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
4+
5+
describe('execute', () => {
6+
beforeEach(async () => {
7+
ApiConfig.init(
8+
TestConstants.TEST_API_URL,
9+
DataverseApiAuthMechanism.API_KEY,
10+
process.env.TEST_API_KEY
11+
)
12+
})
13+
14+
test('should successfully return collection metadatablocks when collection exists', async () => {
15+
let collectionMetadataBlocks: MetadataBlock[] = null
16+
try {
17+
collectionMetadataBlocks = await getCollectionMetadataBlocks.execute('root')
18+
} catch (error) {
19+
throw new Error('Should not raise an error')
20+
} finally {
21+
expect(collectionMetadataBlocks).not.toBeNull()
22+
expect(collectionMetadataBlocks.length).toBe(1)
23+
expect(collectionMetadataBlocks[0].metadataFields.title.name).toBe('title')
24+
}
25+
})
26+
27+
test('should throw an error when a collection is not found', async () => {
28+
expect.assertions(2)
29+
let readError: ReadError
30+
try {
31+
await getCollectionMetadataBlocks.execute('notFoundCollectionAlias')
32+
throw new Error('Use case should throw an error')
33+
} catch (error) {
34+
readError = error
35+
} finally {
36+
expect(readError).toBeInstanceOf(ReadError)
37+
expect(readError.message).toEqual(
38+
`There was an error when reading the resource. Reason was: [404] Can't find dataverse with identifier='notFoundCollectionAlias'`
39+
)
40+
}
41+
})
42+
})

test/testHelpers/datasets/newDatasetHelper.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import {
22
NewDatasetDTO,
33
NewDatasetMetadataFieldValueDTO
44
} from '../../../src/datasets/domain/dtos/NewDatasetDTO'
5-
import { DatasetLicense, MetadataBlock } from '../../../src'
5+
import { DatasetLicense, MetadataBlock, MetadataFieldType } from '../../../src'
66
import { NewDatasetRequestPayload } from '../../../src/datasets/infra/repositories/transformers/newDatasetTransformers'
7+
import {
8+
MetadataFieldWatermark,
9+
MetadataFieldTypeClass
10+
} from '../../../src/metadataBlocks/domain/models/MetadataBlock'
711

812
export const createNewDatasetDTO = (
913
titleFieldValue?: NewDatasetMetadataFieldValueDTO,
@@ -102,121 +106,130 @@ export const createNewDatasetMetadataBlockModel = (): MetadataBlock => {
102106
id: 1,
103107
name: 'citation',
104108
displayName: 'Citation Metadata',
109+
displayOnCreate: true,
105110
metadataFields: {
106111
title: {
107112
name: 'title',
108113
displayName: 'title',
109114
title: 'title',
110-
type: 'DatasetField',
111-
watermark: 'watermark',
115+
type: MetadataFieldType.Text,
116+
watermark: MetadataFieldWatermark.Empty,
112117
description: 'description',
113118
multiple: false,
114119
isControlledVocabulary: false,
115120
displayFormat: '#VALUE',
116121
isRequired: true,
117122
displayOrder: 0,
118-
typeClass: 'primitive'
123+
displayOnCreate: true,
124+
typeClass: MetadataFieldTypeClass.Primitive
119125
},
120126
author: {
121127
name: 'author',
122128
displayName: 'author',
123129
title: 'author',
124-
type: 'NONE',
125-
watermark: 'watermark',
130+
type: MetadataFieldType.None,
131+
watermark: MetadataFieldWatermark.Empty,
126132
description: 'description',
127133
multiple: true,
128134
isControlledVocabulary: false,
129135
displayFormat: '#VALUE',
130136
isRequired: true,
131137
displayOrder: 1,
132-
typeClass: 'compound',
138+
typeClass: MetadataFieldTypeClass.Compound,
139+
displayOnCreate: true,
133140
childMetadataFields: {
134141
authorName: {
135142
name: 'authorName',
136143
displayName: 'author name',
137144
title: 'author name',
138-
type: 'TEXT',
139-
watermark: 'watermark',
145+
type: MetadataFieldType.Text,
146+
watermark: MetadataFieldWatermark.Empty,
140147
description: 'description',
141148
multiple: false,
142149
isControlledVocabulary: false,
143150
displayFormat: '#VALUE',
144151
isRequired: true,
145152
displayOrder: 2,
146-
typeClass: 'primitive'
153+
displayOnCreate: true,
154+
typeClass: MetadataFieldTypeClass.Primitive
147155
},
148156
authorAffiliation: {
149157
name: 'authorAffiliation',
150158
displayName: 'author affiliation',
151159
title: 'author affiliation',
152-
type: 'TEXT',
153-
watermark: 'watermark',
160+
type: MetadataFieldType.Text,
161+
watermark: MetadataFieldWatermark.Empty,
154162
description: 'descriprion',
155163
multiple: false,
156164
isControlledVocabulary: false,
157165
displayFormat: '#VALUE',
158166
isRequired: false,
159167
displayOrder: 3,
160-
typeClass: 'primitive'
168+
displayOnCreate: true,
169+
typeClass: MetadataFieldTypeClass.Primitive
161170
}
162171
}
163172
},
164173
alternativeRequiredTitle: {
165174
name: 'alternativeRequiredTitle',
166175
displayName: 'Alternative Required Title',
167176
title: 'Alternative Title',
168-
type: 'TEXT',
169-
watermark: '',
177+
type: MetadataFieldType.Text,
178+
watermark: MetadataFieldWatermark.Empty,
170179
description:
171180
'Either 1) a title commonly used to refer to the Dataset or 2) an abbreviation of the main title',
172181
multiple: true,
173182
isControlledVocabulary: false,
174183
displayFormat: '',
175184
isRequired: true,
176185
displayOrder: 4,
177-
typeClass: 'primitive'
186+
displayOnCreate: true,
187+
typeClass: MetadataFieldTypeClass.Primitive
178188
},
179189
timePeriodCoveredStart: {
180190
name: 'timePeriodCoveredStart',
181191
displayName: 'Time Period Start Date',
182192
title: 'Start Date',
183-
type: 'DATE',
184-
watermark: 'YYYY-MM-DD',
193+
type: MetadataFieldType.Date,
194+
watermark: MetadataFieldWatermark.Empty,
185195
description: 'The start date of the time period that the data refer to',
186196
multiple: false,
187197
isControlledVocabulary: false,
188198
displayFormat: '#NAME: #VALUE ',
189199
isRequired: false,
190200
displayOrder: 5,
191-
typeClass: 'primitive'
201+
displayOnCreate: true,
202+
typeClass: MetadataFieldTypeClass.Primitive
192203
},
193204
contributor: {
194205
name: 'contributor',
195206
displayName: 'Contributor',
196207
title: 'Contributor',
197-
type: 'NONE',
198-
watermark: '',
208+
type: MetadataFieldType.None,
209+
watermark: MetadataFieldWatermark.Empty,
199210
description:
200211
'The entity, such as a person or organization, responsible for collecting, managing, or otherwise contributing to the development of the Dataset',
201212
multiple: true,
202213
isControlledVocabulary: false,
203214
displayFormat: ':',
204215
isRequired: false,
205216
displayOrder: 6,
206-
typeClass: 'compound',
217+
typeClass: MetadataFieldTypeClass.Compound,
218+
displayOnCreate: true,
207219
childMetadataFields: {
208220
contributorType: {
209221
name: 'contributorType',
210222
displayName: 'Contributor Type',
211223
title: 'Type',
212-
type: 'TEXT',
213-
watermark: '',
224+
type: MetadataFieldType.Text,
225+
watermark: MetadataFieldWatermark.Empty,
214226
description: 'Indicates the type of contribution made to the dataset',
215227
multiple: false,
216228
isControlledVocabulary: true,
217229
displayFormat: '#VALUE ',
218230
isRequired: false,
219231
displayOrder: 7,
232+
displayOnCreate: true,
220233
controlledVocabularyValues: [
221234
'Data Collector',
222235
'Data Curator',
@@ -236,22 +249,23 @@ export const createNewDatasetMetadataBlockModel = (): MetadataBlock => {
236249
'Work Package Leader',
237250
'Other'
238251
],
239-
typeClass: 'controlledVocabulary'
252+
typeClass: MetadataFieldTypeClass.ControlledVocabulary
240253
},
241254
contributorName: {
242255
name: 'contributorName',
243256
displayName: 'Contributor Name',
244257
title: 'Name',
245-
type: 'TEXT',
246-
watermark: '1) FamilyName, GivenName or 2) Organization',
258+
type: MetadataFieldType.Text,
259+
watermark: MetadataFieldWatermark.Empty,
247260
description:
248261
"The name of the contributor, e.g. the person's name or the name of an organization",
249262
multiple: false,
250263
isControlledVocabulary: false,
251264
displayFormat: '#VALUE',
252265
isRequired: true,
253266
displayOrder: 8,
254-
typeClass: 'primitive'
267+
typeClass: MetadataFieldTypeClass.Primitive,
268+
displayOnCreate: true
255269
}
256270
}
257271
}

0 commit comments

Comments
 (0)