Skip to content

Commit

Permalink
test: syncTakenLecture
Browse files Browse the repository at this point in the history
  • Loading branch information
doxylee committed Nov 5, 2024
1 parent b2ce946 commit 931a8a8
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"client:generate": "npx prisma generate --schema src/prisma/schema.prisma",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint:check": "eslint .",
"test": "dotenv -e env/.env.test -- jest",
"test": "dotenv -e env/.env.test -- jest --maxWorkers 1",
"test:watch": "dotenv -e env/.env.test -- jest --watch",
"test:cov": "dotenv -e env/.env.test -- jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand Down
233 changes: 233 additions & 0 deletions src/modules/sync/syncTakenLecture.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppModule } from '@src/app.module';
import { PrismaService } from '@src/prisma/prisma.service';
import { SyncTakenLectureService } from './syncTakenLecture.service';

// This tests on test database only. Add `DATABASE_URL` with `otlplus_test` database to run this test.

const maybe = process.env.DATABASE_URL?.includes('/otlplus_test?')
? describe
: describe.skip;

const userData = [...Array(5).keys()].map((i) => ({
id: i + 1,
student_id: `3000000${i}`,
sid: i.toString(),
date_joined: new Date(),
first_name: 'test',
last_name: 'test',
}));

const departmentData = [...Array(2).keys()].map((i) => ({
id: i + 1,
num_id: (10 + i).toString(),
code: String.fromCharCode(65 + i),
name: `학과${i}`,
name_en: `department${i}`,
visible: true,
}));
const courseData = [...Array(2).keys()].map((i) => ({
id: i + 1,
old_code: `${departmentData[i].code}10${i}`,
department_id: departmentData[i].id,
type: '전공',
type_en: 'major',
title: `과목 ${i}`,
title_en: `subject ${i}`,
summury: '',
grade_sum: 0,
load_sum: 0,
speech_sum: 0,
review_total_weight: 0,
grade: 0,
load: 0,
speech: 0,
title_no_space: `과목${i}`,
title_en_no_space: `subject${i}`,
}));

const lectureData = [...Array(2).keys()].map((i) => ({
id: i + 1,
code: `${departmentData[i].num_id}:10${i}`,
year: 3000,
semester: 1,
class_no: String.fromCharCode(65 + i),
department_id: departmentData[i].id,
old_code: courseData[i].old_code,
title: courseData[i].title,
title_en: courseData[i].title_en,
type: courseData[i].type,
type_en: courseData[i].type_en,
audience: 0,
limit: 0,
credit: 3,
credit_au: 0,
num_classes: 3,
num_labs: 0,
is_english: false,
course_id: courseData[i].id,
deleted: false,
grade_sum: 0,
load_sum: 0,
speech_sum: 0,
review_total_weight: 0,
grade: 0,
load: 0,
speech: 0,
title_no_space: courseData[i].title_no_space,
title_en_no_space: courseData[i].title_en_no_space,
}));

const takenLectureData = [...Array(2).keys()].map((i) => ({
id: i + 1,
userprofile_id: userData[i].id,
lecture_id: lectureData[i].id,
}));

const attendBase = {
lecture_year: 3000,
lecture_term: 1,
subject_no: lectureData[0].code,
lecture_class: lectureData[0].class_no,
dept_id: departmentData[0].id,
student_no: parseInt(userData[0].student_id),
process_type: 'I',
} as const;

maybe('SyncTakenLectureService', () => {
let service: SyncTakenLectureService;
let prisma: PrismaService;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
providers: [],
}).compile();

service = module.get<SyncTakenLectureService>(SyncTakenLectureService);
prisma = module.get<PrismaService>(PrismaService);

await prisma.session_userprofile_taken_lectures.deleteMany();
await prisma.session_userprofile.deleteMany();
await prisma.subject_lecture_professors.deleteMany();
await prisma.subject_lecture.deleteMany();
await prisma.subject_course.deleteMany();
await prisma.subject_department.deleteMany();

await prisma.session_userprofile.createMany({ data: userData });
await prisma.subject_department.createMany({ data: departmentData });
await prisma.subject_course.createMany({ data: courseData });
await prisma.subject_lecture.createMany({ data: lectureData });
});

afterAll(async () => {
await prisma.session_userprofile_taken_lectures.deleteMany();
await prisma.session_userprofile.deleteMany();
await prisma.subject_lecture_professors.deleteMany();
await prisma.subject_lecture.deleteMany();
await prisma.subject_course.deleteMany();
await prisma.subject_department.deleteMany();
});

beforeEach(async () => {
await prisma.session_userprofile_taken_lectures.createMany({
data: takenLectureData,
});
});

afterEach(async () => {
await prisma.session_userprofile_taken_lectures.deleteMany();
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should update taken lectures', async () => {
const result = await service.syncTakenLecture({
year: 3000,
semester: 1,
attend: [
{
...attendBase,
subject_no: lectureData[1].code,
lecture_class: lectureData[1].class_no,
dept_id: departmentData[1].id,
},
],
});

expect(result.updated).toHaveLength(2);
expect(result.errors).toHaveLength(0);
expect(
result.updated.filter(
(u: any) => u.studentId.toString() === userData[0].student_id,
)[0],
).toMatchObject({
studentId: parseInt(userData[0].student_id),
add: [lectureData[1].id],
remove: [takenLectureData[0].id],
});
expect(
result.updated.filter(
(u: any) => u.studentId.toString() === userData[1].student_id,
)[0],
).toMatchObject({
studentId: parseInt(userData[1].student_id),
add: [],
remove: [takenLectureData[1].id],
});

const takenLectures =
await prisma.session_userprofile_taken_lectures.findMany();
expect(takenLectures).toHaveLength(1);
expect(takenLectures[0]).toMatchObject({
userprofile_id: 1,
lecture_id: 2,
});
});

it('should handle error', async () => {
const result = await service.syncTakenLecture({
year: 3000,
semester: 1,
attend: [
{
...attendBase,
subject_no: 'invalid',
},
],
});

expect(result.errors).toHaveLength(1);
expect(result.errors[0]).toMatchObject({
student_no: parseInt(userData[0].student_id),
attend: {
...attendBase,
subject_no: 'invalid',
},
error: 'lecture not found',
});
});

it('should not update if no change', async () => {
const result = await service.syncTakenLecture({
year: 3000,
semester: 1,
attend: [
{
...attendBase,
},
{
...attendBase,
student_no: parseInt(userData[1].student_id),
subject_no: lectureData[1].code,
lecture_class: lectureData[1].class_no,
dept_id: departmentData[1].id,
},
],
});

expect(result.updated).toHaveLength(0);
expect(result.errors).toHaveLength(0);
});
});
2 changes: 2 additions & 0 deletions src/modules/sync/syncTakenLecture.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export class SyncTakenLectureService {
this.slackNoti.sendSyncNoti(
`syncTakenLecture: ${result.updated.length} updated, ${result.errors.length} errors`,
);

return result;
}

getLectureIdOfAttendRecord(
Expand Down

0 comments on commit 931a8a8

Please sign in to comment.