增加 dict 模块的单测覆盖率

This commit is contained in:
YunaiV 2023-12-03 21:15:06 +08:00
parent 30fef09eee
commit dab751dc74
8 changed files with 29 additions and 31 deletions

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.system.controller.admin.dict;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -66,7 +67,8 @@ public class DictDataController {
@Operation(summary = "获得全部字典数据列表", description = "一般用于管理后台缓存字典数据在本地")
// 无需添加权限认证因为前端全局都需要
public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDataList() {
List<DictDataDO> list = dictDataService.getDictDataList();
List<DictDataDO> list = dictDataService.getDictDataList(
CommonStatusEnum.ENABLE.getStatus(), null);
return success(BeanUtils.toBean(list, DictDataSimpleRespVO.class));
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.system.controller.app.dict;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.app.dict.vo.AppDictDataRespVO;
@ -32,7 +33,8 @@ public class AppDictDataController {
@Operation(summary = "根据字典类型查询字典数据信息")
@Parameter(name = "type", description = "字典类型", required = true, example = "common_status")
public CommonResult<List<AppDictDataRespVO>> getDictDataListByType(@RequestParam("type") String type) {
List<DictDataDO> list = dictDataService.getEnabledDictDataListByType(type);
List<DictDataDO> list = dictDataService.getDictDataList(
CommonStatusEnum.ENABLE.getStatus(), type);
return success(BeanUtils.toBean(list, AppDictDataRespVO.class));
}

View File

@ -40,10 +40,10 @@ public interface DictDataMapper extends BaseMapperX<DictDataDO> {
.orderByDesc(Arrays.asList(DictDataDO::getDictType, DictDataDO::getSort)));
}
default List<DictDataDO> selectListByTypeAndStatus(String dictType, Integer status) {
return selectList(new LambdaQueryWrapper<DictDataDO>()
.eq(DictDataDO::getDictType, dictType)
.eq(DictDataDO::getStatus, status));
default List<DictDataDO> selectListByStatusAndDictType(Integer status, String dictType) {
return selectList(new LambdaQueryWrapperX<DictDataDO>()
.eqIfPresent(DictDataDO::getStatus, status)
.eqIfPresent(DictDataDO::getDictType, dictType));
}
}

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import org.springframework.lang.Nullable;
import java.util.Collection;
import java.util.List;
@ -40,9 +41,11 @@ public interface DictDataService {
/**
* 获得字典数据列表
*
* @param status 状态
* @param dictType 字典类型
* @return 字典数据全列表
*/
List<DictDataDO> getDictDataList();
List<DictDataDO> getDictDataList(@Nullable Integer status, @Nullable String dictType);
/**
* 获得字典数据分页列表
@ -52,14 +55,6 @@ public interface DictDataService {
*/
PageResult<DictDataDO> getDictDataPage(DictDataPageReqVO pageReqVO);
/**
* 获得字典数据列表
*
* @param dictType 字典类型
* @return 字典数据列表
*/
List<DictDataDO> getEnabledDictDataListByType(String dictType);
/**
* 获得字典数据详情
*
@ -74,7 +69,7 @@ public interface DictDataService {
* @param dictType 字典类型
* @return 数据数量
*/
long countByDictType(String dictType);
long getDictDataCountByDictType(String dictType);
/**
* 校验字典数据们是否有效如下情况视为无效
@ -103,4 +98,5 @@ public interface DictDataService {
* @return 字典数据
*/
DictDataDO parseDictData(String dictType, String label);
}

View File

@ -46,8 +46,8 @@ public class DictDataServiceImpl implements DictDataService {
private DictDataMapper dictDataMapper;
@Override
public List<DictDataDO> getDictDataList() {
List<DictDataDO> list = dictDataMapper.selectList(DictDataDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
public List<DictDataDO> getDictDataList(Integer status, String dictType) {
List<DictDataDO> list = dictDataMapper.selectListByStatusAndDictType(status, dictType);
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
}
@ -57,13 +57,6 @@ public class DictDataServiceImpl implements DictDataService {
return dictDataMapper.selectPage(pageReqVO);
}
@Override
public List<DictDataDO> getEnabledDictDataListByType(String dictType) {
List<DictDataDO> list = dictDataMapper.selectListByTypeAndStatus(dictType, CommonStatusEnum.ENABLE.getStatus());
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
}
@Override
public DictDataDO getDictData(Long id) {
return dictDataMapper.selectById(id);
@ -106,7 +99,7 @@ public class DictDataServiceImpl implements DictDataService {
}
@Override
public long countByDictType(String dictType) {
public long getDictDataCountByDictType(String dictType) {
return dictDataMapper.selectCountByDictType(dictType);
}

View File

@ -80,7 +80,7 @@ public class DictTypeServiceImpl implements DictTypeService {
// 校验是否存在
DictTypeDO dictType = validateDictTypeExists(id);
// 校验是否有字典数据
if (dictDataService.countByDictType(dictType.getType()) > 0) {
if (dictDataService.getDictDataCountByDictType(dictType.getType()) > 0) {
throw exception(DICT_TYPE_HAS_CHILDREN);
}
// 删除字典类型

View File

@ -50,10 +50,15 @@ public class DictDataServiceImplTest extends BaseDbUnitTest {
DictDataDO dictDataDO03 = randomDictDataDO().setDictType("yunai").setSort(3)
.setStatus(CommonStatusEnum.DISABLE.getStatus());
dictDataMapper.insert(dictDataDO03);
DictDataDO dictDataDO04 = randomDictDataDO().setDictType("yunai2").setSort(3)
.setStatus(CommonStatusEnum.DISABLE.getStatus());
dictDataMapper.insert(dictDataDO04);
// 准备参数
Integer status = CommonStatusEnum.ENABLE.getStatus();
String dictType = "yunai";
// 调用
List<DictDataDO> dictDataDOList = dictDataService.getDictDataList();
List<DictDataDO> dictDataDOList = dictDataService.getDictDataList(status, dictType);
// 断言
assertEquals(2, dictDataDOList.size());
assertPojoEquals(dictDataDO02, dictDataDOList.get(0));
@ -236,7 +241,7 @@ public class DictDataServiceImplTest extends BaseDbUnitTest {
}
@Test
public void testCountByDictType() {
public void testGetDictDataCountByDictType() {
// mock 数据
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai")));
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("tudou")));
@ -245,7 +250,7 @@ public class DictDataServiceImplTest extends BaseDbUnitTest {
String dictType = "yunai";
// 调用
long count = dictDataService.countByDictType(dictType);
long count = dictDataService.getDictDataCountByDictType(dictType);
// 校验
assertEquals(2L, count);
}

View File

@ -158,7 +158,7 @@ public class DictTypeServiceImplTest extends BaseDbUnitTest {
// 准备参数
Long id = dbDictType.getId();
// mock 方法
when(dictDataService.countByDictType(eq(dbDictType.getType()))).thenReturn(1L);
when(dictDataService.getDictDataCountByDictType(eq(dbDictType.getType()))).thenReturn(1L);
// 调用, 并断言异常
assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_HAS_CHILDREN);