Skip to content

Commit

Permalink
Merge pull request #305 from SELab-2/service-tests
Browse files Browse the repository at this point in the history
Type tests
  • Loading branch information
francisvaut authored Apr 14, 2024
2 parents b2120e1 + c7b5c44 commit 054eaf9
Show file tree
Hide file tree
Showing 16 changed files with 822 additions and 64 deletions.
70 changes: 14 additions & 56 deletions frontend/src/test/unit/types/assistant.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
import { describe, it, expect } from 'vitest';

import { Assistant } from '@/types/users/Assistant';
import { assistantData } from './data';
import { createAssistant } from './helper';

describe('Assistant class', () => {
const assistantData = {
id: '1',
username: 'assistant1',
email: '[email protected]',
first_name: 'John',
last_name: 'Doe',
last_enrolled: 2024,
is_staff: true,
roles: [],
faculties: [],
courses: [],
create_time: new Date(),
last_login: null,
};

// Test constructor
it('should create an instance of Assistant with correct properties', () => {
const assistant = new Assistant(
assistantData.id,
assistantData.username,
assistantData.email,
assistantData.first_name,
assistantData.last_name,
assistantData.last_enrolled,
assistantData.is_staff,
assistantData.roles,
assistantData.faculties,
assistantData.courses,
assistantData.create_time,
assistantData.last_login,
);
describe('assistant type', () => {
it('create instance of assistant with correct properties', () => {
const assistant = createAssistant(assistantData);

expect(assistant).toBeInstanceOf(Assistant);
expect(assistant.id).toBe(assistantData.id);
Expand All @@ -43,14 +16,13 @@ describe('Assistant class', () => {
expect(assistant.last_name).toBe(assistantData.last_name);
expect(assistant.last_enrolled).toBe(assistantData.last_enrolled);
expect(assistant.is_staff).toBe(assistantData.is_staff);
expect(assistant.faculties).toBe(assistantData.faculties);
expect(assistant.courses).toBe(assistantData.courses);
expect(assistant.create_time).toBe(assistantData.create_time);
expect(assistant.last_login).toBe(assistantData.last_login);
expect(assistant.faculties).toStrictEqual(assistantData.faculties);
expect(assistant.courses).toStrictEqual(assistantData.courses);
expect(assistant.create_time).toStrictEqual(assistantData.create_time);
expect(assistant.last_login).toStrictEqual(assistantData.last_login);
});

// Test static method fromJSON
it('should create an Assistant instance from JSON data', () => {
it('create an Assistant instance from JSON data', () => {
const assistantJSON = { ...assistantData };
const assistant = Assistant.fromJSON(assistantJSON);

Expand All @@ -62,28 +34,14 @@ describe('Assistant class', () => {
expect(assistant.last_name).toBe(assistantData.last_name);
expect(assistant.last_enrolled).toBe(assistantData.last_enrolled);
expect(assistant.is_staff).toBe(assistantData.is_staff);
expect(assistant.faculties).toBe(assistantData.faculties);
expect(assistant.courses).toBe(assistantData.courses);
expect(assistant.faculties).toStrictEqual(assistantData.faculties);
expect(assistant.courses).toStrictEqual(assistantData.courses);
expect(assistant.create_time).toStrictEqual(assistantData.create_time);
expect(assistant.last_login).toStrictEqual(assistantData.last_login);
});

// Test method isAssistant
it('should return true when isAssistant method is called', () => {
const assistant = new Assistant(
assistantData.id,
assistantData.username,
assistantData.email,
assistantData.first_name,
assistantData.last_name,
assistantData.last_enrolled,
assistantData.is_staff,
assistantData.roles,
assistantData.faculties,
assistantData.courses,
assistantData.create_time,
assistantData.last_login,
);
it('return true when isAssistant method is called', () => {
const assistant = createAssistant(assistantData);

expect(assistant.isAssistant()).toBe(true);
});
Expand Down
70 changes: 70 additions & 0 deletions frontend/src/test/unit/types/course.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, it, expect } from 'vitest';

import { Course, getAcademicYear, getAcademicYears } from '@/types/Course';
import { courseData } from './data';
import { createCourse } from './helper';

describe('course type', () => {
it('create instance of course with correct properties', () => {
const course = createCourse(courseData);

expect(course).toBeInstanceOf(Course);
expect(course.id).toBe(courseData.id);
expect(course.name).toBe(courseData.name);
expect(course.description).toBe(courseData.description);
expect(course.academic_startyear).toBe(courseData.academic_startyear);
expect(course.parent_course).toBe(courseData.parent_course);
expect(course.faculty).toBe(courseData.faculty);
expect(course.teachers).toStrictEqual(courseData.teachers);
expect(course.assistants).toStrictEqual(courseData.assistants);
expect(course.students).toStrictEqual(courseData.students);
expect(course.projects).toStrictEqual(courseData.projects);
});

it('create a course instance from JSON data', () => {
const courseJSON = { ...courseData };
const course = Course.fromJSON(courseJSON);

expect(course).toBeInstanceOf(Course);
expect(course.id).toBe(courseData.id);
expect(course.name).toBe(courseData.name);
expect(course.description).toBe(courseData.description);
expect(course.academic_startyear).toBe(courseData.academic_startyear);
expect(course.parent_course).toBe(courseData.parent_course);
expect(course.faculty).toBe(courseData.faculty);
expect(course.teachers).toStrictEqual(courseData.teachers);
expect(course.assistants).toStrictEqual(courseData.assistants);
expect(course.students).toStrictEqual(courseData.students);
expect(course.projects).toStrictEqual(courseData.projects);
});

it('getCourseYear method', () => {
const course = createCourse(courseData);

expect(course.getCourseYear()).toBe(`${courseData.academic_startyear} - ${courseData.academic_startyear + 1}`);
});

it('getExcerpt method', () => {
const course = createCourse(courseData);

expect(course.getExcerpt()).toBe(courseData.description);
expect(course.getExcerpt(10)).toBe(courseData.description.substring(0, 10) + '...');
});

it('getExcerpt method with null description', () => {
const course = createCourse({ ...courseData, description: null });

expect(course.getExcerpt()).toBe('');
});

it('getAcademicYear method', () => {
expect(getAcademicYear(new Date('November 1, 2024 04:20:00'))).toBe(2024);
expect(getAcademicYear(new Date('February 1, 2024 04:20:00'))).toBe(2023);
});

it('getAcademicYears method', () => {
expect(getAcademicYears(2024, 2024)).toStrictEqual([2024]);
expect(getAcademicYears(2024, 2025)).toStrictEqual([2025, 2024]);
expect(getAcademicYears(2024, 2026)).toStrictEqual([2026, 2025, 2024]);
});
});
134 changes: 134 additions & 0 deletions frontend/src/test/unit/types/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
export const assistantData = {
id: 'assistant1_id',
username: 'assistant1',
email: '[email protected]',
first_name: 'assistant1_first_name',
last_name: 'assistant1_last_name',
last_enrolled: 2024,
is_staff: true,
roles: [],
faculties: [],
courses: [],
create_time: new Date(),
last_login: null,
};

export const studentData = {
id: 'student1_id',
username: 'student1',
email: '[email protected]',
first_name: 'student1_first_name',
last_name: 'student1_last_name',
last_enrolled: 2024,
is_staff: true,
roles: [],
faculties: [],
courses: [],
create_time: new Date(),
last_login: null,
studentId: '1',
groups: [],
};

export const teacherData = {
id: 'teacher1_id',
username: 'teacher1_username',
email: '[email protected]',
first_name: 'teacher1_first_name',
last_name: 'teacher1_last_name',
last_enrolled: 2022,
is_staff: true,
roles: [],
faculties: [],
courses: [],
create_time: new Date(),
last_login: null,
};

export const userData = {
id: 'user1_id',
username: 'user1_username',
email: '[email protected]',
first_name: 'user1_first_name',
last_name: 'user1_last_name',
last_enrolled: 2021,
is_staff: false,
roles: [],
faculties: [],
create_time: new Date(),
last_login: null,
};

export const courseData = {
id: 'course1_id',
name: 'course1_name',
description: 'course1_description',
academic_startyear: 2024,
parent_course: null,
faculty: null,
teachers: [],
assistants: [],
students: [],
projects: [],
};

export const facultyData = {
id: 'faculty1_id',
name: 'faculty1_name',
};

export const groupData = {
id: 'group1_id',
score: 10,
project: null,
students: [],
submissions: [],
};

export const projectData = {
id: 'project1_id',
name: 'project1_name',
description: 'project1_description',
visible: true,
archived: false,
locked_groups: false,
start_date: new Date('November 1, 2024 04:20:00'),
deadline: new Date('November 2, 2024 04:20:00'),
max_score: 10,
score_visible: true,
group_size: 3,
structure_file: null,
course: null,
structureChecks: [],
extra_checks: [],
groups: [],
submissions: [],
};

export const responseData = {
message: 'response message',
};

export const structureCheckData = {
id: 'structureCheck1_id',
name: 'structureCheck1_name',
obligated_extensions: [],
blocked_extensions: [],
project: null,
};

export const submissionStatusData = {
non_empty_groups: 5,
groups_submitted: 2,
submissions_passed: 1,
};

export const submissionData = {
id: 'submission1_id',
submission_number: 1,
submission_time: new Date('November 1, 2024 04:20:00'),
structure_checks_passed: true,
group: null,
files: [],
extra_checks_results: [],
};
24 changes: 24 additions & 0 deletions frontend/src/test/unit/types/faculty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, it, expect } from 'vitest';

import { Faculty } from '@/types/Faculty';
import { facultyData } from './data';
import { createFaculty } from './helper';

describe('faculty type', () => {
it('create instance of faculty with correct properties', () => {
const faculty = createFaculty(facultyData);

expect(faculty).toBeInstanceOf(Faculty);
expect(faculty.id).toBe(facultyData.id);
expect(faculty.name).toBe(facultyData.name);
});

it('create a faculty instance from JSON data', () => {
const facultyJSON = { ...facultyData };
const faculty = Faculty.fromJSON(facultyJSON);

expect(faculty).toBeInstanceOf(Faculty);
expect(faculty.id).toBe(facultyData.id);
expect(faculty.name).toBe(facultyData.name);
});
});
39 changes: 39 additions & 0 deletions frontend/src/test/unit/types/group.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect } from 'vitest';

import { Group } from '@/types/Group';
import { groupData } from './data';
import { createGroup } from './helper';

describe('group type', () => {
it('create instance of group with correct properties', () => {
const group = createGroup(groupData);

expect(group).toBeInstanceOf(Group);
expect(group.id).toBe(groupData.id);
expect(group.score).toBe(groupData.score);
expect(group.project).toBe(groupData.project);
expect(group.students).toStrictEqual(groupData.students);
expect(group.submissions).toStrictEqual(groupData.submissions);
});

it('create a minimal group instance from JSON data', () => {
const groupJSON = { ...groupData };
const group = Group.fromJSON(groupJSON);

expect(group).toBeInstanceOf(Group);
expect(group.id).toBe(groupData.id);
expect(group.score).toBe(groupData.score);
});

it('create a full group instance from JSON data', () => {
const groupJSON = { ...groupData };
const group = Group.fromJSONFullObject(groupJSON);

expect(group).toBeInstanceOf(Group);
expect(group.id).toBe(groupData.id);
expect(group.score).toBe(groupData.score);
expect(group.project).toBe(groupData.project);
expect(group.students).toStrictEqual(groupData.students);
expect(group.submissions).toStrictEqual(groupData.submissions);
});
});
Loading

0 comments on commit 054eaf9

Please sign in to comment.