Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
0a2fbae9
提交
0a2fbae9
authored
1月 31, 2023
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完善 DictTypeServiceImpl 单元测试
上级
30f097ed
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
103 行增加
和
69 行删除
+103
-69
DictTypeServiceImpl.java
...yudao/module/system/service/dict/DictTypeServiceImpl.java
+12
-10
DictTypeServiceImplTest.java
...o/module/system/service/dict/DictTypeServiceImplTest.java
+91
-59
没有找到文件。
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceImpl.java
浏览文件 @
0a2fbae9
...
...
@@ -57,7 +57,8 @@ public class DictTypeServiceImpl implements DictTypeService {
@Override
public
Long
createDictType
(
DictTypeCreateReqVO
reqVO
)
{
// 校验正确性
checkCreateOrUpdate
(
null
,
reqVO
.
getName
(),
reqVO
.
getType
());
validateDictTypeForCreateOrUpdate
(
null
,
reqVO
.
getName
(),
reqVO
.
getType
());
// 插入字典类型
DictTypeDO
dictType
=
DictTypeConvert
.
INSTANCE
.
convert
(
reqVO
)
.
setDeletedTime
(
LocalDateTimeUtils
.
EMPTY
);
// 唯一索引,避免 null 值
...
...
@@ -68,7 +69,8 @@ public class DictTypeServiceImpl implements DictTypeService {
@Override
public
void
updateDictType
(
DictTypeUpdateReqVO
reqVO
)
{
// 校验正确性
checkCreateOrUpdate
(
reqVO
.
getId
(),
reqVO
.
getName
(),
null
);
validateDictTypeForCreateOrUpdate
(
reqVO
.
getId
(),
reqVO
.
getName
(),
null
);
// 更新字典类型
DictTypeDO
updateObj
=
DictTypeConvert
.
INSTANCE
.
convert
(
reqVO
);
dictTypeMapper
.
updateById
(
updateObj
);
...
...
@@ -77,7 +79,7 @@ public class DictTypeServiceImpl implements DictTypeService {
@Override
public
void
deleteDictType
(
Long
id
)
{
// 校验是否存在
DictTypeDO
dictType
=
check
DictTypeExists
(
id
);
DictTypeDO
dictType
=
validate
DictTypeExists
(
id
);
// 校验是否有字典数据
if
(
dictDataService
.
countByDictType
(
dictType
.
getType
())
>
0
)
{
throw
exception
(
DICT_TYPE_HAS_CHILDREN
);
...
...
@@ -91,17 +93,17 @@ public class DictTypeServiceImpl implements DictTypeService {
return
dictTypeMapper
.
selectList
();
}
private
void
check
CreateOrUpdate
(
Long
id
,
String
name
,
String
type
)
{
private
void
validateDictTypeFor
CreateOrUpdate
(
Long
id
,
String
name
,
String
type
)
{
// 校验自己存在
check
DictTypeExists
(
id
);
validate
DictTypeExists
(
id
);
// 校验字典类型的名字的唯一性
check
DictTypeNameUnique
(
id
,
name
);
validate
DictTypeNameUnique
(
id
,
name
);
// 校验字典类型的类型的唯一性
check
DictTypeUnique
(
id
,
type
);
validate
DictTypeUnique
(
id
,
type
);
}
@VisibleForTesting
public
void
check
DictTypeNameUnique
(
Long
id
,
String
name
)
{
void
validate
DictTypeNameUnique
(
Long
id
,
String
name
)
{
DictTypeDO
dictType
=
dictTypeMapper
.
selectByName
(
name
);
if
(
dictType
==
null
)
{
return
;
...
...
@@ -116,7 +118,7 @@ public class DictTypeServiceImpl implements DictTypeService {
}
@VisibleForTesting
public
void
check
DictTypeUnique
(
Long
id
,
String
type
)
{
void
validate
DictTypeUnique
(
Long
id
,
String
type
)
{
if
(
StrUtil
.
isEmpty
(
type
))
{
return
;
}
...
...
@@ -134,7 +136,7 @@ public class DictTypeServiceImpl implements DictTypeService {
}
@VisibleForTesting
public
DictTypeDO
check
DictTypeExists
(
Long
id
)
{
DictTypeDO
validate
DictTypeExists
(
Long
id
)
{
if
(
id
==
null
)
{
return
null
;
}
...
...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dict/DictTypeServiceTest.java
→
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dict/DictTypeService
Impl
Test.java
浏览文件 @
0a2fbae9
...
...
@@ -2,36 +2,36 @@ package cn.iocoder.yudao.module.system.service.dict;
import
cn.iocoder.yudao.framework.common.enums.CommonStatusEnum
;
import
cn.iocoder.yudao.framework.common.pojo.PageResult
;
import
cn.iocoder.yudao.framework.common.util.collection.ArrayUtils
;
import
cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest
;
import
cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO
;
import
cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO
;
import
cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO
;
import
cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO
;
import
cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO
;
import
cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO
;
import
cn.iocoder.yudao.module.system.dal.mysql.dict.DictTypeMapper
;
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
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.context.annotation.Import
;
import
javax.annotation.Resource
;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
java.util.function.Consumer
;
import
static
cn
.
hutool
.
core
.
util
.
RandomUtil
.
randomEle
;
import
static
cn
.
iocoder
.
yudao
.
module
.
system
.
enums
.
ErrorCodeConstants
.*;
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
.
common
.
util
.
date
.
DateUtils
.
buildLocalDateTime
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
RandomUtils
.*;
import
static
cn
.
iocoder
.
yudao
.
module
.
system
.
enums
.
ErrorCodeConstants
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
when
;
@Import
(
DictTypeServiceImpl
.
class
)
public
class
DictTypeServiceTest
extends
BaseDbUnitTest
{
public
class
DictTypeService
Impl
Test
extends
BaseDbUnitTest
{
@Resource
private
DictTypeServiceImpl
dictTypeService
;
...
...
@@ -43,71 +43,86 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
@Test
public
void
testGetDictTypePage
()
{
// mock 数据
DictTypeDO
dbDictType
=
randomPojo
(
DictTypeDO
.
class
,
o
->
{
// 等会查询到
o
.
setName
(
"yunai"
);
o
.
setType
(
"芋艿"
);
o
.
setStatus
(
CommonStatusEnum
.
ENABLE
.
getStatus
());
o
.
setCreateTime
(
buildTime
(
2021
,
1
,
15
));
});
dictTypeMapper
.
insert
(
dbDictType
);
// 测试 name 不匹配
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setName
(
"tudou"
)));
// 测试 type 不匹配
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setType
(
"土豆"
)));
// 测试 status 不匹配
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setStatus
(
CommonStatusEnum
.
DISABLE
.
getStatus
())));
// 测试 createTime 不匹配
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setCreateTime
(
buildTime
(
2021
,
1
,
1
))));
// 准备参数
DictTypePageReqVO
reqVO
=
new
DictTypePageReqVO
();
reqVO
.
setName
(
"nai"
);
reqVO
.
setType
(
"艿"
);
reqVO
.
setStatus
(
CommonStatusEnum
.
ENABLE
.
getStatus
());
reqVO
.
setCreateTime
(
buildBetweenTime
(
2021
,
1
,
10
,
2021
,
1
,
20
));
// 调用
PageResult
<
DictTypeDO
>
pageResult
=
dictTypeService
.
getDictTypePage
(
reqVO
);
// 断言
assertEquals
(
1
,
pageResult
.
getTotal
());
assertEquals
(
1
,
pageResult
.
getList
().
size
());
assertPojoEquals
(
dbDictType
,
pageResult
.
getList
().
get
(
0
));
}
@Test
public
void
testGetDictTypeList_export
()
{
// mock 数据
DictTypeDO
dbDictType
=
randomPojo
(
DictTypeDO
.
class
,
o
->
{
// 等会查询到
o
.
setName
(
"yunai"
);
o
.
setType
(
"芋艿"
);
o
.
setStatus
(
CommonStatusEnum
.
ENABLE
.
getStatus
());
o
.
setCreateTime
(
build
LocalDate
Time
(
2021
,
1
,
15
));
o
.
setCreateTime
(
buildTime
(
2021
,
1
,
15
));
});
dictTypeMapper
.
insert
(
dbDictType
);
// 测试 name 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setName
(
"tudou"
)));
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setName
(
"tudou"
)));
// 测试 type 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setType
(
"土豆"
)));
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setType
(
"土豆"
)));
// 测试 status 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setStatus
(
CommonStatusEnum
.
DISABLE
.
getStatus
())));
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setStatus
(
CommonStatusEnum
.
DISABLE
.
getStatus
())));
// 测试 createTime 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setCreateTime
(
buildLocalDate
Time
(
2021
,
1
,
1
))));
dictTypeMapper
.
insert
(
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setCreateTime
(
build
Time
(
2021
,
1
,
1
))));
// 准备参数
DictType
PageReqVO
reqVO
=
new
DictTypePage
ReqVO
();
DictType
ExportReqVO
reqVO
=
new
DictTypeExport
ReqVO
();
reqVO
.
setName
(
"nai"
);
reqVO
.
setType
(
"艿"
);
reqVO
.
setStatus
(
CommonStatusEnum
.
ENABLE
.
getStatus
());
reqVO
.
setCreateTime
(
(
new
LocalDateTime
[]{
buildLocalDateTime
(
2021
,
1
,
10
),
buildLocalDateTime
(
2021
,
1
,
20
)}
));
reqVO
.
setCreateTime
(
buildBetweenTime
(
2021
,
1
,
10
,
2021
,
1
,
20
));
// 调用
PageResult
<
DictTypeDO
>
pageResult
=
dictTypeService
.
getDictTypePage
(
reqVO
);
List
<
DictTypeDO
>
list
=
dictTypeService
.
getDictTypeList
(
reqVO
);
// 断言
assertEquals
(
1
,
pageResult
.
getTotal
());
assertEquals
(
1
,
pageResult
.
getList
().
size
());
assertPojoEquals
(
dbDictType
,
pageResult
.
getList
().
get
(
0
));
assertEquals
(
1
,
list
.
size
());
assertPojoEquals
(
dbDictType
,
list
.
get
(
0
));
}
@Test
public
void
testGetDictType
List
()
{
public
void
testGetDictType
_id
()
{
// mock 数据
DictTypeDO
dbDictType
=
randomPojo
(
DictTypeDO
.
class
,
o
->
{
// 等会查询到
o
.
setName
(
"yunai"
);
o
.
setType
(
"芋艿"
);
o
.
setStatus
(
CommonStatusEnum
.
ENABLE
.
getStatus
());
o
.
setCreateTime
(
buildLocalDateTime
(
2021
,
1
,
15
));
});
DictTypeDO
dbDictType
=
randomDictTypeDO
();
dictTypeMapper
.
insert
(
dbDictType
);
// 测试 name 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setName
(
"tudou"
)));
// 测试 type 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setType
(
"土豆"
)));
// 测试 status 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setStatus
(
CommonStatusEnum
.
DISABLE
.
getStatus
())));
// 测试 createTime 不匹配
dictTypeMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
dbDictType
,
o
->
o
.
setCreateTime
(
buildLocalDateTime
(
2021
,
1
,
1
))));
// 准备参数
DictTypeExportReqVO
reqVO
=
new
DictTypeExportReqVO
();
reqVO
.
setName
(
"nai"
);
reqVO
.
setType
(
"艿"
);
reqVO
.
setStatus
(
CommonStatusEnum
.
ENABLE
.
getStatus
());
reqVO
.
setCreateTime
((
new
LocalDateTime
[]{
buildLocalDateTime
(
2021
,
1
,
10
),
buildLocalDateTime
(
2021
,
1
,
20
)}));
Long
id
=
dbDictType
.
getId
();
// 调用
List
<
DictTypeDO
>
list
=
dictTypeService
.
getDictTypeList
(
reqVO
);
DictTypeDO
dictType
=
dictTypeService
.
getDictType
(
id
);
// 断言
assert
Equals
(
1
,
list
.
size
()
);
assertPojoEquals
(
dbDictType
,
list
.
get
(
0
)
);
assert
NotNull
(
dictType
);
assertPojoEquals
(
dbDictType
,
dictType
);
}
@Test
public
void
testGetDictType
()
{
public
void
testGetDictType
_type
()
{
// mock 数据
DictTypeDO
dbDictType
=
randomDictTypeDO
();
dictTypeMapper
.
insert
(
dbDictType
);
...
...
@@ -183,40 +198,57 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
}
@Test
public
void
testCheckDictDataExists_success
()
{
public
void
testGetDictTypeList
()
{
// 准备参数
DictTypeDO
dictTypeDO01
=
randomDictTypeDO
();
dictTypeMapper
.
insert
(
dictTypeDO01
);
DictTypeDO
dictTypeDO02
=
randomDictTypeDO
();
dictTypeMapper
.
insert
(
dictTypeDO02
);
// mock 方法
// 调用
List
<
DictTypeDO
>
dictTypeDOList
=
dictTypeService
.
getDictTypeList
();
// 断言
assertEquals
(
2
,
dictTypeDOList
.
size
());
assertPojoEquals
(
dictTypeDO01
,
dictTypeDOList
.
get
(
0
));
assertPojoEquals
(
dictTypeDO02
,
dictTypeDOList
.
get
(
1
));
}
@Test
public
void
testValidateDictDataExists_success
()
{
// mock 数据
DictTypeDO
dbDictType
=
randomDictTypeDO
();
dictTypeMapper
.
insert
(
dbDictType
);
// @Sql: 先插入出一条存在的数据
// 调用成功
dictTypeService
.
check
DictTypeExists
(
dbDictType
.
getId
());
dictTypeService
.
validate
DictTypeExists
(
dbDictType
.
getId
());
}
@Test
public
void
test
Check
DictDataExists_notExists
()
{
assertServiceException
(()
->
dictTypeService
.
check
DictTypeExists
(
randomLongId
()),
DICT_TYPE_NOT_EXISTS
);
public
void
test
Validate
DictDataExists_notExists
()
{
assertServiceException
(()
->
dictTypeService
.
validate
DictTypeExists
(
randomLongId
()),
DICT_TYPE_NOT_EXISTS
);
}
@Test
public
void
test
Check
DictTypeUnique_success
()
{
public
void
test
Validate
DictTypeUnique_success
()
{
// 调用,成功
dictTypeService
.
check
DictTypeUnique
(
randomLongId
(),
randomString
());
dictTypeService
.
validate
DictTypeUnique
(
randomLongId
(),
randomString
());
}
@Test
public
void
test
Check
DictTypeUnique_valueDuplicateForCreate
()
{
public
void
test
Validate
DictTypeUnique_valueDuplicateForCreate
()
{
// 准备参数
String
type
=
randomString
();
// mock 数据
dictTypeMapper
.
insert
(
randomDictTypeDO
(
o
->
o
.
setType
(
type
)));
// 调用,校验异常
assertServiceException
(()
->
dictTypeService
.
check
DictTypeUnique
(
null
,
type
),
assertServiceException
(()
->
dictTypeService
.
validate
DictTypeUnique
(
null
,
type
),
DICT_TYPE_TYPE_DUPLICATE
);
}
@Test
public
void
test
Check
DictTypeUnique_valueDuplicateForUpdate
()
{
public
void
test
Validate
DictTypeUnique_valueDuplicateForUpdate
()
{
// 准备参数
Long
id
=
randomLongId
();
String
type
=
randomString
();
...
...
@@ -224,30 +256,30 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
dictTypeMapper
.
insert
(
randomDictTypeDO
(
o
->
o
.
setType
(
type
)));
// 调用,校验异常
assertServiceException
(()
->
dictTypeService
.
check
DictTypeUnique
(
id
,
type
),
assertServiceException
(()
->
dictTypeService
.
validate
DictTypeUnique
(
id
,
type
),
DICT_TYPE_TYPE_DUPLICATE
);
}
@Test
public
void
test
Check
DictTypNameUnique_success
()
{
public
void
test
Validate
DictTypNameUnique_success
()
{
// 调用,成功
dictTypeService
.
check
DictTypeNameUnique
(
randomLongId
(),
randomString
());
dictTypeService
.
validate
DictTypeNameUnique
(
randomLongId
(),
randomString
());
}
@Test
public
void
test
Check
DictTypeNameUnique_nameDuplicateForCreate
()
{
public
void
test
Validate
DictTypeNameUnique_nameDuplicateForCreate
()
{
// 准备参数
String
name
=
randomString
();
// mock 数据
dictTypeMapper
.
insert
(
randomDictTypeDO
(
o
->
o
.
setName
(
name
)));
// 调用,校验异常
assertServiceException
(()
->
dictTypeService
.
check
DictTypeNameUnique
(
null
,
name
),
assertServiceException
(()
->
dictTypeService
.
validate
DictTypeNameUnique
(
null
,
name
),
DICT_TYPE_NAME_DUPLICATE
);
}
@Test
public
void
test
Check
DictTypeNameUnique_nameDuplicateForUpdate
()
{
public
void
test
Validate
DictTypeNameUnique_nameDuplicateForUpdate
()
{
// 准备参数
Long
id
=
randomLongId
();
String
name
=
randomString
();
...
...
@@ -255,7 +287,7 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
dictTypeMapper
.
insert
(
randomDictTypeDO
(
o
->
o
.
setName
(
name
)));
// 调用,校验异常
assertServiceException
(()
->
dictTypeService
.
check
DictTypeNameUnique
(
id
,
name
),
assertServiceException
(()
->
dictTypeService
.
validate
DictTypeNameUnique
(
id
,
name
),
DICT_TYPE_NAME_DUPLICATE
);
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论