fix(backend): Testen DatabaseLearningPathProvider en LearningPathService gerepareerd na refactoring.
This commit is contained in:
parent
1815371a7b
commit
c624e36680
6 changed files with 163 additions and 204 deletions
|
@ -1,22 +1,11 @@
|
|||
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 learningPathService from '../../../src/services/learning-paths/learning-path-service';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
|
||||
async function initExampleData(): Promise<{ learningObject: LearningObject; learningPath: LearningPath }> {
|
||||
const learningObjectRepo = getLearningObjectRepository();
|
||||
const learningPathRepo = getLearningPathRepository();
|
||||
const learningObject = learningObjectExample.createLearningObject();
|
||||
const learningPath = learningPathExample.createLearningPath();
|
||||
await learningObjectRepo.save(learningObject);
|
||||
await learningPathRepo.save(learningPath);
|
||||
return { learningObject, learningPath };
|
||||
}
|
||||
import {
|
||||
testPartiallyDatabaseAndPartiallyDwengoApiLearningPath
|
||||
} from "../../test_assets/content/learning-paths.testdata";
|
||||
import {LearningPath as LearningPathDTO} from "@dwengo-1/common/interfaces/learning-content";
|
||||
|
||||
const TEST_DWENGO_LEARNING_PATH_HRUID = 'pn_werking';
|
||||
const TEST_DWENGO_LEARNING_PATH_TITLE = 'Werken met notebooks';
|
||||
|
@ -24,42 +13,49 @@ const TEST_DWENGO_EXCLUSIVE_LEARNING_PATH_SEARCH_QUERY = 'Microscopie';
|
|||
const TEST_SEARCH_QUERY_EXPECTING_NO_MATCHES = 'su$m8f9usf89ud<p9<U8SDP8UP9';
|
||||
|
||||
describe('LearningPathService', () => {
|
||||
let example: { learningObject: LearningObject; learningPath: LearningPath };
|
||||
let testLearningPath: LearningPathDTO;
|
||||
beforeAll(async () => {
|
||||
await setupTestApp();
|
||||
example = await initExampleData();
|
||||
testLearningPath = testPartiallyDatabaseAndPartiallyDwengoApiLearningPath
|
||||
});
|
||||
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,
|
||||
[testLearningPath.hruid, TEST_DWENGO_LEARNING_PATH_HRUID],
|
||||
testLearningPath.language as 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 === testLearningPath.hruid).length).not.toBe(0);
|
||||
expect(result.data?.find((it) => it.hruid === TEST_DWENGO_LEARNING_PATH_HRUID)?.title).toEqual(TEST_DWENGO_LEARNING_PATH_TITLE);
|
||||
expect(result.data?.find((it) => it.hruid === example.learningPath.hruid)?.title).toEqual(example.learningPath.title);
|
||||
expect(result.data?.find((it) => it.hruid === testLearningPath.hruid)?.title).toEqual(testLearningPath.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');
|
||||
const result = await learningPathService.fetchLearningPaths(
|
||||
[testLearningPath.hruid],
|
||||
testLearningPath.language as 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((a, b) => a.localeCompare(b))).toEqual(
|
||||
example.learningPath.nodes.map((it) => it.learningObjectHruid).sort((a, b) => a.localeCompare(b))
|
||||
testLearningPath.nodes.map((it) => it.learningobject_hruid).sort((a, b) => a.localeCompare(b))
|
||||
);
|
||||
});
|
||||
});
|
||||
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);
|
||||
const result = await learningPathService.searchLearningPaths(
|
||||
testLearningPath.title.substring(2, 3),
|
||||
testLearningPath.language as 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);
|
||||
expect(result.filter((it) => it.hruid === testLearningPath.hruid && it.title === testLearningPath.title).length).toBe(1);
|
||||
|
||||
// But should not only find that one.
|
||||
expect(result.length).not.toBeLessThan(2);
|
||||
|
@ -71,7 +67,7 @@ describe('LearningPathService', () => {
|
|||
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);
|
||||
expect(result.filter((it) => it.hruid === testLearningPath.hruid && it.title === testLearningPath.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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue