From d1349137b6edc2b5f9327ca92d0c8f008d5bda09 Mon Sep 17 00:00:00 2001 From: lbw <1192299468@qq.com> Date: Fri, 12 Dec 2025 11:53:24 +0800 Subject: [PATCH] =?UTF-8?q?test(service):=20=E6=B7=BB=E5=8A=A0=E8=80=83?= =?UTF-8?q?=E8=AF=95=E8=AF=84=E6=B5=8B=E7=9B=B8=E5=85=B3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增AssessmentService相关测试,生成学生单词测试卷文档 - 实现Excel文件单词数据导入及写入数据库的测试 - 新增OMR测试,加载OpenCV库并分析PNG文件坐标数据 - 测试未记忆单词识别功能,输出对应单词ID日志 - 更新.gitignore,移除对测试目录的忽略规则 --- .gitignore | 1 - .../enlish/service/mapper/TestInsert.java | 82 +++++++++++++++++++ .../yinlihupo/enlish/service/omr/TestOmr.java | 66 +++++++++++++++ .../service/service/assessmentTest.java | 58 +++++++++++++ 4 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 enlish-service/src/test/java/com/yinlihupo/enlish/service/mapper/TestInsert.java create mode 100644 enlish-service/src/test/java/com/yinlihupo/enlish/service/omr/TestOmr.java create mode 100644 enlish-service/src/test/java/com/yinlihupo/enlish/service/service/assessmentTest.java diff --git a/.gitignore b/.gitignore index 578e03d..8014373 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/enlish-service/src/test/java/com/yinlihupo/enlish/service/mapper/TestInsert.java b/enlish-service/src/test/java/com/yinlihupo/enlish/service/mapper/TestInsert.java new file mode 100644 index 0000000..5554d31 --- /dev/null +++ b/enlish-service/src/test/java/com/yinlihupo/enlish/service/mapper/TestInsert.java @@ -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 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); + } + } +} diff --git a/enlish-service/src/test/java/com/yinlihupo/enlish/service/omr/TestOmr.java b/enlish-service/src/test/java/com/yinlihupo/enlish/service/omr/TestOmr.java new file mode 100644 index 0000000..700a7ac --- /dev/null +++ b/enlish-service/src/test/java/com/yinlihupo/enlish/service/omr/TestOmr.java @@ -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 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 knowsIds = knownIds.stream().map(id -> id + "").toList(); + String path = "C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\main\\resources\\templates\\p3.png"; + List coordinatesXIES = PngUtil.analysisXY(path); + + for (CoordinatesXY coordinatesXY : coordinatesXIES) { + log.info("坐标: {}", coordinatesXY); + } + + Integer studentId = 1; + HashMap unitIdAndWordCount = new HashMap<>(); + unitIdAndWordCount.put(142, 30); + unitIdAndWordCount.put(132, 40); + + Integer assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT); + List assessmentWords; + if (assessmentDocxId == null || assessmentDocxId == 0) { + assessmentWords = assessmentService.genAssessmentWords(studentId, unitIdAndWordCount); + } else { + assessmentWords = assessmentService.getAssessmentWordsById(assessmentDocxId); + } + + List integers = PngUtil.analyzePngForUnmemorizedWordIds(path, assessmentWords, coordinatesXIES); + + for (Integer integer : integers) { + log.info("未背熟的单词ID: {}", integer); + } + } + + +} diff --git a/enlish-service/src/test/java/com/yinlihupo/enlish/service/service/assessmentTest.java b/enlish-service/src/test/java/com/yinlihupo/enlish/service/service/assessmentTest.java new file mode 100644 index 0000000..609fac6 --- /dev/null +++ b/enlish-service/src/test/java/com/yinlihupo/enlish/service/service/assessmentTest.java @@ -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 unitIdAndWordCount = new HashMap<>(); + unitIdAndWordCount.put(142, 30); + unitIdAndWordCount.put(132, 40); + + Integer assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT); + List 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 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(); + } + } +}