From c5ec798c10f1f0b50d6c95068d0af5f1dd273ef5 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 8 Mar 2021 00:37:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=20dict=20type=20=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=8C=E5=B0=86=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E3=80=81=E4=BF=AE=E6=94=B9=E3=80=81=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=B6=89=E5=8F=8A=E7=9A=84=E5=85=AC=E7=94=A8=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=8D=95=E7=8B=AC=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dict/impl/SysDictTypeServiceImpl.java | 4 +- .../service/dict/SysDictDataServiceTest.java | 5 + .../service/dict/SysDictTypeServiceTest.java | 144 ++++++++++-------- 3 files changed, 87 insertions(+), 66 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictTypeServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictTypeServiceImpl.java index 5c75befaa..c210e8e2b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictTypeServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictTypeServiceImpl.java @@ -99,8 +99,8 @@ public class SysDictTypeServiceImpl implements SysDictTypeService { } @VisibleForTesting - public void checkDictTypeNameUnique(Long id, String type) { - SysDictTypeDO dictType = dictTypeMapper.selectByName(type); + public void checkDictTypeNameUnique(Long id, String name) { + SysDictTypeDO dictType = dictTypeMapper.selectByName(name); if (dictType == null) { return; } diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java index 82a3dafcb..ae029f73b 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java @@ -204,6 +204,11 @@ public class SysDictDataServiceTest extends BaseSpringBootUnitTest { dictDataService.checkDictDataExists(dbDictData.getId()); } + @Test + public void testCheckDictDataExists_notExists() { + assertServiceException(() -> dictDataService.checkDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS); + } + @Test public void testCheckDictTypeValid_success() { // mock 方法,数据类型被禁用 diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java index e25fe2577..05441a707 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java @@ -23,8 +23,8 @@ import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; -import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static cn.iocoder.dashboard.util.RandomUtils.*; +import static cn.iocoder.dashboard.util.RandomUtils.randomString; import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.eq; @@ -142,32 +142,6 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { assertPojoEquals(reqVO, dictType); } - @Test - public void testCreateDictType_nameDuplicate() { - // mock 数据 - SysDictTypeDO dbDictType = randomDictTypeDO(); - dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 - // 准备参数 - SysDictTypeCreateReqVO reqVO = randomPojo(SysDictTypeCreateReqVO.class, - o -> o.setName(dbDictType.getName())); // 模拟 name 重复 - - // 调用, 并断言异常 - assertServiceException(() -> dictTypeService.createDictType(reqVO), DICT_TYPE_NAME_DUPLICATE); - } - - @Test - public void testCreateDictType_typeDuplicate() { - // mock 数据 - SysDictTypeDO dbDictType = randomDictTypeDO(); - dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 - // 准备参数 - SysDictTypeCreateReqVO reqVO = randomPojo(SysDictTypeCreateReqVO.class, - o -> o.setType(dbDictType.getType())); // 模拟 type 重复 - - // 调用, 并断言异常 - assertServiceException(() -> dictTypeService.createDictType(reqVO), DICT_TYPE_TYPE_DUPLICATE); - } - @Test public void testUpdateDictType_success() { // mock 数据 @@ -186,33 +160,6 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { assertPojoEquals(reqVO, dictType); } - @Test - public void testUpdateDictType_notExists() { - // 准备参数 - SysDictTypeUpdateReqVO reqVO = randomPojo(SysDictTypeUpdateReqVO.class); - - // 调用, 并断言异常 - assertServiceException(() -> dictTypeService.updateDictType(reqVO), DICT_TYPE_NOT_EXISTS); - } - - @Test - public void testUpdateDictType_nameDuplicate() { - // mock 数据,稍后更新它 - SysDictTypeDO dbDictType = randomDictTypeDO(); - dictTypeMapper.insert(dbDictType); - // mock 数据,ks稍后模拟重复它的名字 - SysDictTypeDO nameDictType = randomDictTypeDO(); - dictTypeMapper.insert(nameDictType); - // 准备参数 - SysDictTypeUpdateReqVO reqVO = randomPojo(SysDictTypeUpdateReqVO.class, o -> { - o.setId(dbDictType.getId()); // 设置更新的 ID - o.setName(nameDictType.getName()); // 模拟 name 重复 - }); - - // 调用, 并断言异常 - assertServiceException(() -> dictTypeService.updateDictType(reqVO), DICT_TYPE_NAME_DUPLICATE); - } - @Test public void testDeleteDictType_success() { // mock 数据 @@ -227,15 +174,6 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { assertNull(dictTypeMapper.selectById(id)); } - @Test - public void testDeleteDictType_notExists() { - // 准备参数 - Long id = randomLongId(); - - // 调用, 并断言异常 - assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_NOT_EXISTS); - } - @Test public void testDeleteDictType_hasChildren() { // mock 数据 @@ -250,6 +188,84 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_HAS_CHILDREN); } + @Test + public void testCheckDictDataExists_success() { + // mock 数据 + SysDictTypeDO dbDictType = randomDictTypeDO(); + dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 + + // 调用成功 + dictTypeService.checkDictTypeExists(dbDictType.getId()); + } + + @Test + public void testCheckDictDataExists_notExists() { + assertServiceException(() -> dictTypeService.checkDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS); + } + + @Test + public void testCheckDictTypeUnique_success() { + // 调用,成功 + dictTypeService.checkDictTypeUnique(randomLongId(), randomString()); + } + + @Test + public void testCheckDictTypeUnique_valueDuplicateForCreate() { + // 准备参数 + String type = randomString(); + // mock 数据 + dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); + + // 调用,校验异常 + assertServiceException(() -> dictTypeService.checkDictTypeUnique(null, type), + DICT_TYPE_TYPE_DUPLICATE); + } + + @Test + public void testCheckDictTypeUnique_valueDuplicateForUpdate() { + // 准备参数 + Long id = randomLongId(); + String type = randomString(); + // mock 数据 + dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); + + // 调用,校验异常 + assertServiceException(() -> dictTypeService.checkDictTypeUnique(id, type), + DICT_TYPE_TYPE_DUPLICATE); + } + + + @Test + public void testCheckDictTypNameUnique_success() { + // 调用,成功 + dictTypeService.checkDictTypeNameUnique(randomLongId(), randomString()); + } + + @Test + public void testCheckDictTypeNameUnique_valueDuplicateForCreate() { + // 准备参数 + String name = randomString(); + // mock 数据 + dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); + + // 调用,校验异常 + assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(null, name), + DICT_TYPE_NAME_DUPLICATE); + } + + @Test + public void testCheckDictTypeNameUnique_valueDuplicateForUpdate() { + // 准备参数 + Long id = randomLongId(); + String name = randomString(); + // mock 数据 + dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); + + // 调用,校验异常 + assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(id, name), + DICT_TYPE_NAME_DUPLICATE); + } + // ========== 随机对象 ========== @SafeVarargs