fix: gefaalde testen voor class controller gefixt

This commit is contained in:
Adriaan Jacquet 2025-03-29 22:28:52 +01:00
parent 7a443c0686
commit 943dd04e97
4 changed files with 33 additions and 29 deletions

View file

@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { createClass, getAllClasses, getClass, getClassStudents, getClassStudentsIds, getClassTeacherInvitations } from '../services/class.js';
import { createClass, getAllClasses, getClass, getClassStudents, getClassTeacherInvitations } from '../services/class.js';
import { ClassDTO } from '../interfaces/class.js';
export async function getAllClassesHandler(req: Request, res: Response): Promise<void> {
@ -58,7 +58,12 @@ export async function getClassStudentsHandler(req: Request, res: Response): Prom
const classId = req.params.id;
const full = req.query.full === 'true';
const students = full ? await getClassStudents(classId) : await getClassStudentsIds(classId);
const students = await getClassStudents(classId, full);
if (!students) {
res.status(404).json({ error: 'Class not found' });
return;
}
res.json({
students: students,
@ -71,6 +76,11 @@ export async function getTeacherInvitationsHandler(req: Request, res: Response):
const invitations = await getClassTeacherInvitations(classId, full);
if (!invitations) {
res.status(404).json({ error: 'Class not found' });
return;
}
res.json({
invitations: invitations,
});

View file

@ -1,3 +1,4 @@
import { Class } from '../../entities/classes/class.entity.js';
import { Student } from '../../entities/users/student.entity.js';
import { User } from '../../entities/users/user.entity.js';
import { DwengoEntityRepository } from '../dwengo-entity-repository.js';
@ -9,6 +10,9 @@ export class StudentRepository extends DwengoEntityRepository<Student> {
public findByUsername(username: string): Promise<Student | null> {
return this.findOne({ username: username });
}
public findByClass(cls: Class): Promise<Student[]> {
return this.find({ classes: cls });
}
public deleteByUsername(username: string): Promise<void> {
return this.deleteWhere({ username: username });
}

View file

@ -4,6 +4,7 @@ import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js';
import { getLogger } from '../logging/initalize.js';
import { getStudent } from './students.js';
const logger = getLogger();
@ -60,32 +61,30 @@ export async function getClass(classId: string): Promise<ClassDTO | null> {
return mapToClassDTO(cls);
}
async function fetchClassStudents(classId: string): Promise<StudentDTO[]> {
export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[] | null> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
if (!cls) {
return [];
return null;
}
return cls.students.map(mapToStudentDTO);
const studentRepository = getStudentRepository();
const students = await studentRepository.findByClass(cls);
if (full) {
return cls.students.map(mapToStudentDTO);
}
return students.map((student) => student.username);
}
export async function getClassStudents(classId: string): Promise<StudentDTO[]> {
return await fetchClassStudents(classId);
}
export async function getClassStudentsIds(classId: string): Promise<string[]> {
const students: StudentDTO[] = await fetchClassStudents(classId);
return students.map((student) => student.username);
}
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> {
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[] | null> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
if (!cls) {
return [];
return null;
}
const teacherInvitationRepository = getTeacherInvitationRepository();

View file

@ -3,6 +3,7 @@ import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest';
import { createClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js';
import { Request, Response } from 'express';
import { getAllClasses } from '../../src/services/class.js';
import { checkReturnList, checkReturn404 } from './qol.js';
describe('Class controllers', () => {
let req: Partial<Request>;
@ -84,15 +85,7 @@ describe('Class controllers', () => {
await getClassStudentsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith({ students: [
'DireStraits',
'Nirvana',
'Noordkaap',
'PinkFloyd',
'SmashingPumpkins',
'TheDoors',
'Tool'
]});
checkReturnList(jsonMock, 'students');
});
it('should return 404 not found when calling getClassStudentsHandler on a non-existent class', async () => {
@ -102,8 +95,7 @@ describe('Class controllers', () => {
};
await getClassStudentsHandler(req as Request, res as Response);
// will fail until code is fixed
expect(statusMock).toHaveBeenCalledWith(404);
expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' });
});
@ -133,9 +125,8 @@ describe('Class controllers', () => {
await getTeacherInvitationsHandler(req as Request, res as Response);
// will fail until code is fixed
expect(statusMock).toHaveBeenCalledWith(404);
expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' });
expect(statusMock).toHaveBeenCalledWith(404);
});
it('should return a list of classes', async () => {