Commit cba32552d5cf1964bb022695695e09399e4bf012
1 parent
3d556dfb
✨ feat(mes): 添加 MES 编码生成策略的单元测试
Showing
10 changed files
with
563 additions
and
11 deletions
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/dal/mysql/md/autocode/MesMdAutoCodeRecordMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.mes.dal.mysql.md.autocode; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | 3 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | |
| 6 | -import cn.iocoder.yudao.module.mes.controller.admin.md.autocode.vo.record.MesMdAutoCodeRecordPageReqVO; | |
| 7 | 4 | import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodeRecordDO; |
| 8 | 5 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 6 | |
| ... | ... | @@ -15,14 +12,6 @@ import org.apache.ibatis.annotations.Mapper; |
| 15 | 12 | @Mapper |
| 16 | 13 | public interface MesMdAutoCodeRecordMapper extends BaseMapperX<MesMdAutoCodeRecordDO> { |
| 17 | 14 | |
| 18 | - default PageResult<MesMdAutoCodeRecordDO> selectPage(MesMdAutoCodeRecordPageReqVO reqVO) { | |
| 19 | - return selectPage(reqVO, new LambdaQueryWrapperX<MesMdAutoCodeRecordDO>() | |
| 20 | - .eqIfPresent(MesMdAutoCodeRecordDO::getRuleId, reqVO.getRuleId()) | |
| 21 | - .likeIfPresent(MesMdAutoCodeRecordDO::getResult, reqVO.getResult()) | |
| 22 | - .betweenIfPresent(MesMdAutoCodeRecordDO::getCreateTime, reqVO.getCreateTime()) | |
| 23 | - .orderByDesc(MesMdAutoCodeRecordDO::getId)); | |
| 24 | - } | |
| 25 | - | |
| 26 | 15 | default MesMdAutoCodeRecordDO selectByResult(String result) { |
| 27 | 16 | return selectOne(MesMdAutoCodeRecordDO::getResult, result); |
| 28 | 17 | } | ... | ... |
yudao-module-mes/src/test/java/cn/iocoder/yudao/module/mes/service/md/autocode/MesMdAutoCodeRecordServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.mes.service.md.autocode; | |
| 2 | + | |
| 3 | +import cn.hutool.core.util.ReflectUtil; | |
| 4 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodePartDO; | |
| 5 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodeRecordDO; | |
| 6 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodeRuleDO; | |
| 7 | +import cn.iocoder.yudao.module.mes.dal.mysql.md.autocode.MesMdAutoCodeRecordMapper; | |
| 8 | +import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodePartTypeEnum; | |
| 9 | +import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodeContext; | |
| 10 | +import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodePartStrategy; | |
| 11 | +import org.junit.jupiter.api.BeforeEach; | |
| 12 | +import org.junit.jupiter.api.Test; | |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; | |
| 14 | +import org.mockito.ArgumentCaptor; | |
| 15 | +import org.mockito.InjectMocks; | |
| 16 | +import org.mockito.Mock; | |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; | |
| 18 | + | |
| 19 | +import java.util.Arrays; | |
| 20 | +import java.util.List; | |
| 21 | + | |
| 22 | +import static org.junit.jupiter.api.Assertions.*; | |
| 23 | +import static org.mockito.ArgumentMatchers.*; | |
| 24 | +import static org.mockito.Mockito.*; | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * {@link MesMdAutoCodeRecordServiceImpl} 的单元测试 | |
| 28 | + * | |
| 29 | + * @author 芋道源码 | |
| 30 | + */ | |
| 31 | +@ExtendWith(MockitoExtension.class) | |
| 32 | +public class MesMdAutoCodeRecordServiceImplTest { | |
| 33 | + | |
| 34 | + @InjectMocks | |
| 35 | + private MesMdAutoCodeRecordServiceImpl recordService; | |
| 36 | + | |
| 37 | + @Mock | |
| 38 | + private MesMdAutoCodeRecordMapper recordMapper; | |
| 39 | + | |
| 40 | + @Mock | |
| 41 | + private MesMdAutoCodeRuleService ruleService; | |
| 42 | + @Mock | |
| 43 | + private MesMdAutoCodePartService partService; | |
| 44 | + | |
| 45 | + @Mock | |
| 46 | + private MesMdAutoCodePartStrategy fixedCharStrategy; | |
| 47 | + @Mock | |
| 48 | + private MesMdAutoCodePartStrategy serialNumberStrategy; | |
| 49 | + @Mock | |
| 50 | + private MesMdAutoCodePartStrategy inputCharStrategy; | |
| 51 | + @Mock | |
| 52 | + private MesMdAutoCodePartStrategy dateStrategy; | |
| 53 | + | |
| 54 | + @BeforeEach | |
| 55 | + public void setUp() { | |
| 56 | + // 使用反射注入 strategyList | |
| 57 | + List<MesMdAutoCodePartStrategy> strategyList = Arrays.asList( | |
| 58 | + fixedCharStrategy, serialNumberStrategy, inputCharStrategy, dateStrategy | |
| 59 | + ); | |
| 60 | + when(fixedCharStrategy.getType()).thenReturn(MesMdAutoCodePartTypeEnum.FIXED_CHAR.getType()); | |
| 61 | + when(serialNumberStrategy.getType()).thenReturn(MesMdAutoCodePartTypeEnum.SERIAL_NUMBER.getType()); | |
| 62 | + when(inputCharStrategy.getType()).thenReturn(MesMdAutoCodePartTypeEnum.INPUT_CHAR.getType()); | |
| 63 | + when(dateStrategy.getType()).thenReturn(MesMdAutoCodePartTypeEnum.DATE.getType()); | |
| 64 | + ReflectUtil.setFieldValue(recordService, "strategyList", strategyList); | |
| 65 | + // 手动调用 init 方法初始化策略 map | |
| 66 | + recordService.init(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Test | |
| 70 | + public void testGenerateAutoCode_fixedCharAndSerialNumber() { | |
| 71 | + // 准备参数 | |
| 72 | + String ruleCode = "ITEM_CODE"; | |
| 73 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(1L).setCode(ruleCode).setPadded(false); | |
| 74 | + MesMdAutoCodePartDO part1 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.FIXED_CHAR.getType()) | |
| 75 | + .setFixCharacter("ITEM_").setLength(5).setSort(1); | |
| 76 | + MesMdAutoCodePartDO part2 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.SERIAL_NUMBER.getType()) | |
| 77 | + .setSerialStartNo(1).setSerialStep(1).setLength(4).setSort(2).setCycleFlag(false); | |
| 78 | + // mock | |
| 79 | + when(ruleService.getAutoCodeRuleByCode(ruleCode)).thenReturn(rule); | |
| 80 | + when(partService.getAutoCodePartListByRuleId(1L)).thenReturn(Arrays.asList(part1, part2)); | |
| 81 | + when(fixedCharStrategy.generate(eq(part1), any(MesMdAutoCodeContext.class))).thenReturn("ITEM_"); | |
| 82 | + when(serialNumberStrategy.generate(eq(part2), any(MesMdAutoCodeContext.class))).thenAnswer(invocation -> { | |
| 83 | + MesMdAutoCodeContext context = invocation.getArgument(1); | |
| 84 | + context.setSerialNo(1); | |
| 85 | + return "0001"; | |
| 86 | + }); | |
| 87 | + when(recordMapper.selectByResult("ITEM_0001")).thenReturn(null); | |
| 88 | + | |
| 89 | + // 调用 | |
| 90 | + String result = recordService.generateAutoCode(ruleCode, null); | |
| 91 | + // 断言 | |
| 92 | + assertEquals("ITEM_0001", result); | |
| 93 | + ArgumentCaptor<MesMdAutoCodeRecordDO> captor = ArgumentCaptor.forClass(MesMdAutoCodeRecordDO.class); | |
| 94 | + verify(recordMapper).insert(captor.capture()); | |
| 95 | + MesMdAutoCodeRecordDO record = captor.getValue(); | |
| 96 | + assertEquals(1L, record.getRuleId()); | |
| 97 | + assertEquals("ITEM_0001", record.getResult()); | |
| 98 | + assertEquals(1, record.getSerialNo()); | |
| 99 | + } | |
| 100 | + | |
| 101 | + @Test | |
| 102 | + public void testGenerateAutoCode_withInputChar() { | |
| 103 | + // 准备参数 | |
| 104 | + String ruleCode = "CLIENT_CODE"; | |
| 105 | + String inputChar = "A"; | |
| 106 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(2L).setCode(ruleCode).setPadded(false); | |
| 107 | + MesMdAutoCodePartDO part1 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.INPUT_CHAR.getType()) | |
| 108 | + .setLength(1).setSort(1); | |
| 109 | + MesMdAutoCodePartDO part2 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.SERIAL_NUMBER.getType()) | |
| 110 | + .setSerialStartNo(1).setSerialStep(1).setLength(4).setSort(2).setCycleFlag(false); | |
| 111 | + // mock | |
| 112 | + when(ruleService.getAutoCodeRuleByCode(ruleCode)).thenReturn(rule); | |
| 113 | + when(partService.getAutoCodePartListByRuleId(2L)).thenReturn(Arrays.asList(part1, part2)); | |
| 114 | + when(inputCharStrategy.generate(eq(part1), any(MesMdAutoCodeContext.class))).thenReturn("A"); | |
| 115 | + when(serialNumberStrategy.generate(eq(part2), any(MesMdAutoCodeContext.class))).thenReturn("0001"); | |
| 116 | + when(recordMapper.selectByResult("A0001")).thenReturn(null); | |
| 117 | + | |
| 118 | + // 调用 | |
| 119 | + String result = recordService.generateAutoCode(ruleCode, inputChar); | |
| 120 | + // 断言 | |
| 121 | + assertEquals("A0001", result); | |
| 122 | + ArgumentCaptor<MesMdAutoCodeRecordDO> captor = ArgumentCaptor.forClass(MesMdAutoCodeRecordDO.class); | |
| 123 | + verify(recordMapper).insert(captor.capture()); | |
| 124 | + MesMdAutoCodeRecordDO record = captor.getValue(); | |
| 125 | + assertEquals(inputChar, record.getInputChar()); | |
| 126 | + assertEquals("A0001", record.getResult()); | |
| 127 | + } | |
| 128 | + | |
| 129 | + @Test | |
| 130 | + public void testGenerateAutoCode_withPadding() { | |
| 131 | + // 准备参数 | |
| 132 | + String ruleCode = "PADDED_CODE"; | |
| 133 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(3L).setCode(ruleCode) | |
| 134 | + .setPadded(true).setMaxLength(10).setPaddedChar("0").setPaddedMethod(1); | |
| 135 | + MesMdAutoCodePartDO part1 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.FIXED_CHAR.getType()) | |
| 136 | + .setFixCharacter("IT").setLength(2).setSort(1); | |
| 137 | + // mock | |
| 138 | + when(ruleService.getAutoCodeRuleByCode(ruleCode)).thenReturn(rule); | |
| 139 | + when(partService.getAutoCodePartListByRuleId(3L)).thenReturn(Arrays.asList(part1)); | |
| 140 | + when(fixedCharStrategy.generate(eq(part1), any(MesMdAutoCodeContext.class))).thenReturn("IT"); | |
| 141 | + when(recordMapper.selectByResult("00000000IT")).thenReturn(null); | |
| 142 | + | |
| 143 | + // 调用 | |
| 144 | + String result = recordService.generateAutoCode(ruleCode, null); | |
| 145 | + // 断言:IT 补齐到 10 位,左补 0 | |
| 146 | + assertEquals("00000000IT", result); | |
| 147 | + verify(recordMapper).insert(any(MesMdAutoCodeRecordDO.class)); | |
| 148 | + } | |
| 149 | + | |
| 150 | + @Test | |
| 151 | + public void testGenerateAutoCode_withDatePart() { | |
| 152 | + // 准备参数 | |
| 153 | + String ruleCode = "DATE_CODE"; | |
| 154 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(4L).setCode(ruleCode).setPadded(false); | |
| 155 | + MesMdAutoCodePartDO part1 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.DATE.getType()) | |
| 156 | + .setDateFormat("yyyyMMdd").setLength(8).setSort(1); | |
| 157 | + MesMdAutoCodePartDO part2 = new MesMdAutoCodePartDO().setType(MesMdAutoCodePartTypeEnum.SERIAL_NUMBER.getType()) | |
| 158 | + .setSerialStartNo(1).setSerialStep(1).setLength(4).setSort(2).setCycleFlag(false); | |
| 159 | + | |
| 160 | + // mock | |
| 161 | + when(ruleService.getAutoCodeRuleByCode(ruleCode)).thenReturn(rule); | |
| 162 | + when(partService.getAutoCodePartListByRuleId(4L)).thenReturn(Arrays.asList(part1, part2)); | |
| 163 | + when(dateStrategy.generate(eq(part1), any(MesMdAutoCodeContext.class))).thenReturn("20260304"); | |
| 164 | + when(serialNumberStrategy.generate(eq(part2), any(MesMdAutoCodeContext.class))).thenReturn("0001"); | |
| 165 | + when(recordMapper.selectByResult("202603040001")).thenReturn(null); | |
| 166 | + | |
| 167 | + // 调用 | |
| 168 | + String result = recordService.generateAutoCode(ruleCode, null); | |
| 169 | + // 断言:日期 8 位 + 流水号 4 位 = 12 位 | |
| 170 | + assertEquals(12, result.length()); | |
| 171 | + assertEquals("202603040001", result); | |
| 172 | + verify(recordMapper).insert(any(MesMdAutoCodeRecordDO.class)); | |
| 173 | + } | |
| 174 | + | |
| 175 | +} | ... | ... |
yudao-module-mes/src/test/java/cn/iocoder/yudao/module/mes/service/md/autocode/strategy/impl/MesMdAutoCodeDatePartStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.mes.service.md.autocode.strategy.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodePartDO; | |
| 4 | +import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodeContext; | |
| 5 | +import org.junit.jupiter.api.Test; | |
| 6 | + | |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * {@link MesMdAutoCodeDatePartStrategy} 的单元测试 | |
| 12 | + * | |
| 13 | + * @author 芋道源码 | |
| 14 | + */ | |
| 15 | +public class MesMdAutoCodeDatePartStrategyTest { | |
| 16 | + | |
| 17 | + private final MesMdAutoCodeDatePartStrategy strategy = new MesMdAutoCodeDatePartStrategy(); | |
| 18 | + | |
| 19 | + @Test | |
| 20 | + public void testGenerate_withDefaultFormat() { | |
| 21 | + // 准备参数 | |
| 22 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setDateFormat(null); | |
| 23 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext(); | |
| 24 | + | |
| 25 | + // 调用 | |
| 26 | + String result = strategy.generate(part, context); | |
| 27 | + | |
| 28 | + // 断言:默认格式为 yyyyMMdd,长度为 8 | |
| 29 | + assertEquals(8, result.length()); | |
| 30 | + assertTrue(result.matches("\\d{8}")); | |
| 31 | + } | |
| 32 | + | |
| 33 | + @Test | |
| 34 | + public void testGenerate_withCustomFormat() { | |
| 35 | + // 准备参数 | |
| 36 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setDateFormat("yyyyMM"); | |
| 37 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext(); | |
| 38 | + | |
| 39 | + // 调用 | |
| 40 | + String result = strategy.generate(part, context); | |
| 41 | + // 断言:格式为 yyyyMM,长度为 6 | |
| 42 | + assertEquals(6, result.length()); | |
| 43 | + assertTrue(result.matches("\\d{6}")); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Test | |
| 47 | + public void testGenerate_withTimeFormat() { | |
| 48 | + // 准备参数 | |
| 49 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setDateFormat("HHmmss"); | |
| 50 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext(); | |
| 51 | + | |
| 52 | + // 调用 | |
| 53 | + String result = strategy.generate(part, context); | |
| 54 | + // 断言:格式为 HHmmss,长度为 6 | |
| 55 | + assertEquals(6, result.length()); | |
| 56 | + assertTrue(result.matches("\\d{6}")); | |
| 57 | + } | |
| 58 | + | |
| 59 | +} | ... | ... |
yudao-module-mes/src/test/java/cn/iocoder/yudao/module/mes/service/md/autocode/strategy/impl/MesMdAutoCodeFixedCharPartStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.mes.service.md.autocode.strategy.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodePartDO; | |
| 4 | +import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodeContext; | |
| 5 | +import org.junit.jupiter.api.Test; | |
| 6 | + | |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * {@link MesMdAutoCodeFixedCharPartStrategy} 的单元测试 | |
| 11 | + * | |
| 12 | + * @author 芋道源码 | |
| 13 | + */ | |
| 14 | +public class MesMdAutoCodeFixedCharPartStrategyTest { | |
| 15 | + | |
| 16 | + private final MesMdAutoCodeFixedCharPartStrategy strategy = new MesMdAutoCodeFixedCharPartStrategy(); | |
| 17 | + | |
| 18 | + @Test | |
| 19 | + public void testGenerate_withFixedChar() { | |
| 20 | + // 准备参数 | |
| 21 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setFixCharacter("ITEM_"); | |
| 22 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext(); | |
| 23 | + | |
| 24 | + // 调用 | |
| 25 | + String result = strategy.generate(part, context); | |
| 26 | + // 断言 | |
| 27 | + assertEquals("ITEM_", result); | |
| 28 | + } | |
| 29 | + | |
| 30 | + @Test | |
| 31 | + public void testGenerate_withEmptyFixedChar() { | |
| 32 | + // 准备参数 | |
| 33 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setFixCharacter(""); | |
| 34 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext(); | |
| 35 | + | |
| 36 | + // 调用 | |
| 37 | + String result = strategy.generate(part, context); | |
| 38 | + // 断言 | |
| 39 | + assertEquals("", result); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Test | |
| 43 | + public void testGenerate_withNullFixedChar() { | |
| 44 | + // 准备参数 | |
| 45 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setFixCharacter(null); | |
| 46 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext(); | |
| 47 | + | |
| 48 | + // 调用 | |
| 49 | + String result = strategy.generate(part, context); | |
| 50 | + // 断言 | |
| 51 | + assertEquals("", result); | |
| 52 | + } | |
| 53 | + | |
| 54 | +} | ... | ... |
yudao-module-mes/src/test/java/cn/iocoder/yudao/module/mes/service/md/autocode/strategy/impl/MesMdAutoCodeInputCharPartStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.mes.service.md.autocode.strategy.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodePartDO; | |
| 4 | +import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodeContext; | |
| 5 | +import org.junit.jupiter.api.Test; | |
| 6 | + | |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * {@link MesMdAutoCodeInputCharPartStrategy} 的单元测试 | |
| 11 | + * | |
| 12 | + * @author 芋道源码 | |
| 13 | + */ | |
| 14 | +public class MesMdAutoCodeInputCharPartStrategyTest { | |
| 15 | + | |
| 16 | + private final MesMdAutoCodeInputCharPartStrategy strategy = new MesMdAutoCodeInputCharPartStrategy(); | |
| 17 | + | |
| 18 | + @Test | |
| 19 | + public void testGenerate_withInputChar() { | |
| 20 | + // 准备参数 | |
| 21 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO(); | |
| 22 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setInputChar("ABC"); | |
| 23 | + | |
| 24 | + // 调用 | |
| 25 | + String result = strategy.generate(part, context); | |
| 26 | + // 断言 | |
| 27 | + assertEquals("ABC", result); | |
| 28 | + } | |
| 29 | + | |
| 30 | + @Test | |
| 31 | + public void testGenerate_withEmptyInputChar() { | |
| 32 | + // 准备参数 | |
| 33 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO(); | |
| 34 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setInputChar(""); | |
| 35 | + | |
| 36 | + // 调用 | |
| 37 | + String result = strategy.generate(part, context); | |
| 38 | + // 断言 | |
| 39 | + assertEquals("", result); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Test | |
| 43 | + public void testGenerate_withNullInputChar() { | |
| 44 | + // 准备参数 | |
| 45 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO(); | |
| 46 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setInputChar(null); | |
| 47 | + | |
| 48 | + // 调用 | |
| 49 | + String result = strategy.generate(part, context); | |
| 50 | + // 断言 | |
| 51 | + assertEquals("", result); | |
| 52 | + } | |
| 53 | + | |
| 54 | +} | ... | ... |
yudao-module-mes/src/test/java/cn/iocoder/yudao/module/mes/service/md/autocode/strategy/impl/MesMdAutoCodeSerialNumberPartStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.mes.service.md.autocode.strategy.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.test.core.ut.BaseRedisUnitTest; | |
| 4 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodePartDO; | |
| 5 | +import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodeRuleDO; | |
| 6 | +import cn.iocoder.yudao.module.mes.dal.redis.md.autocode.MesMdAutoCodeRedisDAO; | |
| 7 | +import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeCycleMethodEnum; | |
| 8 | +import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodeContext; | |
| 9 | +import jakarta.annotation.Resource; | |
| 10 | +import org.junit.jupiter.api.Test; | |
| 11 | +import org.springframework.context.annotation.Import; | |
| 12 | + | |
| 13 | +import static org.junit.jupiter.api.Assertions.*; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * {@link MesMdAutoCodeSerialNumberPartStrategy} 的单元测试 | |
| 17 | + * | |
| 18 | + * @author 芋道源码 | |
| 19 | + */ | |
| 20 | +@Import({MesMdAutoCodeSerialNumberPartStrategy.class, MesMdAutoCodeRedisDAO.class}) | |
| 21 | +public class MesMdAutoCodeSerialNumberPartStrategyTest extends BaseRedisUnitTest { | |
| 22 | + | |
| 23 | + @Resource | |
| 24 | + private MesMdAutoCodeSerialNumberPartStrategy strategy; | |
| 25 | + | |
| 26 | + @Test | |
| 27 | + public void testGenerate_noCycle() { | |
| 28 | + // 准备参数 | |
| 29 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setLength(4).setSerialStartNo(1).setSerialStep(1).setCycleFlag(false); | |
| 30 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(1L); | |
| 31 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setRule(rule); | |
| 32 | + | |
| 33 | + // 调用 | |
| 34 | + String result = strategy.generate(part, context); | |
| 35 | + // 断言 | |
| 36 | + assertEquals("0001", result); | |
| 37 | + assertEquals(1, context.getSerialNo()); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Test | |
| 41 | + public void testGenerate_withCycleByDay() { | |
| 42 | + // 准备参数 | |
| 43 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setLength(4).setSerialStartNo(1).setSerialStep(1) | |
| 44 | + .setCycleFlag(true).setCycleMethod(MesMdAutoCodeCycleMethodEnum.DAY.getMethod()); | |
| 45 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(2L); | |
| 46 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setRule(rule); | |
| 47 | + | |
| 48 | + // 调用 | |
| 49 | + String result = strategy.generate(part, context); | |
| 50 | + // 断言 | |
| 51 | + assertEquals("0001", result); | |
| 52 | + assertEquals(1, context.getSerialNo()); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Test | |
| 56 | + public void testGenerate_withCycleByInputChar() { | |
| 57 | + // 准备参数 | |
| 58 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setLength(4).setSerialStartNo(1).setSerialStep(1) | |
| 59 | + .setCycleFlag(true).setCycleMethod(MesMdAutoCodeCycleMethodEnum.INPUT_CHAR.getMethod()); | |
| 60 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(3L); | |
| 61 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setRule(rule).setInputChar("A"); | |
| 62 | + | |
| 63 | + // 调用 | |
| 64 | + String result = strategy.generate(part, context); | |
| 65 | + // 断言 | |
| 66 | + assertEquals("0001", result); | |
| 67 | + assertEquals(1, context.getSerialNo()); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Test | |
| 71 | + public void testGenerate_withStep() { | |
| 72 | + // 准备参数 | |
| 73 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setLength(4).setSerialStartNo(1).setSerialStep(5).setCycleFlag(false); | |
| 74 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(4L); | |
| 75 | + MesMdAutoCodeContext context = new MesMdAutoCodeContext().setRule(rule); | |
| 76 | + | |
| 77 | + // 调用 | |
| 78 | + String result = strategy.generate(part, context); | |
| 79 | + // 断言 | |
| 80 | + assertEquals("0005", result); | |
| 81 | + assertEquals(5, context.getSerialNo()); | |
| 82 | + } | |
| 83 | + | |
| 84 | + @Test | |
| 85 | + public void testGenerate_multipleCallsIncrement() { | |
| 86 | + // 准备参数 | |
| 87 | + MesMdAutoCodePartDO part = new MesMdAutoCodePartDO().setLength(4).setSerialStartNo(1).setSerialStep(1).setCycleFlag(false); | |
| 88 | + MesMdAutoCodeRuleDO rule = new MesMdAutoCodeRuleDO().setId(5L); | |
| 89 | + MesMdAutoCodeContext context1 = new MesMdAutoCodeContext().setRule(rule); | |
| 90 | + MesMdAutoCodeContext context2 = new MesMdAutoCodeContext().setRule(rule); | |
| 91 | + | |
| 92 | + // 调用 | |
| 93 | + String result1 = strategy.generate(part, context1); | |
| 94 | + String result2 = strategy.generate(part, context2); | |
| 95 | + // 断言 | |
| 96 | + assertEquals("0001", result1); | |
| 97 | + assertEquals("0002", result2); | |
| 98 | + } | |
| 99 | + | |
| 100 | +} | ... | ... |
yudao-module-mes/src/test/resources/application-unit-test.yaml
0 → 100644
| 1 | +spring: | |
| 2 | + main: | |
| 3 | + lazy-initialization: true # 开启懒加载,加快速度 | |
| 4 | + banner-mode: off # 单元测试,禁用 Banner | |
| 5 | + allow-bean-definition-overriding: true # 允许 Bean 覆盖,解决 Redis 配置冲突 | |
| 6 | + | |
| 7 | +--- #################### 数据库相关配置 #################### | |
| 8 | + | |
| 9 | +spring: | |
| 10 | + # 数据源配置项 | |
| 11 | + datasource: | |
| 12 | + name: ruoyi-vue-pro | |
| 13 | + url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false;NON_KEYWORDS=value; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写 | |
| 14 | + driver-class-name: org.h2.Driver | |
| 15 | + username: sa | |
| 16 | + password: | |
| 17 | + druid: | |
| 18 | + async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 | |
| 19 | + initial-size: 1 # 单元测试,配置为 1,提升启动速度 | |
| 20 | + sql: | |
| 21 | + init: | |
| 22 | + schema-locations: classpath:/sql/create_tables.sql | |
| 23 | + | |
| 24 | + # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 | |
| 25 | + data: | |
| 26 | + redis: | |
| 27 | + host: 127.0.0.1 # 地址 | |
| 28 | + port: 16379 # 端口(单元测试,使用 16379 端口) | |
| 29 | + database: 0 # 数据库索引 | |
| 30 | + | |
| 31 | +mybatis: | |
| 32 | + lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试 | |
| 33 | + | |
| 34 | +--- #################### 定时任务相关配置 #################### | |
| 35 | + | |
| 36 | +--- #################### 配置中心相关配置 #################### | |
| 37 | + | |
| 38 | +--- #################### 服务保障相关配置 #################### | |
| 39 | + | |
| 40 | +# Lock4j 配置项(单元测试,禁用 Lock4j) | |
| 41 | + | |
| 42 | +--- #################### 监控相关配置 #################### | |
| 43 | + | |
| 44 | +--- #################### 芋道相关配置 #################### | |
| 45 | + | |
| 46 | +# 芋道配置项,设置当前项目所有自定义的配置 | |
| 47 | +yudao: | |
| 48 | + info: | |
| 49 | + base-package: cn.iocoder.yudao.module | ... | ... |
yudao-module-mes/src/test/resources/logback.xml
0 → 100644
yudao-module-mes/src/test/resources/sql/clean.sql
0 → 100644
yudao-module-mes/src/test/resources/sql/create_tables.sql
0 → 100644
| 1 | +-- ---------------------------- | |
| 2 | +-- MES 编码规则表 | |
| 3 | +-- ---------------------------- | |
| 4 | +CREATE TABLE IF NOT EXISTS "mes_md_auto_code_rule" ( | |
| 5 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 6 | + "code" varchar(64) NOT NULL, | |
| 7 | + "name" varchar(64) NOT NULL, | |
| 8 | + "description" varchar(255) DEFAULT NULL, | |
| 9 | + "max_length" int DEFAULT NULL, | |
| 10 | + "padded" bit NOT NULL DEFAULT FALSE, | |
| 11 | + "padded_char" varchar(1) DEFAULT NULL, | |
| 12 | + "padded_method" tinyint DEFAULT NULL, | |
| 13 | + "status" tinyint NOT NULL, | |
| 14 | + "remark" varchar(500) DEFAULT NULL, | |
| 15 | + "creator" varchar(64) DEFAULT '', | |
| 16 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 17 | + "updater" varchar(64) DEFAULT '', | |
| 18 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 19 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 20 | + "tenant_id" bigint NOT NULL DEFAULT 0, | |
| 21 | + PRIMARY KEY ("id") | |
| 22 | +); | |
| 23 | + | |
| 24 | +-- ---------------------------- | |
| 25 | +-- MES 编码规则组成表 | |
| 26 | +-- ---------------------------- | |
| 27 | +CREATE TABLE IF NOT EXISTS "mes_md_auto_code_part" ( | |
| 28 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 29 | + "rule_id" bigint NOT NULL, | |
| 30 | + "sort" int NOT NULL, | |
| 31 | + "type" tinyint NOT NULL, | |
| 32 | + "length" int NOT NULL, | |
| 33 | + "date_format" varchar(20) DEFAULT NULL, | |
| 34 | + "fix_character" varchar(64) DEFAULT NULL, | |
| 35 | + "serial_start_no" int DEFAULT NULL, | |
| 36 | + "serial_step" int DEFAULT NULL, | |
| 37 | + "cycle_flag" bit DEFAULT FALSE, | |
| 38 | + "cycle_method" tinyint DEFAULT NULL, | |
| 39 | + "remark" varchar(500) DEFAULT NULL, | |
| 40 | + "creator" varchar(64) DEFAULT '', | |
| 41 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 42 | + "updater" varchar(64) DEFAULT '', | |
| 43 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 44 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 45 | + "tenant_id" bigint NOT NULL DEFAULT 0, | |
| 46 | + PRIMARY KEY ("id") | |
| 47 | +); | |
| 48 | + | |
| 49 | +-- ---------------------------- | |
| 50 | +-- MES 编码生成记录表 | |
| 51 | +-- ---------------------------- | |
| 52 | +CREATE TABLE IF NOT EXISTS "mes_md_auto_code_record" ( | |
| 53 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 54 | + "rule_id" bigint NOT NULL, | |
| 55 | + "result" varchar(64) DEFAULT NULL, | |
| 56 | + "serial_no" int DEFAULT NULL, | |
| 57 | + "input_char" varchar(64) DEFAULT NULL, | |
| 58 | + "creator" varchar(64) DEFAULT '', | |
| 59 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 60 | + "updater" varchar(64) DEFAULT '', | |
| 61 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 62 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 63 | + "tenant_id" bigint NOT NULL DEFAULT 0, | |
| 64 | + PRIMARY KEY ("id") | |
| 65 | +); | ... | ... |