fix(backend): Testen LearningPathRepository gerepareerd na refactoring.

This commit is contained in:
Gerald Schmittinger 2025-04-16 00:55:43 +02:00
parent a3be3699f2
commit ee9afab6ca
2 changed files with 34 additions and 24 deletions

View file

@ -2,41 +2,34 @@ import { beforeAll, describe, expect, it } from 'vitest';
import { setupTestApp } from '../../setup-tests.js'; import { setupTestApp } from '../../setup-tests.js';
import { getLearningPathRepository } from '../../../src/data/repositories.js'; import { getLearningPathRepository } from '../../../src/data/repositories.js';
import { LearningPathRepository } from '../../../src/data/content/learning-path-repository.js'; import { LearningPathRepository } from '../../../src/data/content/learning-path-repository.js';
import example from '../../test-assets/learning-paths/pn-werking-example.js';
import { LearningPath } from '../../../src/entities/content/learning-path.entity.js'; import { LearningPath } from '../../../src/entities/content/learning-path.entity.js';
import { expectToBeCorrectEntity } from '../../test-utils/expectations.js'; import {
expectToBeCorrectEntity,
expectToHaveFoundNothing,
expectToHaveFoundPrecisely
} from '../../test-utils/expectations.js';
import { Language } from '@dwengo-1/common/util/language'; import { Language } from '@dwengo-1/common/util/language';
import {testLearningPath01} from "../../test_assets/content/learning-paths.testdata";
function expectToHaveFoundPrecisely(expected: LearningPath, result: LearningPath[]): void { import {mapToLearningPath} from "../../../src/services/learning-paths/learning-path-service";
expect(result).toHaveProperty('length');
expect(result.length).toBe(1);
expectToBeCorrectEntity({ entity: result[0] }, { entity: expected });
}
function expectToHaveFoundNothing(result: LearningPath[]): void {
expect(result).toHaveProperty('length');
expect(result.length).toBe(0);
}
describe('LearningPathRepository', () => { describe('LearningPathRepository', () => {
let learningPathRepo: LearningPathRepository; let learningPathRepo: LearningPathRepository;
let examplePath: LearningPath;
beforeAll(async () => { beforeAll(async () => {
await setupTestApp(); await setupTestApp();
learningPathRepo = getLearningPathRepository(); learningPathRepo = getLearningPathRepository();
examplePath = mapToLearningPath(testLearningPath01, []);
}); });
let examplePath: LearningPath; it('should return a learning path when it is queried by hruid and language', async () => {
const result = await learningPathRepo.findByHruidAndLanguage(
it('should be able to add a learning path without throwing an error', async () => { testLearningPath01.hruid,
examplePath = example.createLearningPath(); testLearningPath01.language as Language
await learningPathRepo.insert(examplePath); );
});
it('should return the added path when it is queried by hruid and language', async () => {
const result = await learningPathRepo.findByHruidAndLanguage(examplePath.hruid, examplePath.language);
expect(result).toBeInstanceOf(LearningPath); expect(result).toBeInstanceOf(LearningPath);
expectToBeCorrectEntity({ entity: result! }, { entity: examplePath }); expectToBeCorrectEntity(result!, examplePath);
}); });
it('should return null to a query on a non-existing hruid or language', async () => { it('should return null to a query on a non-existing hruid or language', async () => {
@ -45,7 +38,7 @@ describe('LearningPathRepository', () => {
}); });
it('should return the learning path when we search for a search term occurring in its title', async () => { it('should return the learning path when we search for a search term occurring in its title', async () => {
const result = await learningPathRepo.findByQueryStringAndLanguage(examplePath.title.slice(4, 9), examplePath.language); const result = await learningPathRepo.findByQueryStringAndLanguage(examplePath.title.slice(9, 13), examplePath.language);
expectToHaveFoundPrecisely(examplePath, result); expectToHaveFoundPrecisely(examplePath, result);
}); });

View file

@ -141,3 +141,20 @@ export function expectToBeCorrectLearningPath(
expect(new Set(node.transitions.map((it) => it.next.version))).toEqual(new Set(expectedNode.transitions.map((it) => it.next.version))); expect(new Set(node.transitions.map((it) => it.next.version))).toEqual(new Set(expectedNode.transitions.map((it) => it.next.version)));
} }
} }
/**
* Expect that the given result is a singleton list with exactly the given element.
*/
export function expectToHaveFoundPrecisely<T extends object>(expected: T, result: T[]): void {
expect(result).toHaveProperty('length');
expect(result.length).toBe(1);
expectToBeCorrectEntity(result[0], expected);
}
/**
* Expect that the given result is an empty list.
*/
export function expectToHaveFoundNothing<T>(result: T[]): void {
expect(result).toHaveProperty('length');
expect(result.length).toBe(0);
}