-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGetFileCitationByFormat.test.ts
More file actions
123 lines (99 loc) · 4.06 KB
/
GetFileCitationByFormat.test.ts
File metadata and controls
123 lines (99 loc) · 4.06 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
import {
ApiConfig,
createDataset,
CreatedDatasetIdentifiers,
FileCitationFormat,
getDatasetFiles,
getFileCitationByFormat,
ReadError
} from '../../../src'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import {
createCollectionViaApi,
deleteCollectionViaApi
} from '../../testHelpers/collections/collectionHelper'
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
import { uploadFileViaApi } from '../../testHelpers/files/filesHelper'
import { TestConstants } from '../../testHelpers/TestConstants'
describe('execute', () => {
const testCollectionAlias = 'getFileCitationByFormatFunctionalTest'
const testTextFile1Name = 'test-file-1.txt'
let testDatasetIds: CreatedDatasetIdentifiers
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')
}
})
const getTestFileId = async (): Promise<number> => {
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId)
return datasetFiles.files[0].id
}
test('should successfully get file citation in EndNote (XML) format', async () => {
const fileId = await getTestFileId()
const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.ENDNOTE)
expect(typeof citation).toBe('string')
expect(citation.trimStart()).toMatch(/^<\?xml/)
})
test('should successfully get file citation in RIS (plain text) format', async () => {
const fileId = await getTestFileId()
const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.RIS)
expect(typeof citation).toBe('string')
// RIS records use TY (type) and ER (end of record) tags
expect(citation).toMatch(/TY\s+-/)
expect(citation).toMatch(/ER\s+-/)
})
test('should successfully get file citation in BibTeX (plain text) format', async () => {
const fileId = await getTestFileId()
const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.BIBTEX)
expect(typeof citation).toBe('string')
// BibTeX entries start with @<entry-type>{
expect(citation.trimStart()).toMatch(/^@\w+\{/)
})
test('should successfully get file citation in CSL (JSON) format', async () => {
const fileId = await getTestFileId()
const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.CSL)
expect(typeof citation).toBe('string')
const parsed = JSON.parse(citation)
expect(typeof parsed).toBe('object')
expect(parsed).not.toBeNull()
})
test('should successfully get file citation in Internal (HTML) format', async () => {
const fileId = await getTestFileId()
const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.INTERNAL)
expect(typeof citation).toBe('string')
// Internal HTML format includes anchor tags linking to the dataset
expect(citation).toMatch(/<a\s+href=/i)
})
test('should throw an error when the file id does not exist', async () => {
const nonExistentFileId = 5
await expect(
getFileCitationByFormat.execute(nonExistentFileId, FileCitationFormat.BIBTEX)
).rejects.toThrow(ReadError)
})
})