|
| 1 | +import { |
| 2 | + ApiConfig, |
| 3 | + createDataset, |
| 4 | + CreatedDatasetIdentifiers, |
| 5 | + FileCitationFormat, |
| 6 | + getDatasetFiles, |
| 7 | + getFileCitationByFormat, |
| 8 | + ReadError |
| 9 | +} from '../../../src' |
| 10 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 11 | +import { |
| 12 | + createCollectionViaApi, |
| 13 | + deleteCollectionViaApi |
| 14 | +} from '../../testHelpers/collections/collectionHelper' |
| 15 | +import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' |
| 16 | +import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' |
| 17 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 18 | + |
| 19 | +describe('execute', () => { |
| 20 | + const testCollectionAlias = 'getFileCitationByFormatFunctionalTest' |
| 21 | + const testTextFile1Name = 'test-file-1.txt' |
| 22 | + let testDatasetIds: CreatedDatasetIdentifiers |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + ApiConfig.init( |
| 26 | + TestConstants.TEST_API_URL, |
| 27 | + DataverseApiAuthMechanism.API_KEY, |
| 28 | + process.env.TEST_API_KEY |
| 29 | + ) |
| 30 | + await createCollectionViaApi(testCollectionAlias) |
| 31 | + |
| 32 | + try { |
| 33 | + testDatasetIds = await createDataset.execute( |
| 34 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 35 | + testCollectionAlias |
| 36 | + ) |
| 37 | + } catch (error) { |
| 38 | + throw new Error('Tests beforeAll(): Error while creating test dataset') |
| 39 | + } |
| 40 | + |
| 41 | + await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => { |
| 42 | + throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + afterAll(async () => { |
| 47 | + try { |
| 48 | + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) |
| 49 | + } catch (error) { |
| 50 | + throw new Error('Tests afterAll(): Error while deleting test dataset') |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + await deleteCollectionViaApi(testCollectionAlias) |
| 55 | + } catch (error) { |
| 56 | + throw new Error('Tests afterAll(): Error while deleting test collection') |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + const getTestFileId = async (): Promise<number> => { |
| 61 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 62 | + return datasetFiles.files[0].id |
| 63 | + } |
| 64 | + |
| 65 | + test('should successfully get file citation in EndNote (XML) format', async () => { |
| 66 | + const fileId = await getTestFileId() |
| 67 | + |
| 68 | + const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.ENDNOTE) |
| 69 | + |
| 70 | + expect(typeof citation).toBe('string') |
| 71 | + expect(citation.trimStart()).toMatch(/^<\?xml/) |
| 72 | + }) |
| 73 | + |
| 74 | + test('should successfully get file citation in RIS (plain text) format', async () => { |
| 75 | + const fileId = await getTestFileId() |
| 76 | + |
| 77 | + const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.RIS) |
| 78 | + |
| 79 | + expect(typeof citation).toBe('string') |
| 80 | + // RIS records use TY (type) and ER (end of record) tags |
| 81 | + expect(citation).toMatch(/TY\s+-/) |
| 82 | + expect(citation).toMatch(/ER\s+-/) |
| 83 | + }) |
| 84 | + |
| 85 | + test('should successfully get file citation in BibTeX (plain text) format', async () => { |
| 86 | + const fileId = await getTestFileId() |
| 87 | + |
| 88 | + const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.BIBTEX) |
| 89 | + |
| 90 | + expect(typeof citation).toBe('string') |
| 91 | + // BibTeX entries start with @<entry-type>{ |
| 92 | + expect(citation.trimStart()).toMatch(/^@\w+\{/) |
| 93 | + }) |
| 94 | + |
| 95 | + test('should successfully get file citation in CSL (JSON) format', async () => { |
| 96 | + const fileId = await getTestFileId() |
| 97 | + |
| 98 | + const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.CSL) |
| 99 | + |
| 100 | + expect(typeof citation).toBe('string') |
| 101 | + const parsed = JSON.parse(citation) |
| 102 | + expect(typeof parsed).toBe('object') |
| 103 | + expect(parsed).not.toBeNull() |
| 104 | + }) |
| 105 | + |
| 106 | + test('should successfully get file citation in Internal (HTML) format', async () => { |
| 107 | + const fileId = await getTestFileId() |
| 108 | + |
| 109 | + const citation = await getFileCitationByFormat.execute(fileId, FileCitationFormat.INTERNAL) |
| 110 | + |
| 111 | + expect(typeof citation).toBe('string') |
| 112 | + // Internal HTML format includes anchor tags linking to the dataset |
| 113 | + expect(citation).toMatch(/<a\s+href=/i) |
| 114 | + }) |
| 115 | + |
| 116 | + test('should throw an error when the file id does not exist', async () => { |
| 117 | + const nonExistentFileId = 5 |
| 118 | + |
| 119 | + await expect( |
| 120 | + getFileCitationByFormat.execute(nonExistentFileId, FileCitationFormat.BIBTEX) |
| 121 | + ).rejects.toThrow(ReadError) |
| 122 | + }) |
| 123 | +}) |
0 commit comments