From 392510db8290482358bb18a51c289ad36a6dbb9b Mon Sep 17 00:00:00 2001 From: Gerald Schmittinger Date: Mon, 10 Mar 2025 23:32:22 +0100 Subject: [PATCH] test(backend): Testen voor LearningPathService toegevoegd --- .../learning-paths/learning-path-service.ts | 2 +- .../content/attachment-repository.test.ts | 2 +- .../learning-object-repository.test.ts | 2 +- .../database-learning-object-provider.test.ts | 2 +- .../learning-object-service.test.ts | 4 +- .../database-learning-path-provider.test.ts | 2 +- .../learning-path-service.test.ts | 106 ++++++++++++++++++ .../Knop.png | Bin .../content.md | 0 .../dwengo.png | Bin .../pn-werkingnotebooks-example.ts | 2 +- .../rendering.html | 0 12 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 backend/tests/services/learning-path/learning-path-service.test.ts rename backend/tests/test-assets/learning-objects/{pn_werkingnotebooks => pn-werkingnotebooks}/Knop.png (100%) rename backend/tests/test-assets/learning-objects/{pn_werkingnotebooks => pn-werkingnotebooks}/content.md (100%) rename backend/tests/test-assets/learning-objects/{pn_werkingnotebooks => pn-werkingnotebooks}/dwengo.png (100%) rename backend/tests/test-assets/learning-objects/{pn_werkingnotebooks => pn-werkingnotebooks}/pn-werkingnotebooks-example.ts (98%) rename backend/tests/test-assets/learning-objects/{pn_werkingnotebooks => pn-werkingnotebooks}/rendering.html (100%) diff --git a/backend/src/services/learning-paths/learning-path-service.ts b/backend/src/services/learning-paths/learning-path-service.ts index 601790c9..a00b4633 100644 --- a/backend/src/services/learning-paths/learning-path-service.ts +++ b/backend/src/services/learning-paths/learning-path-service.ts @@ -24,7 +24,7 @@ const learningPathService = { const userContentLearningPaths = await databaseLearningPathProvider.fetchLearningPaths(userContentHruids, language, source); const nonUserContentLearningPaths - = await databaseLearningPathProvider.fetchLearningPaths(nonUserContentHruids, language, source); + = await dwengoApiLearningPathProvider.fetchLearningPaths(nonUserContentHruids, language, source); return { data: (userContentLearningPaths.data || []).concat(nonUserContentLearningPaths.data || []), diff --git a/backend/tests/data/content/attachment-repository.test.ts b/backend/tests/data/content/attachment-repository.test.ts index 02727460..3f573cdb 100644 --- a/backend/tests/data/content/attachment-repository.test.ts +++ b/backend/tests/data/content/attachment-repository.test.ts @@ -3,7 +3,7 @@ import {setupTestApp} from "../../setup-tests"; import {getAttachmentRepository, getLearningObjectRepository} from "../../../src/data/repositories"; import {AttachmentRepository} from "../../../src/data/content/attachment-repository"; import {LearningObjectRepository} from "../../../src/data/content/learning-object-repository"; -import example from "../../test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example"; +import example from "../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example"; import {LearningObject} from "../../../src/entities/content/learning-object.entity"; import {Attachment} from "../../../src/entities/content/attachment.entity"; import {LearningObjectIdentifier} from "../../../src/entities/content/learning-object-identifier"; diff --git a/backend/tests/data/content/learning-object-repository.test.ts b/backend/tests/data/content/learning-object-repository.test.ts index 87b8511e..bcca4c83 100644 --- a/backend/tests/data/content/learning-object-repository.test.ts +++ b/backend/tests/data/content/learning-object-repository.test.ts @@ -2,7 +2,7 @@ import {beforeAll, describe, it, expect} from "vitest"; import {LearningObjectRepository} from "../../../src/data/content/learning-object-repository"; import {setupTestApp} from "../../setup-tests"; import {getLearningObjectRepository} from "../../../src/data/repositories"; -import example from "../../test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example.js" +import example from "../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example.js" import {LearningObject} from "../../../src/entities/content/learning-object.entity"; import {expectToBeCorrectEntity} from "../../test-utils/expectations"; diff --git a/backend/tests/services/learning-objects/database-learning-object-provider.test.ts b/backend/tests/services/learning-objects/database-learning-object-provider.test.ts index c8783246..691b29ba 100644 --- a/backend/tests/services/learning-objects/database-learning-object-provider.test.ts +++ b/backend/tests/services/learning-objects/database-learning-object-provider.test.ts @@ -1,7 +1,7 @@ import {beforeAll, describe, expect, it} from "vitest"; import {setupTestApp} from "../../setup-tests"; import {getLearningObjectRepository} from "../../../src/data/repositories"; -import example from "../../test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example"; +import example from "../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example"; import {LearningObject} from "../../../src/entities/content/learning-object.entity"; import databaseLearningObjectProvider from "../../../src/services/learning-objects/database-learning-object-provider"; import { diff --git a/backend/tests/services/learning-objects/learning-object-service.test.ts b/backend/tests/services/learning-objects/learning-object-service.test.ts index 2c2efd1a..2fac3d3e 100644 --- a/backend/tests/services/learning-objects/learning-object-service.test.ts +++ b/backend/tests/services/learning-objects/learning-object-service.test.ts @@ -2,7 +2,7 @@ import {beforeAll, describe, expect, it} from "vitest"; import {setupTestApp} from "../../setup-tests"; import {LearningObject} from "../../../src/entities/content/learning-object.entity"; import {getLearningObjectRepository} from "../../../src/data/repositories"; -import learningObjectExample from "../../test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example"; +import learningObjectExample from "../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example"; import learningObjectService from "../../../src/services/learning-objects/learning-object-service"; import {LearningObjectIdentifier} from "../../../src/interfaces/learning-content"; import {Language} from "../../../src/entities/content/language"; @@ -11,7 +11,7 @@ import {EnvVars, getEnvVar} from "../../../src/util/envvars"; const TEST_LEARNING_OBJECT_TITLE = "Test title"; const EXPECTED_DWENGO_LEARNING_OBJECT_TITLE = "Werken met notebooks"; const DWENGO_TEST_LEARNING_OBJECT_ID: LearningObjectIdentifier = { - hruid: "pn_werkingnotebooks", + hruid: "pn-werkingnotebooks", language: Language.Dutch, version: 3 }; diff --git a/backend/tests/services/learning-path/database-learning-path-provider.test.ts b/backend/tests/services/learning-path/database-learning-path-provider.test.ts index 5d612f04..0d46c1e0 100644 --- a/backend/tests/services/learning-path/database-learning-path-provider.test.ts +++ b/backend/tests/services/learning-path/database-learning-path-provider.test.ts @@ -3,7 +3,7 @@ import {LearningObject} from "../../../src/entities/content/learning-object.enti 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 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"; import {expectToBeCorrectLearningPath} from "../../test-utils/expectations"; diff --git a/backend/tests/services/learning-path/learning-path-service.test.ts b/backend/tests/services/learning-path/learning-path-service.test.ts new file mode 100644 index 00000000..83db1bef --- /dev/null +++ b/backend/tests/services/learning-path/learning-path-service.test.ts @@ -0,0 +1,106 @@ +import {beforeAll, describe, expect, it} from "vitest"; +import {setupTestApp} from "../../setup-tests"; +import {LearningObject} from "../../../src/entities/content/learning-object.entity"; +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 {Language} from "../../../src/entities/content/language"; +import learningPathService from "../../../src/services/learning-paths/learning-path-service"; + +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 }; +} + +const TEST_DWENGO_LEARNING_PATH_HRUID = "pn_werking"; +const TEST_DWENGO_LEARNING_PATH_TITLE = "Werken met notebooks"; +const TEST_DWENGO_EXCLUSIVE_LEARNING_PATH_SEARCH_QUERY = "Microscopie"; +const TEST_SEARCH_QUERY_EXPECTING_NO_MATCHES = "su$m8f9usf89ud { + let example: { learningObject: LearningObject, learningPath: LearningPath }; + beforeAll(async () => { + await setupTestApp(); + example = await initExampleData(); + }); + describe("fetchLearningPaths", () => { + it("should return learning paths both from the database and from the Dwengo API", async () => { + const result = await learningPathService.fetchLearningPaths( + [example.learningPath.hruid, TEST_DWENGO_LEARNING_PATH_HRUID], + example.learningPath.language, + "the source" + ); + expect(result.success).toBeTruthy(); + expect(result.data?.filter(it => it.hruid == TEST_DWENGO_LEARNING_PATH_HRUID).length).not.toBe(0); + expect(result.data?.filter(it => it.hruid == example.learningPath.hruid).length).not.toBe(0); + expect(result.data?.filter(it => it.hruid == TEST_DWENGO_LEARNING_PATH_HRUID)[0].title) + .toEqual(TEST_DWENGO_LEARNING_PATH_TITLE); + expect(result.data?.filter(it => it.hruid == example.learningPath.hruid)[0].title) + .toEqual(example.learningPath.title); + }); + it("should include both the learning objects from the Dwengo API and learning objects from the database in its response", async () => { + const result = await learningPathService.fetchLearningPaths( + [example.learningPath.hruid], + example.learningPath.language, + "the source" + ); + expect(result.success).toBeTruthy(); + expect(result.data?.length).toBe(1); + + // Should include all the nodes, even those pointing to foreign learning objects. + expect( + [...result.data![0].nodes.map(it => it.learningobject_hruid)].sort() + ).toEqual( + example.learningPath.nodes.map(it => it.learningObjectHruid).sort() + ); + }); + }); + describe("searchLearningPath", () => { + it("should include both the learning paths from the Dwengo API and those from the database in its response", async () => { + // This matches the learning object in the database, but definitely also some learning objects in the Dwengo API. + const result = await learningPathService.searchLearningPaths( + example.learningPath.title.substring(2, 3), + example.learningPath.language + ); + + // Should find the one from the database + expect( + result.filter(it => + it.hruid === example.learningPath.hruid && it.title === example.learningPath.title + ).length + ).toBe(1); + + // but should not only find that one. + expect(result.length).not.toBeLessThan(2); + }); + it("should still return results from the Dwengo API even though there are no matches in the database", async () => { + const result = await learningPathService.searchLearningPaths( + TEST_DWENGO_EXCLUSIVE_LEARNING_PATH_SEARCH_QUERY, + Language.Dutch + ); + + // Should find something... + expect(result.length).not.toBe(0); + + // but not the example learning path. + expect( + result.filter(it => + it.hruid === example.learningPath.hruid && it.title === example.learningPath.title + ).length + ).toBe(0); + }); + it("should return an empty list if neither the Dwengo API nor the database contains matches", async () => { + const result = await learningPathService.searchLearningPaths( + TEST_SEARCH_QUERY_EXPECTING_NO_MATCHES, + Language.Dutch + ); + expect(result.length).toBe(0); + }); + }); +}); diff --git a/backend/tests/test-assets/learning-objects/pn_werkingnotebooks/Knop.png b/backend/tests/test-assets/learning-objects/pn-werkingnotebooks/Knop.png similarity index 100% rename from backend/tests/test-assets/learning-objects/pn_werkingnotebooks/Knop.png rename to backend/tests/test-assets/learning-objects/pn-werkingnotebooks/Knop.png diff --git a/backend/tests/test-assets/learning-objects/pn_werkingnotebooks/content.md b/backend/tests/test-assets/learning-objects/pn-werkingnotebooks/content.md similarity index 100% rename from backend/tests/test-assets/learning-objects/pn_werkingnotebooks/content.md rename to backend/tests/test-assets/learning-objects/pn-werkingnotebooks/content.md diff --git a/backend/tests/test-assets/learning-objects/pn_werkingnotebooks/dwengo.png b/backend/tests/test-assets/learning-objects/pn-werkingnotebooks/dwengo.png similarity index 100% rename from backend/tests/test-assets/learning-objects/pn_werkingnotebooks/dwengo.png rename to backend/tests/test-assets/learning-objects/pn-werkingnotebooks/dwengo.png diff --git a/backend/tests/test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example.ts b/backend/tests/test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example.ts similarity index 98% rename from backend/tests/test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example.ts rename to backend/tests/test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example.ts index 4dff2c34..09cab0f9 100644 --- a/backend/tests/test-assets/learning-objects/pn_werkingnotebooks/pn-werkingnotebooks-example.ts +++ b/backend/tests/test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example.ts @@ -6,7 +6,7 @@ import {EducationalGoal, LearningObject, ReturnValue} from "../../../../src/enti import {Attachment} from "../../../../src/entities/content/attachment.entity"; import {EnvVars, getEnvVar} from "../../../../src/util/envvars"; -const ASSETS_PREFIX = "learning-objects/pn_werkingnotebooks/"; +const ASSETS_PREFIX = "learning-objects/pn-werkingnotebooks/"; const example: LearningObjectExample = { createLearningObject: ()=> { diff --git a/backend/tests/test-assets/learning-objects/pn_werkingnotebooks/rendering.html b/backend/tests/test-assets/learning-objects/pn-werkingnotebooks/rendering.html similarity index 100% rename from backend/tests/test-assets/learning-objects/pn_werkingnotebooks/rendering.html rename to backend/tests/test-assets/learning-objects/pn-werkingnotebooks/rendering.html