Commit 5fe1aba7 authored by wujipeng's avatar wujipeng
Browse files

管理员-智能考评-理论试卷管理

parent 6bae6649
package com.platform.business.service.impl;
import com.platform.business.mapper.ScoreAdminMapper;
import com.platform.business.model.dto.ScoreAdminExamQueryDTO;
import com.platform.business.model.dto.ScoreAdminStudentQueryDTO;
import com.platform.business.model.vo.ScoreAdminExamVO;
import com.platform.business.model.vo.ScoreAdminStudentDetailVO;
import com.platform.business.model.vo.ScoreAdminStudentVO;
import com.platform.business.service.ExamAdminService;
import com.platform.exception.BusinessException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletResponse;
import java.io.ByteArrayInputStream;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** 管理员成绩服务的发布边界、参数归一和Excel导出测试。 */
@ExtendWith(MockitoExtension.class)
class ScoreAdminServiceImplTest {
@Mock
private ScoreAdminMapper scoreAdminMapper;
@Mock
private ExamAdminService examAdminService;
@InjectMocks
private ScoreAdminServiceImpl service;
@Test
void examQueryShouldNormalizePublicEnumsToStoredValues() {
ScoreAdminExamQueryDTO query = new ScoreAdminExamQueryDTO();
query.setClassId(1);
query.setType("practice");
query.setStatus("published");
service.exams(query);
assertEquals("实训", query.getType());
assertEquals("PUBLISHED", query.getStatus());
verify(scoreAdminMapper).queryExams(query);
}
@Test
void questionStatisticsShouldRejectUnpublishedExam() {
ScoreAdminExamVO exam = endedExam(false);
when(scoreAdminMapper.findExam(1)).thenReturn(exam);
BusinessException error = assertThrows(BusinessException.class,
() -> service.questionStatistics(1));
assertEquals("本场考试成绩尚未正式发布,不能查看题目统计", error.getMessage());
verify(scoreAdminMapper, never()).queryTheoryStatistics(1);
}
@Test
void studentDetailShouldRejectDraftScore() {
ScoreAdminStudentDetailVO detail = new ScoreAdminStudentDetailVO();
detail.setPublished(false);
when(scoreAdminMapper.findStudent(2)).thenReturn(detail);
assertThrows(BusinessException.class, () -> service.student(2));
}
@Test
void studentScoreBandShouldDefaultToAll() {
when(scoreAdminMapper.findExam(1)).thenReturn(endedExam(false));
ScoreAdminStudentQueryDTO query = new ScoreAdminStudentQueryDTO();
service.students(1, query);
assertEquals("ALL", query.getScoreBand());
}
@Test
void exportExamShouldCreateChineseXlsxWithPublishedRows() throws Exception {
ScoreAdminExamVO exam = endedExam(true);
exam.setExamName("污水一级期末考试");
when(scoreAdminMapper.findExam(1)).thenReturn(exam);
ScoreAdminStudentVO student = new ScoreAdminStudentVO();
student.setExamName(exam.getExamName());
student.setExamType("理论");
student.setClassName("污水一级一班");
student.setStudentName("张三");
student.setIdCard("310000000000000001");
student.setScore(new BigDecimal("88.5"));
student.setGrade("良好");
student.setPassed(true);
when(scoreAdminMapper.queryPublishedStudents(1)).thenReturn(Collections.singletonList(student));
MockHttpServletResponse response = new MockHttpServletResponse();
service.exportExam(1, response);
assertTrue(response.getHeader("Content-Disposition").contains("UTF-8''"));
try (XSSFWorkbook workbook = new XSSFWorkbook(
new ByteArrayInputStream(response.getContentAsByteArray()))) {
assertEquals("成绩明细", workbook.getSheetAt(0).getSheetName());
assertEquals("学员姓名", workbook.getSheetAt(0).getRow(0).getCell(5).getStringCellValue());
assertEquals("张三", workbook.getSheetAt(0).getRow(1).getCell(5).getStringCellValue());
}
}
private ScoreAdminExamVO endedExam(boolean published) {
ScoreAdminExamVO exam = new ScoreAdminExamVO();
exam.setExamId(1);
exam.setExamTime(new Date(System.currentTimeMillis() - 3_600_000L));
exam.setDuration(30);
exam.setPublished(published);
exam.setReviewStatus(published ? "PUBLISHED" : "PENDING_REVIEW");
return exam;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment