forked from wangziqi/gongxue-base
新增考试归档与恢复功能
This commit is contained in:
@@ -1,14 +1,24 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { BadRequestException, ForbiddenException, ValidationPipe } from '@nestjs/common';
|
||||
import { ExamScore } from '../entities';
|
||||
import { QueryExamDto } from './dto/exam.dto';
|
||||
import { ExamsService } from './exams.service';
|
||||
|
||||
function createService(transaction: (run: (manager: any) => Promise<unknown>) => Promise<unknown>) {
|
||||
function createService(
|
||||
transaction: (run: (manager: any) => Promise<unknown>) => Promise<unknown>,
|
||||
repositories: {
|
||||
examRepo?: Record<string, jest.Mock>;
|
||||
scoreRepo?: Record<string, jest.Mock>;
|
||||
classTeacherRepo?: Record<string, jest.Mock>;
|
||||
} = {},
|
||||
) {
|
||||
return new ExamsService(
|
||||
(repositories.examRepo ?? {}) as never,
|
||||
(repositories.scoreRepo ?? {}) as never,
|
||||
{} as never,
|
||||
{} as never,
|
||||
{} as never,
|
||||
{} as never,
|
||||
{ findOne: jest.fn().mockResolvedValue({ id: 1 }) } as never,
|
||||
(repositories.classTeacherRepo ?? {
|
||||
findOne: jest.fn().mockResolvedValue({ id: 1 }),
|
||||
}) as never,
|
||||
{ transaction } as never,
|
||||
);
|
||||
}
|
||||
@@ -124,4 +134,122 @@ describe('ExamsService', () => {
|
||||
expect.objectContaining({ score: 70, classAvg: 70, rank: 1 }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('lists active exams by default and archived exams on request', async () => {
|
||||
const examRepo = {
|
||||
find: jest.fn().mockResolvedValue([]),
|
||||
};
|
||||
const service = createService(async () => undefined, { examRepo });
|
||||
|
||||
await service.findAll({});
|
||||
await service.findAll({ isArchived: true });
|
||||
|
||||
expect(examRepo.find).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
expect.objectContaining({ where: expect.objectContaining({ status: 'active' }) }),
|
||||
);
|
||||
expect(examRepo.find).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
expect.objectContaining({ where: expect.objectContaining({ status: 'archived' }) }),
|
||||
);
|
||||
});
|
||||
|
||||
it('preserves the accessible class filter for archived exams', async () => {
|
||||
const examRepo = {
|
||||
find: jest.fn().mockResolvedValue([]),
|
||||
};
|
||||
const service = createService(async () => undefined, { examRepo });
|
||||
|
||||
await service.findAll({ isArchived: true }, [3, 5]);
|
||||
|
||||
expect(examRepo.find).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ status: 'archived', classId: expect.anything() }),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('archives an exam without changing score rows', async () => {
|
||||
const examRepo = {
|
||||
findOne: jest.fn().mockResolvedValue({ id: 8, classId: 3, status: 'active' }),
|
||||
update: jest.fn().mockResolvedValue({ affected: 1 }),
|
||||
};
|
||||
const scoreRepo = { update: jest.fn(), save: jest.fn() };
|
||||
const service = createService(async () => undefined, { examRepo, scoreRepo });
|
||||
|
||||
await expect(service.archive(8, 1, true)).resolves.toEqual({ success: true });
|
||||
|
||||
expect(examRepo.update).toHaveBeenCalledWith(8, { status: 'archived' });
|
||||
expect(scoreRepo.update).not.toHaveBeenCalled();
|
||||
expect(scoreRepo.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects archiving an archived exam', async () => {
|
||||
const examRepo = {
|
||||
findOne: jest.fn().mockResolvedValue({ id: 8, classId: 3, status: 'archived' }),
|
||||
update: jest.fn(),
|
||||
};
|
||||
const service = createService(async () => undefined, { examRepo });
|
||||
|
||||
await expect(service.archive(8, 1, true)).rejects.toBeInstanceOf(BadRequestException);
|
||||
expect(examRepo.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('restores an archived exam and rejects restoring an active exam', async () => {
|
||||
const examRepo = {
|
||||
findOne: jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ id: 8, classId: 3, status: 'archived' })
|
||||
.mockResolvedValueOnce({ id: 9, classId: 3, status: 'active' }),
|
||||
update: jest.fn().mockResolvedValue({ affected: 1 }),
|
||||
};
|
||||
const service = createService(async () => undefined, { examRepo });
|
||||
|
||||
await expect(service.restore(8, 1, true)).resolves.toEqual({ success: true });
|
||||
await expect(service.restore(9, 1, true)).rejects.toBeInstanceOf(BadRequestException);
|
||||
expect(examRepo.update).toHaveBeenCalledTimes(1);
|
||||
expect(examRepo.update).toHaveBeenCalledWith(8, { status: 'active' });
|
||||
});
|
||||
|
||||
it('requires class access before archiving an exam', async () => {
|
||||
const examRepo = {
|
||||
findOne: jest.fn().mockResolvedValue({ id: 8, classId: 3, status: 'active' }),
|
||||
update: jest.fn(),
|
||||
};
|
||||
const classTeacherRepo = { findOne: jest.fn().mockResolvedValue(null) };
|
||||
const service = createService(async () => undefined, { examRepo, classTeacherRepo });
|
||||
|
||||
await expect(service.archive(8, 21, false)).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(classTeacherRepo.findOne).toHaveBeenCalledWith({ where: { userId: 21, classId: 3 } });
|
||||
expect(examRepo.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects score updates for archived exams', async () => {
|
||||
const manager = {
|
||||
findOne: jest.fn().mockResolvedValue({ id: 8, classId: 3, status: 'archived' }),
|
||||
};
|
||||
const service = createService(async (run) => run(manager));
|
||||
|
||||
await expect(service.updateScore(8, 1, 90, 1, true)).rejects.toBeInstanceOf(
|
||||
BadRequestException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('QueryExamDto - query transformation', () => {
|
||||
const pipe = new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true });
|
||||
|
||||
it.each([
|
||||
['false', false],
|
||||
['0', false],
|
||||
['true', true],
|
||||
['1', true],
|
||||
])('transforms isArchived=%s to %s', async (input, expected) => {
|
||||
await expect(
|
||||
pipe.transform(
|
||||
{ isArchived: input },
|
||||
{ type: 'query', metatype: QueryExamDto, data: undefined },
|
||||
),
|
||||
).resolves.toEqual({ isArchived: expected });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user