From 1f9e9ed70afb4b94128cbb9b3e53093da3c4ec50 Mon Sep 17 00:00:00 2001 From: Gerald Schmittinger Date: Sun, 9 Mar 2025 23:23:38 +0100 Subject: [PATCH] test(backend): Gewerkt aan testen voor DatabaseLearningPathProvider. --- .../database-learning-path-provider.test.ts | 40 +++++++++++++++++++ backend/tests/test-utils/expectations.ts | 16 +++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 backend/tests/services/learning-objects/database-learning-path-provider.test.ts diff --git a/backend/tests/services/learning-objects/database-learning-path-provider.test.ts b/backend/tests/services/learning-objects/database-learning-path-provider.test.ts new file mode 100644 index 00000000..dab40a69 --- /dev/null +++ b/backend/tests/services/learning-objects/database-learning-path-provider.test.ts @@ -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) + }) + }); +}); diff --git a/backend/tests/test-utils/expectations.ts b/backend/tests/test-utils/expectations.ts index 048a327e..285c001a 100644 --- a/backend/tests/test-utils/expectations.ts +++ b/backend/tests/test-utils/expectations.ts @@ -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); +}