fix(backend): Foute entity-structuur van leerpaden verbeterd.

Ook testen geschreven voor LearningPathRepository en LearningObjectRepository.
This commit is contained in:
Gerald Schmittinger 2025-03-09 08:50:39 +01:00
parent 4d999c78ba
commit 1417907933
24 changed files with 474 additions and 64 deletions

View file

@ -0,0 +1,71 @@
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 {LearningObject} from "../../../src/entities/content/learning-object.entity";
import {expectToBeCorrectEntity} from "../../test-utils/expect-to-be-correct-entity";
describe("LearningObjectRepository", () => {
let learningObjectRepository: LearningObjectRepository;
let exampleLearningObject: LearningObject;
beforeAll(async () => {
await setupTestApp();
learningObjectRepository = getLearningObjectRepository();
});
it("should be able to add a learning object to it without an error", async () => {
exampleLearningObject = example.createLearningObject();
await learningObjectRepository.insert(exampleLearningObject);
});
it("should return the learning object when queried by id", async () => {
const result = await learningObjectRepository.findByIdentifier({
hruid: exampleLearningObject.hruid,
language: exampleLearningObject.language,
version: exampleLearningObject.version
});
expect(result).toBeInstanceOf(LearningObject);
console.log(result);
expectToBeCorrectEntity({
name: "actual",
entity: result!
}, {
name: "expected",
entity: exampleLearningObject
});
});
it("should return null when non-existing version is queried", async () => {
const result = await learningObjectRepository.findByIdentifier({
hruid: exampleLearningObject.hruid,
language: exampleLearningObject.language,
version: 100
});
expect(result).toBe(null);
});
let newerExample: LearningObject;
it("should allow a learning object with the same id except a different version to be added", async () => {
newerExample = example.createLearningObject();
newerExample.version = 10;
newerExample.title += " (nieuw)";
await learningObjectRepository.save(newerExample);
});
it("should return the newest version of the learning object when queried by only hruid and language", async () => {
const result = await learningObjectRepository.findLatestByHruidAndLanguage(newerExample.hruid, newerExample.language);
expect(result).toBeInstanceOf(LearningObject);
expect(result?.version).toBe(10);
expect(result?.title).toContain("(nieuw)");
});
it("should return null when queried by non-existing hruid or language", async () => {
const result = await learningObjectRepository.findLatestByHruidAndLanguage("something_that_does_not_exist", exampleLearningObject.language);
expect(result).toBe(null);
});
});

View file

@ -0,0 +1,78 @@
import {beforeAll, describe, expect, it} from "vitest";
import {setupTestApp} from "../../setup-tests";
import {getLearningPathRepository} from "../../../src/data/repositories";
import {LearningPathRepository} from "../../../src/data/content/learning-path-repository";
import example from "../../test-assets/learning-paths/pn-werking-example";
import {LearningPath} from "../../../src/entities/content/learning-path.entity";
import {expectToBeCorrectEntity} from "../../test-utils/expect-to-be-correct-entity";
import {Language} from "../../../src/entities/content/language";
function expectToHaveFoundPrecisely(expected: LearningPath, result: LearningPath[]): void {
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", () => {
let learningPathRepo: LearningPathRepository;
beforeAll(async () => {
await setupTestApp();
learningPathRepo = getLearningPathRepository();
});
let examplePath: LearningPath;
it("should be able to add a learning path without throwing an error", async () => {
examplePath = example.createLearningPath();
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);
expectToBeCorrectEntity({ entity: result! }, { entity: examplePath });
});
it("should return null to a query on a non-existing hruid or language", async () => {
const result = await learningPathRepo.findByHruidAndLanguage("not_existing_hruid", examplePath.language);
expect(result).toBe(null);
});
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
);
expectToHaveFoundPrecisely(examplePath, result);
});
it("should return the learning path when we search for a search term occurring in its description", async () => {
const result = await learningPathRepo.findByQueryStringAndLanguage(
examplePath.description.slice(8, 15),
examplePath.language
);
expectToHaveFoundPrecisely(examplePath, result);
});
it("should return null when we search for something not occurring in its title or description", async () => {
const result = await learningPathRepo.findByQueryStringAndLanguage(
"something not occurring in the path",
examplePath.language
);
expectToHaveFoundNothing(result);
});
it("should return null when we search for something occurring in its title, but another language", async () => {
const result = await learningPathRepo.findByQueryStringAndLanguage(
examplePath.description.slice(1, 3),
Language.Kalaallisut
);
expectToHaveFoundNothing(result);
});
});