Merge pull request #119 from SELab-2/test/service-test-correction

test: testen bug opgelost
This commit is contained in:
Joyelle Ndagijimana 2025-03-15 21:40:15 +01:00 committed by GitHub
commit 46e9e8d6a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 1962 additions and 2301 deletions

View file

@ -1,117 +0,0 @@
import { describe, it, expect, vi } from 'vitest';
import { LearningObjectMetadata, LearningPath } from '../../src/interfaces/learningPath';
import { fetchWithLogging } from '../../src/util/apiHelper';
import { getLearningObjectById, getLearningObjectsFromPath } from '../../src/services/learningObjects';
import { fetchLearningPaths } from '../../src/services/learningPaths';
// Mock API functions
vi.mock('../../src/util/apiHelper', () => ({
fetchWithLogging: vi.fn(),
}));
vi.mock('../../src/services/learningPaths', () => ({
fetchLearningPaths: vi.fn(),
}));
describe('getLearningObjectById', () => {
const hruid = 'test-object';
const language = 'en';
const mockMetadata: LearningObjectMetadata = {
hruid,
_id: '123',
uuid: 'uuid-123',
version: 1,
title: 'Test Object',
language,
difficulty: 5,
estimated_time: 120,
available: true,
teacher_exclusive: false,
educational_goals: [{ source: 'source', id: 'id' }],
keywords: ['robotics'],
description: 'A test object',
target_ages: [10, 12],
content_type: 'markdown',
content_location: '',
};
it('✅ Should return a filtered learning object when API provides data', async () => {
vi.mocked(fetchWithLogging).mockResolvedValueOnce(mockMetadata);
const result = await getLearningObjectById(hruid, language);
expect(result).toEqual({
key: hruid,
_id: '123',
uuid: 'uuid-123',
version: 1,
title: 'Test Object',
htmlUrl: expect.stringContaining('/learningObject/getRaw?hruid=test-object&language=en'),
language,
difficulty: 5,
estimatedTime: 120,
available: true,
teacherExclusive: false,
educationalGoals: [{ source: 'source', id: 'id' }],
keywords: ['robotics'],
description: 'A test object',
targetAges: [10, 12],
contentType: 'markdown',
contentLocation: '',
});
});
it('⚠️ Should return null if API returns no metadata', async () => {
vi.mocked(fetchWithLogging).mockResolvedValueOnce(null);
const result = await getLearningObjectById(hruid, language);
expect(result).toBeNull();
});
});
describe('getLearningObjectsFromPath', () => {
const hruid = 'test-path';
const language = 'en';
it('✅ Should not give error or warning', async () => {
const mockPathResponse: LearningPath[] = [
{
_id: 'path-1',
hruid,
language,
title: 'Test Path',
description: '',
num_nodes: 1,
num_nodes_left: 0,
nodes: [],
keywords: '',
target_ages: [],
min_age: 10,
max_age: 12,
__order: 1,
},
];
vi.mocked(fetchLearningPaths).mockResolvedValueOnce({
success: true,
source: 'Test Source',
data: mockPathResponse,
});
const result = await getLearningObjectsFromPath(hruid, language);
expect(result).toEqual([]);
});
it('⚠️ Should give a warning', async () => {
vi.mocked(fetchLearningPaths).mockResolvedValueOnce({ success: false, source: 'Test Source', data: [] });
const result = await getLearningObjectsFromPath(hruid, language);
expect(result).toEqual([]);
});
it('❌ Should give an error', async () => {
vi.mocked(fetchLearningPaths).mockRejectedValueOnce(new Error('API Error'));
const result = await getLearningObjectsFromPath(hruid, language);
expect(result).toEqual([]);
});
});

View file

@ -1,90 +0,0 @@
import { describe, it, expect, vi } from 'vitest';
import { fetchLearningPaths, searchLearningPaths } from '../../src/services/learningPaths';
import { fetchWithLogging } from '../../src/util/apiHelper';
import { LearningPathResponse } from '../../src/interfaces/learningPath';
// Mock the fetchWithLogging module using vi
vi.mock('../../src/util/apiHelper', () => ({
fetchWithLogging: vi.fn(),
}));
describe('fetchLearningPaths', () => {
// Mock data and response
const mockHruids = ['pn_werking', 'art1'];
const language = 'en';
const source = 'Test Source';
const mockResponse = [{ title: 'Test Path', hruids: mockHruids }];
it('✅ Should return a successful response when HRUIDs are provided', async () => {
// Mock the function to return mockResponse
vi.mocked(fetchWithLogging).mockResolvedValue(mockResponse);
const result: LearningPathResponse = await fetchLearningPaths(mockHruids, language, source);
expect(result.success).toBe(true);
expect(result.data).toEqual(mockResponse);
expect(result.source).toBe(source);
});
it('⚠️ Should return an error when no HRUIDs are provided', async () => {
vi.mocked(fetchWithLogging).mockResolvedValue(mockResponse);
const result: LearningPathResponse = await fetchLearningPaths([], language, source);
expect(result.success).toBe(false);
expect(result.data).toBeNull();
expect(result.message).toBe(`No HRUIDs provided for ${source}.`);
});
it('⚠️ Should return a failure response when no learning paths are found', async () => {
// Mock fetchWithLogging to return an empty array
vi.mocked(fetchWithLogging).mockResolvedValue([]);
const result: LearningPathResponse = await fetchLearningPaths(mockHruids, language, source);
expect(result.success).toBe(false);
expect(result.data).toEqual([]);
expect(result.message).toBe(`No learning paths found for ${source}.`);
});
});
describe('searchLearningPaths', () => {
const query =
'https://dwengo.org/backend/api/learningPath/getPathsFromIdList?pathIdList=%7B%22hruids%22:%5B%22pn_werking%22,%22un_artificiele_intelligentie%22%5D%7D&language=nl';
const language = 'nl';
it('✅ Should return search results when API responds with data', async () => {
const mockResults = [
{
_id: '67b4488c9dadb305c4104618',
language: 'nl',
hruid: 'pn_werking',
title: 'Werken met notebooks',
description: 'Een korte inleiding tot Python notebooks. Hoe ga je gemakkelijk en efficiënt met de notebooks aan de slag?',
num_nodes: 0,
num_nodes_left: 0,
nodes: [],
keywords: 'Python KIKS Wiskunde STEM AI',
target_ages: [14, 15, 16, 17, 18],
min_age: 14,
max_age: 18,
__order: 0,
},
];
// Mock fetchWithLogging to return search results
vi.mocked(fetchWithLogging).mockResolvedValue(mockResults);
const result = await searchLearningPaths(query, language);
expect(result).toEqual(mockResults);
});
it('⚠️ Should return an empty array when API returns no results', async () => {
vi.mocked(fetchWithLogging).mockResolvedValue([]);
const result = await searchLearningPaths(query, language);
expect(result).toEqual([]);
});
});

View file

@ -32,9 +32,7 @@
},
"/api/student/": {
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -55,9 +53,7 @@
}
},
"post": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"responses": {
"201": {
@ -89,9 +85,7 @@
}
},
"delete": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"responses": {
"200": {
@ -108,9 +102,7 @@
},
"/api/student/{username}": {
"delete": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -135,9 +127,7 @@
}
},
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -164,9 +154,7 @@
},
"/api/student/{id}/classes": {
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -197,9 +185,7 @@
},
"/api/student/{id}/submissions": {
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -220,9 +206,7 @@
},
"/api/student/{id}/assignments": {
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -250,9 +234,7 @@
},
"/api/student/{id}/groups": {
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -280,9 +262,7 @@
},
"/api/student/{id}/questions": {
"get": {
"tags": [
"Student"
],
"tags": ["Student"],
"description": "",
"parameters": [
{
@ -303,9 +283,7 @@
},
"/api/group/": {
"get": {
"tags": [
"Group"
],
"tags": ["Group"],
"description": "",
"parameters": [
{
@ -326,9 +304,7 @@
}
},
"post": {
"tags": [
"Group"
],
"tags": ["Group"],
"description": "",
"responses": {
"201": {
@ -345,9 +321,7 @@
},
"/api/group/{groupid}": {
"get": {
"tags": [
"Group"
],
"tags": ["Group"],
"description": "",
"parameters": [
{
@ -371,9 +345,7 @@
},
"/api/group/{id}/questions": {
"get": {
"tags": [
"Group"
],
"tags": ["Group"],
"description": "",
"parameters": [
{
@ -394,9 +366,7 @@
},
"/api/assignment/": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -414,9 +384,7 @@
}
},
"post": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"responses": {
"201": {
@ -456,9 +424,7 @@
},
"/api/assignment/{id}": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -485,9 +451,7 @@
},
"/api/assignment/{id}/submissions": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -511,9 +475,7 @@
},
"/api/assignment/{id}/questions": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -534,9 +496,7 @@
},
"/api/assignment/{assignmentid}/groups/": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -565,9 +525,7 @@
}
},
"post": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -594,9 +552,7 @@
},
"/api/assignment/{assignmentid}/groups/{groupid}": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -628,9 +584,7 @@
},
"/api/assignment/{assignmentid}/groups/{id}/questions": {
"get": {
"tags": [
"Assignment"
],
"tags": ["Assignment"],
"description": "",
"parameters": [
{
@ -659,9 +613,7 @@
},
"/api/submission/": {
"get": {
"tags": [
"Submission"
],
"tags": ["Submission"],
"description": "",
"responses": {
"200": {
@ -672,9 +624,7 @@
},
"/api/submission/{id}": {
"post": {
"tags": [
"Submission"
],
"tags": ["Submission"],
"description": "",
"parameters": [
{
@ -696,9 +646,7 @@
}
},
"get": {
"tags": [
"Submission"
],
"tags": ["Submission"],
"description": "",
"parameters": [
{
@ -737,9 +685,7 @@
}
},
"delete": {
"tags": [
"Submission"
],
"tags": ["Submission"],
"description": "",
"parameters": [
{
@ -777,9 +723,7 @@
},
"/api/class/": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -797,9 +741,7 @@
}
},
"post": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"responses": {
"201": {
@ -830,9 +772,7 @@
},
"/api/class/{id}": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -859,9 +799,7 @@
},
"/api/class/{id}/teacher-invitations": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -889,9 +827,7 @@
},
"/api/class/{id}/students": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -919,9 +855,7 @@
},
"/api/class/{classid}/assignments/": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -947,9 +881,7 @@
}
},
"post": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -999,9 +931,7 @@
},
"/api/class/{classid}/assignments/{id}": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1036,9 +966,7 @@
},
"/api/class/{classid}/assignments/{id}/submissions": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1070,9 +998,7 @@
},
"/api/class/{classid}/assignments/{id}/questions": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1101,9 +1027,7 @@
},
"/api/class/{classid}/assignments/{assignmentid}/groups/": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1140,9 +1064,7 @@
}
},
"post": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1177,9 +1099,7 @@
},
"/api/class/{classid}/assignments/{assignmentid}/groups/{groupid}": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1219,9 +1139,7 @@
},
"/api/class/{classid}/assignments/{assignmentid}/groups/{id}/questions": {
"get": {
"tags": [
"Class"
],
"tags": ["Class"],
"description": "",
"parameters": [
{
@ -1258,9 +1176,7 @@
},
"/api/question/": {
"get": {
"tags": [
"Question"
],
"tags": ["Question"],
"description": "",
"parameters": [
{
@ -1281,9 +1197,7 @@
}
},
"post": {
"tags": [
"Question"
],
"tags": ["Question"],
"description": "",
"responses": {
"200": {
@ -1317,9 +1231,7 @@
},
"/api/question/{seq}": {
"delete": {
"tags": [
"Question"
],
"tags": ["Question"],
"description": "",
"parameters": [
{
@ -1341,9 +1253,7 @@
}
},
"get": {
"tags": [
"Question"
],
"tags": ["Question"],
"description": "",
"parameters": [
{
@ -1367,9 +1277,7 @@
},
"/api/question/answers/{seq}": {
"get": {
"tags": [
"Question"
],
"tags": ["Question"],
"description": "",
"parameters": [
{
@ -1400,9 +1308,7 @@
},
"/api/auth/config": {
"get": {
"tags": [
"Auth"
],
"tags": ["Auth"],
"description": "",
"responses": {
"200": {
@ -1413,9 +1319,7 @@
},
"/api/auth/testAuthenticatedOnly": {
"get": {
"tags": [
"Auth"
],
"tags": ["Auth"],
"description": "",
"responses": {
"200": {
@ -1434,9 +1338,7 @@
},
"/api/auth/testStudentsOnly": {
"get": {
"tags": [
"Auth"
],
"tags": ["Auth"],
"description": "",
"responses": {
"200": {
@ -1452,9 +1354,7 @@
},
"/api/auth/testTeachersOnly": {
"get": {
"tags": [
"Auth"
],
"tags": ["Auth"],
"description": "",
"responses": {
"200": {
@ -1470,9 +1370,7 @@
},
"/api/theme/": {
"get": {
"tags": [
"Theme"
],
"tags": ["Theme"],
"description": "",
"parameters": [
{
@ -1492,9 +1390,7 @@
},
"/api/theme/{theme}": {
"get": {
"tags": [
"Theme"
],
"tags": ["Theme"],
"description": "",
"parameters": [
{
@ -1518,9 +1414,7 @@
},
"/api/learningPath/": {
"get": {
"tags": [
"Learning Path"
],
"tags": ["Learning Path"],
"description": "",
"parameters": [
{
@ -1589,9 +1483,7 @@
},
"/api/learningObject/": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1611,9 +1503,7 @@
},
"/api/learningObject/{hruid}": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1634,9 +1524,7 @@
},
"/api/learningObject/{hruid}/html": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1657,9 +1545,7 @@
},
"/api/learningObject/{hruid}/html/{attachmentName}": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1688,9 +1574,7 @@
},
"/api/learningObject/{hruid}/submissions/": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1711,9 +1595,7 @@
},
"/api/learningObject/{hruid}/submissions/{id}": {
"post": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1743,9 +1625,7 @@
}
},
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1792,9 +1672,7 @@
}
},
"delete": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1840,9 +1718,7 @@
},
"/api/learningObject/{hruid}/{version}/questions/": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1879,9 +1755,7 @@
}
},
"post": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1933,9 +1807,7 @@
},
"/api/learningObject/{hruid}/{version}/questions/{seq}": {
"delete": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -1973,9 +1845,7 @@
}
},
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{
@ -2015,9 +1885,7 @@
},
"/api/learningObject/{hruid}/{version}/questions/answers/{seq}": {
"get": {
"tags": [
"Learning Object"
],
"tags": ["Learning Object"],
"description": "",
"parameters": [
{