完善 ErrorCodeServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-01 08:04:26 +08:00
parent 0a2fbae9d8
commit 589aaab298
6 changed files with 96 additions and 68 deletions

View File

@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@ -19,9 +18,7 @@ public interface DeptMapper extends BaseMapperX<DeptDO> {
}
default DeptDO selectByParentIdAndName(Long parentId, String name) {
return selectOne(new LambdaQueryWrapper<DeptDO>()
.eq(DeptDO::getParentId, parentId)
.eq(DeptDO::getName, name));
return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name);
}
default Long selectCountByParentId(Long parentId) {

View File

@ -2,11 +2,10 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
@ -16,31 +15,32 @@ import java.util.List;
public interface PostMapper extends BaseMapperX<PostDO> {
default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses) {
return selectList(new QueryWrapperX<PostDO>().inIfPresent("id", ids)
.inIfPresent("status", statuses));
return selectList(new LambdaQueryWrapperX<PostDO>()
.inIfPresent(PostDO::getId, ids)
.inIfPresent(PostDO::getStatus, statuses));
}
default PageResult<PostDO> selectPage(PostPageReqVO reqVO) {
return selectPage(reqVO, new QueryWrapperX<PostDO>()
.likeIfPresent("code", reqVO.getCode())
.likeIfPresent("name", reqVO.getName())
.eqIfPresent("status", reqVO.getStatus())
.orderByDesc("id"));
return selectPage(reqVO, new LambdaQueryWrapperX<PostDO>()
.likeIfPresent(PostDO::getCode, reqVO.getCode())
.likeIfPresent(PostDO::getName, reqVO.getName())
.eqIfPresent(PostDO::getStatus, reqVO.getStatus())
.orderByDesc(PostDO::getId));
}
default List<PostDO> selectList(PostExportReqVO reqVO) {
return selectList(new QueryWrapperX<PostDO>()
.likeIfPresent("code", reqVO.getCode())
.likeIfPresent("name", reqVO.getName())
.eqIfPresent("status", reqVO.getStatus()));
return selectList(new LambdaQueryWrapperX<PostDO>()
.likeIfPresent(PostDO::getCode, reqVO.getCode())
.likeIfPresent(PostDO::getName, reqVO.getName())
.eqIfPresent(PostDO::getStatus, reqVO.getStatus()));
}
default PostDO selectByName(String name) {
return selectOne(new QueryWrapper<PostDO>().eq("name", name));
return selectOne(PostDO::getName, name);
}
default PostDO selectByCode(String code) {
return selectOne(new QueryWrapper<PostDO>().eq("code", code));
return selectOne(PostDO::getCode, code);
}
}

View File

@ -28,7 +28,7 @@ public interface UserPostMapper extends BaseMapperX<UserPostDO> {
.in(UserPostDO::getPostId, postIds));
}
default void deleteByUserId(Long userId){
default void deleteByUserId(Long userId) {
delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
}
}

View File

@ -17,13 +17,11 @@ import java.util.List;
public interface DictDataMapper extends BaseMapperX<DictDataDO> {
default DictDataDO selectByDictTypeAndValue(String dictType, String value) {
return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType)
.eq(DictDataDO::getValue, value));
return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getValue, value);
}
default DictDataDO selectByDictTypeAndLabel(String dictType, String label) {
return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType)
.eq(DictDataDO::getLabel, label));
return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getLabel, label);
}
default List<DictDataDO> selectByDictTypeAndValues(String dictType, Collection<String> values) {

View File

@ -8,8 +8,8 @@ import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePag
import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
@Mapper
@ -36,11 +36,11 @@ public interface ErrorCodeMapper extends BaseMapperX<ErrorCodeDO> {
}
default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) {
return selectList(new LambdaQueryWrapperX<ErrorCodeDO>().in(ErrorCodeDO::getCode, codes));
return selectList(ErrorCodeDO::getCode, codes);
}
default ErrorCodeDO selectByCode(Integer code) {
return selectOne(new LambdaQueryWrapperX<ErrorCodeDO>().eq(ErrorCodeDO::getCode, code));
return selectOne(ErrorCodeDO::getCode, code);
}
default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) {

View File

@ -2,9 +2,9 @@ package cn.iocoder.yudao.module.system.service.errorcode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO;
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO;
@ -22,8 +22,9 @@ import java.util.List;
import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildLocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
@ -58,7 +59,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test
public void testUpdateErrorCode_success() {
// mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO();
ErrorCodeDO dbErrorCode = randomErrorCodeDO();
errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据
// 准备参数
ErrorCodeUpdateReqVO reqVO = randomPojo(ErrorCodeUpdateReqVO.class, o -> {
@ -76,7 +77,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test
public void testDeleteErrorCode_success() {
// mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO();
ErrorCodeDO dbErrorCode = randomErrorCodeDO();
errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbErrorCode.getId();
@ -97,8 +98,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
reqVO.setApplicationName("tu");
reqVO.setCode(1);
reqVO.setMessage("ma");
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2020, 11, 1),
buildLocalDateTime(2020, 11, 30)}));
reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
// 调用
PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO);
@ -112,29 +112,29 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
* 初始化 getErrorCodePage 方法的测试数据
*/
private ErrorCodeDO initGetErrorCodePage() {
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> { // 等会查询到
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> { // 等会查询到
o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
o.setApplicationName("tudou");
o.setCode(1);
o.setMessage("yuanma");
o.setCreateTime(buildLocalDateTime(2020, 11, 11));
o.setCreateTime(buildTime(2020, 11, 11));
});
errorCodeMapper.insert(dbErrorCode);
// 测试 type 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())));
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())));
// 测试 applicationName 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan")));
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan")));
// 测试 code 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCode(2)));
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCode(2)));
// 测试 message 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai")));
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai")));
// 测试 createTime 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildLocalDateTime(2020, 12, 12))));
errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12))));
return dbErrorCode;
}
@Test
public void testGetErrorCodeList() {
public void testGetErrorCodeList_export() {
// mock 数据
ErrorCodeDO dbErrorCode = initGetErrorCodePage();
// 准备参数
@ -143,8 +143,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
reqVO.setApplicationName("tu");
reqVO.setCode(1);
reqVO.setMessage("ma");
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2020, 11, 1),
buildLocalDateTime(2020, 11, 30)}));
reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
// 调用
List<ErrorCodeDO> list = errorCodeService.getErrorCodeList(reqVO);
@ -158,7 +157,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
// 准备参数
Integer code = randomInteger();
// mock 数据
errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code)));
errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code)));
// 调用校验异常
assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null),
@ -171,7 +170,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
Long id = randomLongId();
Integer code = randomInteger();
// mock 数据
errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code)));
errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code)));
// 调用校验异常
assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id),
@ -207,7 +206,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test
public void testAutoGenerateErrorCodes_021() {
// mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()));
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()));
errorCodeMapper.insert(dbErrorCode);
// 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -227,7 +226,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test
public void testAutoGenerateErrorCodes_022() {
// mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
errorCodeMapper.insert(dbErrorCode);
// 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -247,7 +246,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test
public void testAutoGenerateErrorCodes_023() {
// mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
errorCodeMapper.insert(dbErrorCode);
// 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -268,7 +267,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test
public void testAutoGenerateErrorCodes_024() {
// mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
errorCodeMapper.insert(dbErrorCode);
// 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -282,10 +281,44 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
assertPojoEquals(generateReqDTO, errorCode);
}
@Test
public void testGetErrorCode() {
// 准备参数
ErrorCodeDO errorCodeDO = randomErrorCodeDO();
errorCodeMapper.insert(errorCodeDO);
// mock 方法
Long id = errorCodeDO.getId();
// 调用
ErrorCodeDO dbErrorCode = errorCodeService.getErrorCode(id);
// 断言
assertPojoEquals(errorCodeDO, dbErrorCode);
}
@Test
public void testGetErrorCodeList() {
// 准备参数
ErrorCodeDO errorCodeDO01 = randomErrorCodeDO(
o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 10)));
errorCodeMapper.insert(errorCodeDO01);
ErrorCodeDO errorCodeDO02 = randomErrorCodeDO(
o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 12)));
errorCodeMapper.insert(errorCodeDO02);
// mock 方法
String applicationName = "yunai_server";
LocalDateTime minUpdateTime = buildTime(2022, 1, 11);
// 调用
List<ErrorCodeRespDTO> errorCodeList = errorCodeService.getErrorCodeList(applicationName, minUpdateTime);
// 断言
assertEquals(1, errorCodeList.size());
assertPojoEquals(errorCodeDO02, errorCodeList.get(0));
}
// ========== 随机对象 ==========
@SafeVarargs
private static ErrorCodeDO randomInfErrorCodeDO(Consumer<ErrorCodeDO>... consumers) {
private static ErrorCodeDO randomErrorCodeDO(Consumer<ErrorCodeDO>... consumers) {
Consumer<ErrorCodeDO> consumer = (o) -> {
o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围
};