Commit 67ead0bb authored by 王蔡翾's avatar 王蔡翾
Browse files

bug修复

parent 2c66995a
......@@ -3,20 +3,21 @@ package com.platform.business.mapper;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 教师端考试详情中的应考学员查询回归测试。
* 考试学员查询字段契约的回归测试。
*
* <p>详情接口复用ExamStudentVO,并由queryByExamId负责填充交卷标记。该测试直接检查
* MyBatis最终生成的SQL,防止后续调整查询列时再次遗漏submit,导致前端把已交卷学员
* 错误展示为“未交卷”。</p>
* <p>详情接口需要明确的交卷标记,最近考试接口则需要返回考试安排的日期和整数时长;
* 两组断言共同防止共享Mapper在后续修改中发生字段语义回退。</p>
*/
class ExamStudentMapperXmlTest {
......@@ -43,4 +44,33 @@ class ExamStudentMapperXmlTest {
assertTrue(sql.contains(
"case when coalesce(es.submit, 0) = 1 then true else false end submit"));
}
@Test
void recentShouldReadScheduledFieldsAndActualExamType() throws Exception {
String resource = "com/platform/business/mapper/ExamStudentMapper.xml";
Configuration configuration = new Configuration();
try (InputStream input = Resources.getResourceAsStream(resource)) {
XMLMapperBuilder builder = new XMLMapperBuilder(
input, configuration, resource, configuration.getSqlFragments());
builder.parse();
}
MappedStatement statement = configuration.getMappedStatement(
"com.platform.business.mapper.ExamStudentMapper.recent");
String sql = statement.getBoundSql(Collections.singletonMap("studentId", "student-1"))
.getSql()
.replaceAll("\\s+", " ")
.toLowerCase();
/*
* exam表保存考试安排,exam_student表保存考生本次作答过程。接口文档中的
* “开始时间”和“考试时长”属于考试安排,必须从exam表读取。type同样直接读取
* exam表的实际值,查询不能再固定为某一种考试类型。
*/
assertTrue(sql.contains("e.exam_time as starttime"));
assertTrue(sql.contains("e.duration"));
assertTrue(sql.contains("e.type"));
assertFalse(sql.contains("es.start_time as starttime"));
assertFalse(sql.contains("e.type = '实训考试'"));
}
}
......@@ -7,12 +7,15 @@ import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Locale;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** 验证历史用户列表的关键词条件不会绕过教师或其他角色筛选。 */
/** 验证用户查询条件和考试标记批量更新SQL。 */
class UserMapperXmlTest {
@Test
......@@ -34,14 +37,33 @@ class UserMapperXmlTest {
() -> "关键词与角色条件的分组不正确,实际SQL:" + sql);
}
/** 解析真实 Mapper XML,使测试覆盖 MyBatis 动态标签最终生成的 SQL。 */
@Test
void activateDueExamsShouldUseActiveHalfOpenTimeRange() throws Exception {
Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement(
"com.platform.business.mapper.UserMapper.activateDueExams")
.getBoundSql(Collections.singletonMap(
"now", Timestamp.valueOf("2026-09-18 09:00:00")));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase();
// 开始时刻包含在活动区间内,结束时刻不包含,必须与考试冲突校验的边界一致。
assertTrue(sql.contains("exam_plan.exam_time <= ?"));
assertTrue(sql.contains("greatest(coalesce(exam_plan.duration, 0), 0)"));
assertTrue(sql.contains(") > ?"));
assertTrue(sql.contains("coalesce(candidate.submit, 0) = 0"));
assertTrue(sql.contains("target_user.exam_flag = true"));
assertTrue(sql.contains("target_user.exam_oneself_id = active_exam.exam_student_id"));
assertEquals(2, boundSql.getParameterMappings().size());
}
/** 解析真实Mapper XML,使测试覆盖MyBatis动态标签最终生成的SQL。 */
private Configuration parseMapper() throws Exception {
String resource = "com/platform/business/mapper/UserMapper.xml";
Configuration configuration = new Configuration();
try (InputStream input = getClass().getClassLoader().getResourceAsStream(
"com/platform/business/mapper/UserMapper.xml")) {
try (InputStream input = getClass().getClassLoader().getResourceAsStream(resource)) {
assertNotNull(input);
new XMLMapperBuilder(input, configuration,
"com/platform/business/mapper/UserMapper.xml",
new XMLMapperBuilder(input, configuration, resource,
configuration.getSqlFragments()).parse();
}
return configuration;
......
......@@ -251,6 +251,31 @@ class ExamAdminServiceImplTest {
verify(examMapper, never()).insert(any(ExamEntity.class));
}
@Test
void createShouldRejectOverlappingExamForAnyCandidate() {
ExamAdminSaveDTO request = validCreateRequest();
mockWritableClass();
mockValidPaper();
mockValidRelationships();
when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0);
ExamAdminDetailVO conflict = new ExamAdminDetailVO();
conflict.setId(8);
conflict.setClassId(2);
conflict.setName("其他班级考试");
conflict.setChiefExaminerId("teacher-3");
conflict.setInvigilatorId("teacher-4");
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(null)))
.thenReturn(Collections.singletonList(conflict));
BusinessException error = assertThrows(BusinessException.class,
() -> service.create(request));
// 班级和教师均不相同时,冲突只能来自重复安排的应考学员。
assertEquals("应考学员与考试“其他班级考试”时间冲突", error.getMessage());
verify(examMapper, never()).insert(any(ExamEntity.class));
}
@Test
void updateShouldRejectExamThatHasStarted() {
ExamEntity existing = new ExamEntity();
......
package com.platform.business.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.business.mapper.ExamStudentMapper;
import com.platform.business.model.vo.ExamStudentVO;
import com.platform.business.model.vo.PracticePaperRuleConfigurationVO;
import com.platform.business.model.vo.RecentExamStudentVO;
import com.platform.business.service.PracticePaperRuleService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -12,14 +14,16 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.Collections;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
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;
/** 最近实训考试接口接入当前最新实操配分规则的回归测试。 */
/** 最近考试接口返回实际考试类型并接入当前最新实操配分规则的回归测试。 */
@ExtendWith(MockitoExtension.class)
class ExamStaudentServiceImplTest {
......@@ -39,9 +43,10 @@ class ExamStaudentServiceImplTest {
@Test
void recentShouldAttachLatestEffectiveRuleAndSameEventStudents() {
ExamStudentVO recent = new ExamStudentVO();
RecentExamStudentVO recent = new RecentExamStudentVO();
recent.setExamId(88);
recent.setLev("污水一级");
recent.setType("理论考试");
PracticePaperRuleConfigurationVO configuration = new PracticePaperRuleConfigurationVO();
configuration.setGrade("一级");
when(mapper.recent("student-1")).thenReturn(recent);
......@@ -49,8 +54,9 @@ class ExamStaudentServiceImplTest {
.thenReturn(configuration);
when(mapper.theSameEvent(88)).thenReturn(Collections.singletonList(new ExamStudentVO()));
ExamStudentVO result = service.recent("student-1");
RecentExamStudentVO result = service.recent("student-1");
assertEquals("理论考试", result.getType());
assertEquals(configuration, result.getPracticeRule());
assertEquals(1, result.getData().size());
}
......@@ -63,4 +69,20 @@ class ExamStaudentServiceImplTest {
verify(practicePaperRuleService, never()).effectiveConfiguration(org.mockito.ArgumentMatchers.anyString());
verify(mapper, never()).theSameEvent(org.mockito.ArgumentMatchers.anyInt());
}
@Test
void recentResponseShouldSerializeDocumentedStartTimeAndDurationTypes() throws Exception {
RecentExamStudentVO response = new RecentExamStudentVO();
response.setStartTime(new Date(0L));
response.setDuration(90);
/*
* 接口文档要求startTime为日期、duration为整数。这里直接验证最终JSON,防止后续
* 又把考生实际开始时间戳或“72分钟”一类答题时长字符串映射到该接口。
*/
String json = new ObjectMapper().writeValueAsString(response);
assertTrue(json.contains("\"startTime\":\"1970-01-01 08:00:00\""));
assertTrue(json.contains("\"duration\":90"));
}
}
......@@ -29,6 +29,7 @@ import java.util.Collections;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
......@@ -205,7 +206,7 @@ class TeachingPlanAdminServiceImplTest {
}
@Test
void exportAdminDetailShouldCreateWordWithContentAndAttachmentNames() throws Exception {
void exportAdminDetailShouldCreateWordWithUnifiedCellFontAndWithoutAttachments() throws Exception {
TeachingPlanVO plan = planVo();
plan.setReviewerName("教务管理员");
plan.setReviewOpinion("审核通过");
......@@ -245,8 +246,18 @@ class TeachingPlanAdminServiceImplTest {
document.getTables().forEach(table -> text.append(table.getText()));
assertTrue(text.toString().contains("高处作业安全教案"));
assertTrue(text.toString().contains("审核通过"));
assertTrue(text.toString().contains("安全演示.pptx"));
assertFalse(text.toString().contains("安全演示.pptx"));
assertFalse(text.toString().contains("教师签名"));
assertTrue(text.toString().contains("观看事故案例并识别风险点"));
// 固定标签和动态内容必须使用完全相同的字体、字号和颜色,防止导出后视觉不统一。
document.getTables().forEach(table -> table.getRows().forEach(row ->
row.getTableCells().forEach(cell -> cell.getParagraphs().forEach(paragraph ->
paragraph.getRuns().forEach(run -> {
assertEquals("Songti SC", run.getFontFamily());
assertEquals(10, run.getFontSize());
assertEquals("000000", run.getColor());
})))));
}
}
......
package com.platform.business.service.impl;
import com.platform.business.mapper.TheoreticalPracticeStudentSimulationMapper;
import com.platform.business.model.entity.UserEntity;
import com.platform.business.model.vo.StudentHomeVO;
import com.platform.business.service.StudentHomeService;
import com.platform.business.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/** 学员换班后,模拟试卷必须使用当前班级职级,不能继续读取历史证书等级。 */
@ExtendWith(MockitoExtension.class)
class TheoreticalPracticeStudentSimulationServiceImplTest {
@Mock
private TheoreticalPracticeStudentSimulationMapper mapper;
@Mock
private UserService userService;
@Mock
private StudentHomeService studentHomeService;
private TheoreticalPracticeStudentSimulationServiceImpl service;
@BeforeEach
void setUp() {
service = new TheoreticalPracticeStudentSimulationServiceImpl();
ReflectionTestUtils.setField(service, "baseMapper", mapper);
ReflectionTestUtils.setField(service, "userService", userService);
ReflectionTestUtils.setField(service, "studentHomeService", studentHomeService);
}
@Test
void paperInformationShouldUseCurrentClassLevelInsteadOfStoredCertificateLevel() {
UserEntity student = new UserEntity();
student.setId("student-1");
student.setLevel("13");
StudentHomeVO.Profile currentProfile = new StudentHomeVO.Profile();
currentProfile.setLevel("污水三级");
when(userService.getOne(any())).thenReturn(student);
when(studentHomeService.profile(student)).thenReturn(currentProfile);
when(mapper.selectCount(any())).thenReturn(2L);
Map<String, String> result = service.getPaperInformation("13800000000");
// 即使user.level仍是旧值13,接口也必须返回当前班级对应的新职级。
assertEquals("污水三级", result.get("lev"));
assertEquals("污水三级模拟试卷3", result.get("name"));
}
}
package com.platform.business.task;
import com.platform.business.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
/** 考试状态定时同步任务测试。 */
@ExtendWith(MockitoExtension.class)
class ExamFlagSyncTaskTest {
@Mock
private UserMapper userMapper;
@Test
void activateDueExamsShouldPassCurrentTimeToBatchUpdate() {
ExamFlagSyncTask task = new ExamFlagSyncTask();
ReflectionTestUtils.setField(task, "userMapper", userMapper);
long before = System.currentTimeMillis();
task.activateDueExams();
ArgumentCaptor<Date> now = ArgumentCaptor.forClass(Date.class);
verify(userMapper).activateDueExams(now.capture());
// 调度任务必须使用本轮执行时刻,不能缓存应用启动时的旧时间。
assertTrue(now.getValue().getTime() >= before);
assertTrue(now.getValue().getTime() <= System.currentTimeMillis());
}
}
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