fix: gefaalde testen voor class controller gefixt
This commit is contained in:
parent
7a443c0686
commit
943dd04e97
4 changed files with 33 additions and 29 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { Request, Response } from 'express';
|
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';
|
import { ClassDTO } from '../interfaces/class.js';
|
||||||
|
|
||||||
export async function getAllClassesHandler(req: Request, res: Response): Promise<void> {
|
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 classId = req.params.id;
|
||||||
const full = req.query.full === 'true';
|
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({
|
res.json({
|
||||||
students: students,
|
students: students,
|
||||||
|
@ -71,6 +76,11 @@ export async function getTeacherInvitationsHandler(req: Request, res: Response):
|
||||||
|
|
||||||
const invitations = await getClassTeacherInvitations(classId, full);
|
const invitations = await getClassTeacherInvitations(classId, full);
|
||||||
|
|
||||||
|
if (!invitations) {
|
||||||
|
res.status(404).json({ error: 'Class not found' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
invitations: invitations,
|
invitations: invitations,
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { Class } from '../../entities/classes/class.entity.js';
|
||||||
import { Student } from '../../entities/users/student.entity.js';
|
import { Student } from '../../entities/users/student.entity.js';
|
||||||
import { User } from '../../entities/users/user.entity.js';
|
import { User } from '../../entities/users/user.entity.js';
|
||||||
import { DwengoEntityRepository } from '../dwengo-entity-repository.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> {
|
public findByUsername(username: string): Promise<Student | null> {
|
||||||
return this.findOne({ username: username });
|
return this.findOne({ username: username });
|
||||||
}
|
}
|
||||||
|
public findByClass(cls: Class): Promise<Student[]> {
|
||||||
|
return this.find({ classes: cls });
|
||||||
|
}
|
||||||
public deleteByUsername(username: string): Promise<void> {
|
public deleteByUsername(username: string): Promise<void> {
|
||||||
return this.deleteWhere({ username: username });
|
return this.deleteWhere({ username: username });
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
|
||||||
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
|
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
|
||||||
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js';
|
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js';
|
||||||
import { getLogger } from '../logging/initalize.js';
|
import { getLogger } from '../logging/initalize.js';
|
||||||
|
import { getStudent } from './students.js';
|
||||||
|
|
||||||
const logger = getLogger();
|
const logger = getLogger();
|
||||||
|
|
||||||
|
@ -60,32 +61,30 @@ export async function getClass(classId: string): Promise<ClassDTO | null> {
|
||||||
return mapToClassDTO(cls);
|
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 classRepository = getClassRepository();
|
||||||
const cls = await classRepository.findById(classId);
|
const cls = await classRepository.findById(classId);
|
||||||
|
|
||||||
if (!cls) {
|
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[]> {
|
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[] | null> {
|
||||||
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[]> {
|
|
||||||
const classRepository = getClassRepository();
|
const classRepository = getClassRepository();
|
||||||
const cls = await classRepository.findById(classId);
|
const cls = await classRepository.findById(classId);
|
||||||
|
|
||||||
if (!cls) {
|
if (!cls) {
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||||
|
|
|
@ -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 { createClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js';
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { getAllClasses } from '../../src/services/class.js';
|
import { getAllClasses } from '../../src/services/class.js';
|
||||||
|
import { checkReturnList, checkReturn404 } from './qol.js';
|
||||||
|
|
||||||
describe('Class controllers', () => {
|
describe('Class controllers', () => {
|
||||||
let req: Partial<Request>;
|
let req: Partial<Request>;
|
||||||
|
@ -84,15 +85,7 @@ describe('Class controllers', () => {
|
||||||
|
|
||||||
await getClassStudentsHandler(req as Request, res as Response);
|
await getClassStudentsHandler(req as Request, res as Response);
|
||||||
|
|
||||||
expect(jsonMock).toHaveBeenCalledWith({ students: [
|
checkReturnList(jsonMock, 'students');
|
||||||
'DireStraits',
|
|
||||||
'Nirvana',
|
|
||||||
'Noordkaap',
|
|
||||||
'PinkFloyd',
|
|
||||||
'SmashingPumpkins',
|
|
||||||
'TheDoors',
|
|
||||||
'Tool'
|
|
||||||
]});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 404 not found when calling getClassStudentsHandler on a non-existent class', async () => {
|
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);
|
await getClassStudentsHandler(req as Request, res as Response);
|
||||||
|
|
||||||
// will fail until code is fixed
|
|
||||||
expect(statusMock).toHaveBeenCalledWith(404);
|
expect(statusMock).toHaveBeenCalledWith(404);
|
||||||
expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' });
|
expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' });
|
||||||
});
|
});
|
||||||
|
@ -133,9 +125,8 @@ describe('Class controllers', () => {
|
||||||
|
|
||||||
await getTeacherInvitationsHandler(req as Request, res as Response);
|
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(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' });
|
||||||
|
expect(statusMock).toHaveBeenCalledWith(404);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a list of classes', async () => {
|
it('should return a list of classes', async () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue