test(backend): Gewerkt aan testen voor DatabaseLearningPathProvider.

This commit is contained in:
Gerald Schmittinger 2025-03-09 23:23:38 +01:00
parent 91e3b5ad91
commit 1f9e9ed70a
2 changed files with 55 additions and 1 deletions

View file

@ -0,0 +1,40 @@
import {beforeAll, describe, expect, it} from "vitest";
import {LearningObject} from "../../../src/entities/content/learning-object.entity";
import {setupTestApp} from "../../setup-tests";
import {LearningPath} from "../../../src/entities/content/learning-path.entity";
import {getLearningObjectRepository, getLearningPathRepository} from "../../../src/data/repositories";
import learningObjectExample from "../../test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example";
import learningPathExample from "../../test-assets/learning-paths/pn-werking-example"
import databaseLearningPathProvider from "../../../src/services/learning-paths/database-learning-path-provider";
async function initExampleData(): Promise<{ learningObject: LearningObject, learningPath: LearningPath }> {
const learningObjectRepo = getLearningObjectRepository();
const learningPathRepo = getLearningPathRepository();
let learningObject = learningObjectExample.createLearningObject();
let learningPath = learningPathExample.createLearningPath();
await learningObjectRepo.save(learningObject);
await learningPathRepo.save(learningPath);
return { learningObject, learningPath };
}
describe("DatabaseLearningPathProvider", () => {
let example: {learningObject: LearningObject, learningPath: LearningPath};
beforeAll(async () => {
await setupTestApp();
example = await initExampleData();
});
describe("fetchLearningPaths", () => {
it("returns the learning path correctly", () => {
const result = await databaseLearningPathProvider.fetchLearningPaths(
[example.learningPath.hruid],
example.learningPath.language,
"the source"
);
expect(result.success).toBe(true);
expect(result.data?.length).toBe(1);
expect(result.data)
})
});
});

View file

@ -1,6 +1,7 @@
import {AssertionError} from "node:assert";
import {LearningObject} from "../../src/entities/content/learning-object.entity";
import {FilteredLearningObject} from "../../src/interfaces/learning-content";
import {FilteredLearningObject, LearningPath} from "../../src/interfaces/learning-content";
import {LearningPath as LearningPathEntity} from "../../src/entities/content/learning-path.entity"
import { expect } from "vitest";
// Ignored properties because they belang for example to the class, not to the entity itself.
@ -91,3 +92,16 @@ export function expectToBeCorrectFilteredLearningObject(filtered: FilteredLearni
expect(filtered.htmlUrl).toContain(`language=${original.language}`);
expect(filtered.htmlUrl).toContain(`version=${original.version}`);
}
/**
* Check that a learning path returned by a LearningPathRetriever, the LearningPathService or an API endpoint
* is a correct representation of the given learning path entity.
*
* @param learningPath The learning path returned by the retriever, service or endpoint
* @param expectedEntity The expected entity
*/
export function expectToBeCorrectLearningPath(learningPath: LearningPath, expectedEntity: LearningPathEntity) {
expect(learningPath.hruid).toEqual(expectedEntity.hruid);
expect(learningPath.language).toEqual(expectedEntity.language);
expect(learningPath.description).toEqual(expectedEntity.description);
}