Merge branch 'fix/testdata-niet-meer-correct-opgezet' of https://github.com/SELab-2/Dwengo-1 into fix/testdata-niet-meer-correct-opgezet

This commit is contained in:
laurejablonski 2025-05-13 09:58:08 +02:00
commit 6670e086fe
34 changed files with 622 additions and 6033 deletions

View file

@ -0,0 +1,76 @@
import { setupTestApp } from '../setup-tests.js';
import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest';
import { Request, Response } from 'express';
import { getAssignmentHandler, getAllAssignmentsHandler, getAssignmentsSubmissionsHandler } from '../../src/controllers/assignments.js';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { getClass01 } from '../test_assets/classes/classes.testdata';
import { getAssignment01 } from '../test_assets/assignments/assignments.testdata';
function createRequestObject(
classid: string,
assignmentid: string
): {
query: { full: string };
params: { classid: string; id: string };
} {
return {
params: {
classid: classid,
id: assignmentid,
},
query: {
full: 'true',
},
};
}
describe('Assignment controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
let statusMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(async () => {
jsonMock = vi.fn();
statusMock = vi.fn().mockReturnThis();
res = {
json: jsonMock,
status: statusMock,
};
});
it('return error non-existing assignment', async () => {
req = createRequestObject('doesnotexist', '43000'); // Should not exist
await expect(async () => getAssignmentHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return an assignment', async () => {
const assignment = getAssignment01();
req = createRequestObject(assignment.within.classId as string, (assignment.id ?? 1).toString());
await getAssignmentHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ assignment: expect.anything() }));
});
it('should return a list of assignments', async () => {
req = createRequestObject(getClass01().classId as string, 'irrelevant');
await getAllAssignmentsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ assignments: expect.anything() }));
});
it('should return a list of submissions for an assignment', async () => {
const assignment = getAssignment01();
req = createRequestObject(assignment.within.classId as string, (assignment.id ?? 1).toString());
await getAssignmentsSubmissionsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() }));
});
});

View file

@ -1,8 +1,17 @@
import { setupTestApp } from '../setup-tests.js';
import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest';
import {
createClassHandler,
deleteClassHandler,
getAllClassesHandler,
getClassHandler,
getClassStudentsHandler,
getTeacherInvitationsHandler,
} from '../../src/controllers/classes.js';
import { Request, Response } from 'express';
import { createClassHandler, deleteClassHandler } from '../../src/controllers/classes';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { getClass01 } from '../test_assets/classes/classes.testdata';
describe('Class controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
@ -44,4 +53,71 @@ describe('Class controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() }));
});
it('Error class not found', async () => {
req = {
params: { id: 'doesnotexist' },
};
await expect(async () => getClassHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('Error create a class without name', async () => {
req = {
body: {},
};
await expect(async () => createClassHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException);
});
it('return list of students', async () => {
req = {
params: { id: getClass01().classId as string },
query: {},
};
await getClassStudentsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ students: expect.anything() }));
});
it('Error students on a non-existent class', async () => {
req = {
params: { id: 'doesnotexist' },
query: {},
};
await expect(async () => getClassStudentsHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return 200 and a list of teacher-invitations', async () => {
const classId = getClass01().classId as string;
req = {
params: { id: classId },
query: {},
};
await getTeacherInvitationsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() }));
});
it('Error teacher-invitations on a non-existent class', async () => {
req = {
params: { id: 'doesnotexist' },
query: {},
};
await expect(async () => getTeacherInvitationsHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return a list of classes', async () => {
req = {
query: {},
};
await getAllClassesHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ classes: expect.anything() }));
});
});

View file

@ -0,0 +1,140 @@
import { setupTestApp } from '../setup-tests.js';
import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest';
import { Request, Response } from 'express';
import {
createGroupHandler,
deleteGroupHandler,
getAllGroupsHandler,
getGroupHandler,
getGroupSubmissionsHandler,
} from '../../src/controllers/groups.js';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { getClass01 } from '../test_assets/classes/classes.testdata';
import { getAssignment01, getAssignment02 } from '../test_assets/assignments/assignments.testdata';
import { getTestGroup01 } from '../test_assets/assignments/groups.testdata';
function createRequestObject(
classid: string,
assignmentid: string,
groupNumber: string
): {
query: { full: string };
params: { classid: string; groupid: string; assignmentid: string };
} {
return {
params: {
classid: classid,
assignmentid: assignmentid,
groupid: groupNumber,
},
query: {
full: 'true',
},
};
}
describe('Group controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
let statusMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(async () => {
jsonMock = vi.fn();
statusMock = vi.fn().mockReturnThis();
res = {
json: jsonMock,
status: statusMock,
};
});
it('Error not found on a non-existing group', async () => {
req = {
params: {
classid: 'id01',
assignmentid: '1',
groupid: '154981', // Should not exist
},
query: {},
};
await expect(async () => getGroupHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return 404 not found on a non-existing assignment', async () => {
req = {
params: {
classid: 'id01',
assignmentid: '1000', // Should not exist
groupid: '42000', // Should not exist
},
query: {},
};
await expect(async () => getGroupHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return 404 not found ont a non-existing class', async () => {
req = {
params: {
classid: 'doesnotexist', // Should not exist
assignmentid: '1000', // Should not exist
groupid: '42000', // Should not exist
},
query: {},
};
await expect(async () => getGroupHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return an existing group', async () => {
const group = getTestGroup01();
const classId = getClass01().classId as string;
req = createRequestObject(classId, (group.assignment.id ?? 1).toString(), (group.groupNumber ?? 1).toString());
await getGroupHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ group: expect.anything() }));
});
it('Create and delete', async () => {
const assignment = getAssignment02();
const classId = assignment.within.classId as string;
req = createRequestObject(classId, (assignment.id ?? 1).toString(), '1');
req.body = {
members: ['Noordkaap', 'DireStraits'],
};
await createGroupHandler(req as Request, res as Response);
await deleteGroupHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ group: expect.anything() }));
});
it('should return the submissions for a group', async () => {
const group = getTestGroup01();
const classId = getClass01().classId as string;
req = createRequestObject(classId, (group.assignment.id ?? 1).toString(), (group.groupNumber ?? 1).toString());
await getGroupSubmissionsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() }));
});
it('should return a list of groups for an assignment', async () => {
const assignment = getAssignment01();
const classId = assignment.within.classId as string;
req = createRequestObject(classId, (assignment.id ?? 1).toString(), '1');
await getAllGroupsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ groups: expect.anything() }));
});
});

View file

@ -0,0 +1,61 @@
import { setupTestApp } from '../setup-tests.js';
import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest';
import { getSubmissionHandler, getAllSubmissionsHandler } from '../../src/controllers/submissions.js';
import { Request, Response } from 'express';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { getClass02 } from '../test_assets/classes/classes.testdata';
function createRequestObject(
hruid: string,
submissionNumber: string
): {
query: { language: string; version: string };
params: { hruid: string; id: string };
} {
return {
params: {
hruid: hruid,
id: submissionNumber,
},
query: {
language: 'en',
version: '1',
},
};
}
describe('Submission controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
let statusMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(async () => {
jsonMock = vi.fn();
statusMock = vi.fn().mockReturnThis();
res = {
json: jsonMock,
status: statusMock,
};
});
it('error submission is not found', async () => {
req = createRequestObject('id01', '1000000');
await expect(async () => getSubmissionHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('should return a list of submissions for a learning object', async () => {
req = createRequestObject(getClass02().classId as string, 'irrelevant');
await getAllSubmissionsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() }));
});
});

View file

@ -21,7 +21,11 @@ describe('SubmissionRepository', () => {
it('should find the requested submission', async () => {
const usedSubmission = getSubmission01();
const id = new LearningObjectIdentifier(usedSubmission.learningObjectHruid, usedSubmission.learningObjectLanguage, usedSubmission.learningObjectVersion);
const id = new LearningObjectIdentifier(
usedSubmission.learningObjectHruid,
usedSubmission.learningObjectLanguage,
usedSubmission.learningObjectVersion
);
const submission = await submissionRepository.findSubmissionByLearningObjectAndSubmissionNumber(id, usedSubmission.submissionNumber!);
expect(submission).toBeTruthy();
@ -42,8 +46,12 @@ describe('SubmissionRepository', () => {
it('should find the most recent submission for a group', async () => {
const usedSubmission = getSubmission02();
const id = new LearningObjectIdentifier(usedSubmission.learningObjectHruid, usedSubmission.learningObjectLanguage, usedSubmission.learningObjectVersion);
const id = new LearningObjectIdentifier(
usedSubmission.learningObjectHruid,
usedSubmission.learningObjectLanguage,
usedSubmission.learningObjectVersion
);
const submission = await submissionRepository.findMostRecentSubmissionForGroup(id, usedSubmission.onBehalfOf);
expect(submission).toBeTruthy();
@ -53,13 +61,14 @@ describe('SubmissionRepository', () => {
it('should find all submissions for a certain learning object and assignment', async () => {
const usedSubmission = getSubmission08();
const assignment = getAssignment01();
const loId = {
hruid: usedSubmission.learningObjectHruid,
language: usedSubmission.learningObjectLanguage,
version: usedSubmission.learningObjectVersion,
};
const result = await submissionRepository.findAllSubmissionsForLearningObjectAndAssignment(loId, assignment);
const result = await submissionRepository.findAllSubmissionsForLearningObjectAndAssignment(loId, assignment);
sortSubmissions(result);
expect(result).toHaveLength(3);
@ -86,6 +95,7 @@ describe('SubmissionRepository', () => {
version: usedSubmission.learningObjectVersion,
};
const result = await submissionRepository.findAllSubmissionsForLearningObjectAndGroup(loId, group);
const result = await submissionRepository.findAllSubmissionsForLearningObjectAndGroup(loId, group);
expect(result).toHaveLength(1);

View file

@ -48,7 +48,6 @@ describe('ClassRepository', () => {
expect(invitations).toHaveLength(2);
expect(invitations[0].class.classId).toBeOneOf([ti1.class.classId, ti2.class.classId]);
expect(invitations[1].class.classId).toBeOneOf([ti1.class.classId, ti2.class.classId]);
});
it('should not find a removed invitation', async () => {

View file

@ -47,6 +47,13 @@ describe('LearningObjectRepository', () => {
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', testLearningObject01.language);
expect(result).toBe(null);

View file

@ -5,6 +5,12 @@ import { testLearningPath01, testLearningPath02, testLearningPathWithConditions
import { getClass01, getClass02, getClassWithTestleerlingAndTestleerkracht } from '../classes/classes.testdata';
export function makeTestAssignemnts(em: EntityManager): Assignment[] {
const futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
const pastDate = new Date();
pastDate.setDate(pastDate.getDate() - 7);
const today = new Date();
today.setHours(23, 59);
assignment01 = em.create(Assignment, {
id: 21000,
within: getClass01(),
@ -12,6 +18,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] {
description: 'reading',
learningPathHruid: testLearningPath02.hruid,
learningPathLanguage: testLearningPath02.language as Language,
deadline: today,
groups: [],
});
@ -22,6 +29,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] {
description: 'reading',
learningPathHruid: testLearningPath01.hruid,
learningPathLanguage: testLearningPath01.language as Language,
deadline: futureDate,
groups: [],
});
@ -32,6 +40,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] {
description: 'will be deleted',
learningPathHruid: testLearningPath02.hruid,
learningPathLanguage: testLearningPath02.language as Language,
deadline: pastDate,
groups: [],
});
@ -42,6 +51,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] {
description: 'with a description',
learningPathHruid: testLearningPath01.hruid,
learningPathLanguage: testLearningPath01.language as Language,
deadline: pastDate,
groups: [],
});
@ -52,6 +62,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] {
description: 'You have to do the testing learning path with a condition.',
learningPathHruid: testLearningPathWithConditions.hruid,
learningPathLanguage: testLearningPathWithConditions.language as Language,
deadline: futureDate,
groups: [],
});

View file

@ -106,34 +106,34 @@ let submission06: Submission;
let submission07: Submission;
let submission08: Submission;
export function getSubmission01(): Submission{
export function getSubmission01(): Submission {
return submission01;
}
export function getSubmission02(): Submission{
export function getSubmission02(): Submission {
return submission02;
}
export function getSubmission03(): Submission{
export function getSubmission03(): Submission {
return submission03;
}
export function getSubmission04(): Submission{
export function getSubmission04(): Submission {
return submission04;
}
export function getSubmission05(): Submission{
export function getSubmission05(): Submission {
return submission05;
}
export function getSubmission06(): Submission{
export function getSubmission06(): Submission {
return submission06;
}
export function getSubmission07(): Submission{
export function getSubmission07(): Submission {
return submission07;
}
export function getSubmission08(): Submission{
export function getSubmission08(): Submission {
return submission08;
}
}

View file

@ -37,18 +37,18 @@ let classJoinRequest02: ClassJoinRequest;
let classJoinRequest03: ClassJoinRequest;
let classJoinRequest04: ClassJoinRequest;
export function getClassJoinRequest01(): ClassJoinRequest{
export function getClassJoinRequest01(): ClassJoinRequest {
return classJoinRequest01;
}
export function getClassJoinRequest02(): ClassJoinRequest{
export function getClassJoinRequest02(): ClassJoinRequest {
return classJoinRequest02;
}
export function getClassJoinRequest03(): ClassJoinRequest{
export function getClassJoinRequest03(): ClassJoinRequest {
return classJoinRequest03;
}
export function getClassJoinRequest04(): ClassJoinRequest{
export function getClassJoinRequest04(): ClassJoinRequest {
return classJoinRequest04;
}
}

View file

@ -56,4 +56,4 @@ export function getTeacherInvitation03(): TeacherInvitation {
export function getTeacherInvitation04(): TeacherInvitation {
return teacherInvitation04;
}
}

View file

@ -19,6 +19,6 @@ export function makeTestAttachments(em: EntityManager): Attachment[] {
let attachment01: Attachment;
export function getAttachment01(): Attachment{
export function getAttachment01(): Attachment {
return attachment01;
}

View file

@ -72,4 +72,4 @@ export function getAnswer04(): Answer {
export function getAnswer05(): Answer {
return answer05;
}
}

View file

@ -1,7 +1,12 @@
import { EntityManager } from '@mikro-orm/core';
import { Question } from '../../../src/entities/questions/question.entity';
import { getDireStraits, getNoordkaap, getTestleerling1, getTool } from '../users/students.testdata';
import { testLearningObject01, testLearningObject04, testLearningObject05, testLearningObjectMultipleChoice } from '../content/learning-objects.testdata';
import {
testLearningObject01,
testLearningObject04,
testLearningObject05,
testLearningObjectMultipleChoice,
} from '../content/learning-objects.testdata';
import { getGroup1ConditionalLearningPath, getTestGroup01, getTestGroup02 } from '../assignments/groups.testdata';
export function makeTestQuestions(em: EntityManager): Question[] {
@ -130,7 +135,6 @@ export function getQuestion06(): Question {
return question06;
}
export function getQuestion07(): Question {
return question07;
}