-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGetMyDataCollectionItems.test.ts
More file actions
201 lines (193 loc) · 6.42 KB
/
Copy pathGetMyDataCollectionItems.test.ts
File metadata and controls
201 lines (193 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {
ApiConfig,
createDataset,
CreatedDatasetIdentifiers,
CollectionPreview,
getMyDataCollectionItems,
ReadError
} from '../../../src'
import { TestConstants } from '../../testHelpers/TestConstants'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import {
createCollectionViaApi,
deleteCollectionViaApi
} from '../../testHelpers/collections/collectionHelper'
import { uploadFileViaApi } from '../../testHelpers/files/filesHelper'
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
import { CollectionItemType } from '../../../src/collections/domain/models/CollectionItemType'
import { PublicationStatus } from '../../../src/core/domain/models/PublicationStatus'
const testRoleIds = [1, 2, 3, 4, 5, 6, 7, 8]
const testCollectionItemTypes = [
CollectionItemType.COLLECTION,
CollectionItemType.DATASET,
CollectionItemType.FILE
]
const testPublishingStatuses = [
PublicationStatus.Published,
PublicationStatus.Draft,
PublicationStatus.Unpublished
]
describe('execute', () => {
const testCollectionAlias = 'collectionsRepositoryGetMyDataCollection'
let testDatasetIds: CreatedDatasetIdentifiers
const testTextFile1Name = 'test-file-2.txt'
beforeAll(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.API_KEY,
process.env.TEST_API_KEY
)
await createCollectionViaApi(testCollectionAlias)
try {
testDatasetIds = await createDataset.execute(
TestConstants.TEST_NEW_DATASET_DTO,
testCollectionAlias
)
} catch (error) {
throw new Error('Tests beforeAll(): Error while creating test dataset')
}
await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => {
throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`)
})
})
afterAll(async () => {
try {
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId)
} catch (error) {
throw new Error('Tests afterAll(): Error while deleting test dataset')
}
try {
await deleteCollectionViaApi(testCollectionAlias)
} catch (error) {
throw new Error('Tests afterAll(): Error while deleting test collection')
}
})
test('should return error message when repository returns empty item subset', async () => {
expect.assertions(2)
let readError: ReadError | undefined = undefined
try {
await getMyDataCollectionItems.execute(
testRoleIds,
testCollectionItemTypes,
[PublicationStatus.Deaccessioned],
undefined,
undefined,
undefined
)
throw new Error('Use case should throw an error')
} catch (error) {
readError = error as ReadError
} finally {
expect(readError).toBeInstanceOf(ReadError)
expect(readError?.message).toEqual(
'There was an error when reading the resource. Reason was: Sorry, no results were found.'
)
}
}),
test('should return items when valid roles,collection types, and publishingStatuses are provided', async () => {
// Give enough time to Solr for indexing
await new Promise((resolve) => setTimeout(resolve, 5000))
try {
const actual = await getMyDataCollectionItems.execute(
testRoleIds,
testCollectionItemTypes,
testPublishingStatuses,
undefined,
undefined,
testCollectionAlias
)
const actualCollectionPreview = actual.items[0] as CollectionPreview
expect(actualCollectionPreview.alias).toBe(testCollectionAlias)
expect(actual.totalItemCount).toBe(1)
expect(actual.publicationStatusCounts).toEqual([
{
publicationStatus: 'Published',
count: 0
},
{
publicationStatus: 'Unpublished',
count: 1
},
{
publicationStatus: 'Draft',
count: 0
},
{
publicationStatus: 'In Review',
count: 0
},
{
publicationStatus: 'Deaccessioned',
count: 0
}
])
expect(actual.countPerObjectType).toEqual({
collections: 1,
datasets: 0,
files: 0
})
} catch (error) {
console.log(error)
throw new Error('Item subset should be retrieved')
}
})
test('should return an error message when no role is specified', async () => {
expect.assertions(2)
let readError: ReadError | undefined = undefined
try {
await getMyDataCollectionItems.execute([], [], [], undefined, undefined, undefined)
throw new Error('Use case should throw an error')
} catch (error) {
readError = error as ReadError
} finally {
expect(readError).toBeInstanceOf(ReadError)
expect(readError?.message).toEqual(
`There was an error when reading the resource. Reason was: No results. Please select at least one Role.`
)
}
})
test('should return an error message when no publication status is specified', async () => {
expect.assertions(2)
let readError: ReadError | undefined = undefined
try {
await getMyDataCollectionItems.execute(
testRoleIds,
testCollectionItemTypes,
[],
undefined,
undefined,
undefined
)
throw new Error('Use case should throw an error')
} catch (error) {
readError = error as ReadError
} finally {
expect(readError).toBeInstanceOf(ReadError)
expect(readError?.message).toEqual(
`There was an error when reading the resource. Reason was: No user found for: "Published, Unpublished, Draft, In Review, Deaccessioned"`
)
}
})
test('should an error message when no collection type is specified', async () => {
expect.assertions(2)
let readError: ReadError | undefined = undefined
try {
await getMyDataCollectionItems.execute(
testRoleIds,
testCollectionItemTypes,
[],
undefined,
undefined,
undefined
)
throw new Error('Use case should throw an error')
} catch (error) {
readError = error as ReadError
} finally {
expect(readError).toBeInstanceOf(ReadError)
expect(readError?.message).toEqual(
`There was an error when reading the resource. Reason was: No user found for: "Published, Unpublished, Draft, In Review, Deaccessioned"`
)
}
})
})