test(service): 添加考试评测相关单元测试

- 新增AssessmentService相关测试,生成学生单词测试卷文档
- 实现Excel文件单词数据导入及写入数据库的测试
- 新增OMR测试,加载OpenCV库并分析PNG文件坐标数据
- 测试未记忆单词识别功能,输出对应单词ID日志
- 更新.gitignore,移除对测试目录的忽略规则
This commit is contained in:
lbw
2025-12-12 11:53:24 +08:00
parent b01810191e
commit d1349137b6
4 changed files with 206 additions and 1 deletions

1
.gitignore vendored
View File

@@ -10,7 +10,6 @@
/httpRequests/
.idea
enlish-service/target
enlish-service/src/test
enlish-framework/enlish-common/target
enlish-framework/enlish-spring-boot-starter-biz-operationlog/target
enlish-framework/enlish-spring-boot-starter-jackson/target

View File

@@ -0,0 +1,82 @@
package com.yinlihupo.enlish.service.mapper;
import com.yinlihupo.enlish.service.domain.dataobject.UnitDO;
import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO;
import com.yinlihupo.enlish.service.domain.mapper.UnitDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.VocabularyBankDOMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
@SpringBootTest
@Slf4j
public class TestInsert {
@Resource
private VocabularyBankDOMapper vocabularyBankMapper;
@Resource
private UnitDOMapper unitDOMapper;
@Test
void test() {
String file = "C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\test\\java\\com\\yinlihupo\\enlish\\service\\mapper\\min.xlsx";
HashMap<String, Integer> map = new HashMap<>();
try (FileInputStream fis = new FileInputStream(file); Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
String gradeUnit = row.getCell(0).getStringCellValue();
String word = row.getCell(1).getStringCellValue();
String phonetic = row.getCell(2) != null ? row.getCell(2).getStringCellValue() : "";
String meaning = row.getCell(3) != null ? row.getCell(3).getStringCellValue() : "";
int gradeUnitId = 0;
if (map.containsKey(gradeUnit)) {
gradeUnitId = map.get(gradeUnit);
} else {
UnitDO unitDO = unitDOMapper.selectByTitle(gradeUnit);
if (unitDO == null) {
unitDO = UnitDO.builder()
.title(gradeUnit)
.version("人教版")
.createAt(LocalDateTime.now())
.build();
unitDOMapper.insert(unitDO);
gradeUnitId = unitDO.getId();
} else {
gradeUnitId = unitDO.getId();
}
map.put(gradeUnit, gradeUnitId);
}
VocabularyBankDO vocabularyBankDO = VocabularyBankDO.builder()
.word(word)
.definition(meaning)
.pronunciation(phonetic)
.unitId(gradeUnitId)
.build();
vocabularyBankMapper.insertSelective(vocabularyBankDO);
log.info("插入数据 {} ", vocabularyBankDO);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,66 @@
package com.yinlihupo.enlish.service.omr;
import com.yinlihupo.enlish.service.enums.AssessmentsType;
import com.yinlihupo.enlish.service.model.bo.CoordinatesXY;
import com.yinlihupo.enlish.service.model.bo.Word;
import com.yinlihupo.enlish.service.service.AssessmentService;
import com.yinlihupo.enlish.service.utils.PngUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import nu.pattern.OpenCV;
import org.junit.jupiter.api.Test;
import org.opencv.core.*;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
@Slf4j
public class TestOmr {
@Resource
private AssessmentService assessmentService;
@Test
public void testOmr(){
OpenCV.loadLocally();
List<Integer> knownIds = Arrays.asList(3184, 3185, 3186, 3187, 3188, 3189, 3190, 3191, 3192, 3193,
3195, 3196, 3197, 3198, 3199, 3200, 3201, 3202, 3203, 3204,
3206, 3208, 3209, 3210, 3211, 3212, 3507, 3508, 3509, 3510,
3511, 3512, 3513, 3514, 3515, 3516, 3517, 3519, 3521, 3522,
3523, 3524, 3525, 3526, 3527, 3528, 3529, 3530, 3531, 3532,
3533, 3535, 3536, 3537, 3538, 3539);
List<String> knowsIds = knownIds.stream().map(id -> id + "").toList();
String path = "C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\main\\resources\\templates\\p3.png";
List<CoordinatesXY> coordinatesXIES = PngUtil.analysisXY(path);
for (CoordinatesXY coordinatesXY : coordinatesXIES) {
log.info("坐标: {}", coordinatesXY);
}
Integer studentId = 1;
HashMap<Integer, Integer> unitIdAndWordCount = new HashMap<>();
unitIdAndWordCount.put(142, 30);
unitIdAndWordCount.put(132, 40);
Integer assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT);
List<Word> assessmentWords;
if (assessmentDocxId == null || assessmentDocxId == 0) {
assessmentWords = assessmentService.genAssessmentWords(studentId, unitIdAndWordCount);
} else {
assessmentWords = assessmentService.getAssessmentWordsById(assessmentDocxId);
}
List<Integer> integers = PngUtil.analyzePngForUnmemorizedWordIds(path, assessmentWords, coordinatesXIES);
for (Integer integer : integers) {
log.info("未背熟的单词ID: {}", integer);
}
}
}

View File

@@ -0,0 +1,58 @@
package com.yinlihupo.enlish.service.service;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.yinlihupo.enlish.service.enums.AssessmentsType;
import com.yinlihupo.enlish.service.model.bo.Word;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class assessmentTest {
@Resource
private AssessmentService assessmentService;
@Test
public void generateAssessmentDocxStudent() {
Integer studentId = 1;
HashMap<Integer, Integer> unitIdAndWordCount = new HashMap<>();
unitIdAndWordCount.put(142, 30);
unitIdAndWordCount.put(132, 40);
Integer assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT);
List<Word> assessmentWords;
if (assessmentDocxId == null || assessmentDocxId == 0) {
assessmentWords = assessmentService.genAssessmentWords(studentId, unitIdAndWordCount);
assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT);
} else {
assessmentWords = assessmentService.getAssessmentWordsById(assessmentDocxId);
}
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder()
.bind("words", policy)
.build();
Map<String, Object> data = new HashMap<>();
data.put("assessment_id", assessmentDocxId);
data.put("words", assessmentWords);
// 4. 渲染并输出
try (XWPFTemplate template = XWPFTemplate.compile("C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\main\\resources\\templates\\assessment_v4.docx", config)) {
template.render(data);
template.write(new FileOutputStream("学生单词测试卷.docx"));
System.out.println("文档生成成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}