From db222080bd6eec4f7db02f67fd36eb9d51dae6ed Mon Sep 17 00:00:00 2001 From: Caspar Neumann Date: Fri, 27 Oct 2023 10:03:39 +0200 Subject: [PATCH] fix library.repo.spec code coverage --- .../h5p-editor/repo/library.repo.spec.ts | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/h5p-editor/repo/library.repo.spec.ts b/apps/server/src/modules/h5p-editor/repo/library.repo.spec.ts index 4897b62daf6..78d219aa557 100644 --- a/apps/server/src/modules/h5p-editor/repo/library.repo.spec.ts +++ b/apps/server/src/modules/h5p-editor/repo/library.repo.spec.ts @@ -11,6 +11,7 @@ describe('LibraryRepo', () => { let module: TestingModule; let libraryRepo: LibraryRepo; let addonLibVersionOne: InstalledLibrary; + let addonLibVersionOneDuplicate: InstalledLibrary; let addonLibVersionTwo: InstalledLibrary; let em: EntityManager; @@ -45,14 +46,25 @@ describe('LibraryRepo', () => { majorVersion: 1, minorVersion: 2, }; + const addonLibMetadataVersionOneDuplicate: ILibraryMetadata = { + runnable: false, + title: 'Duplicate', + patchVersion: 3, + machineName: 'addonVersionOne', + majorVersion: 1, + minorVersion: 2, + }; addonLibVersionOne = new InstalledLibrary(addonLibMetadataVersionOne); addonLibVersionOne.addTo = { player: { machineNames: [testingLibVersionOne.machineName] } }; + addonLibVersionOneDuplicate = new InstalledLibrary(addonLibMetadataVersionOneDuplicate); + addonLibVersionOneDuplicate.addTo = { player: { machineNames: [testingLibVersionOne.machineName] } }; + const testingLibMetadataVersionTwo: ILibraryMetadata = { runnable: false, title: '', patchVersion: 4, - machineName: 'testing', + machineName: 'addonVersionTwo', majorVersion: 2, minorVersion: 3, }; @@ -100,6 +112,14 @@ describe('LibraryRepo', () => { }); }); + describe('findByName', () => { + it('should get libaries by name', async () => { + const result = await libraryRepo.findByName('addonVersionTwo'); + expect(result).toBeDefined(); + expect(result).toEqual([addonLibVersionTwo]); + }); + }); + describe('findOneByNameAndVersionOrFail', () => { it('should get library', async () => { const result = await libraryRepo.findOneByNameAndVersionOrFail('addonVersionOne', 1, 2); @@ -114,6 +134,16 @@ describe('LibraryRepo', () => { expect(error).toBeDefined(); } }); + it('should throw error', async () => { + try { + await libraryRepo.createLibrary(addonLibVersionOneDuplicate); + await libraryRepo.findOneByNameAndVersionOrFail('addonVersionOne', 1, 2); + fail('Expected Error'); + } catch (error) { + expect(error).toBeDefined(); + expect(error).toEqual(new Error('Multiple libraries with the same name and version found')); + } + }); }); describe('findNewestByNameAndVersion', () => { @@ -126,9 +156,23 @@ describe('LibraryRepo', () => { describe('findByNameAndExactVersion', () => { it('should get a library by name and exact version', async () => { - const result = await libraryRepo.findByNameAndExactVersion('addonVersionOne', 1, 2, 3); + const result = await libraryRepo.findByNameAndExactVersion('addonVersionTwo', 2, 3, 4); + expect(result).toBeDefined(); + expect(result).toEqual(addonLibVersionTwo); + }); + it('should throw error', async () => { + try { + await libraryRepo.findByNameAndExactVersion('addonVersionOne', 1, 2, 3); + fail('Expected Error'); + } catch (error) { + expect(error).toBeDefined(); + expect(error).toEqual(new Error('too many libraries with same name and version')); + } + }); + it('should return null', async () => { + const result = await libraryRepo.findByNameAndExactVersion('addonVersionTwo', 99, 3, 4); expect(result).toBeDefined(); - expect(result).toEqual(addonLibVersionOne); + expect(result).toEqual(null); }); }); });