|
@@ -0,0 +1,144 @@
|
|
|
+package com.citu.module.menduner.system.service.tag;
|
|
|
+
|
|
|
+
|
|
|
+import com.citu.framework.common.pojo.PageResult;
|
|
|
+import com.citu.framework.test.core.ut.BaseDbUnitTest;
|
|
|
+import com.citu.module.menduner.system.controller.base.tag.TagPageReqVO;
|
|
|
+import com.citu.module.menduner.system.controller.base.tag.TagSaveReqVO;
|
|
|
+import com.citu.module.menduner.system.dal.dataobject.tag.TagDO;
|
|
|
+import com.citu.module.menduner.system.dal.mysql.tag.TagMapper;
|
|
|
+import org.junit.jupiter.api.Disabled;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import static com.citu.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
|
|
+import static com.citu.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
|
+import static com.citu.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
|
+import static com.citu.framework.test.core.util.AssertUtils.assertServiceException;
|
|
|
+import static com.citu.framework.test.core.util.RandomUtils.randomLongId;
|
|
|
+import static com.citu.framework.test.core.util.RandomUtils.randomPojo;
|
|
|
+import static com.citu.module.menduner.system.enums.ErrorCodeConstants.TAG_NOT_EXISTS;
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@link TagServiceImpl} 的单元测试类
|
|
|
+ *
|
|
|
+ * @author Rayson
|
|
|
+ */
|
|
|
+@Import(TagServiceImpl.class)
|
|
|
+public class TagServiceImplTest extends BaseDbUnitTest {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TagServiceImpl tagService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TagMapper tagMapper;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCreateTag_success() {
|
|
|
+ // 准备参数
|
|
|
+ TagSaveReqVO createReqVO = randomPojo(TagSaveReqVO.class).setId(null);
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ Long tagId = tagService.createTag(createReqVO);
|
|
|
+ // 断言
|
|
|
+ assertNotNull(tagId);
|
|
|
+ // 校验记录的属性是否正确
|
|
|
+ TagDO tag = tagMapper.selectById(tagId);
|
|
|
+ assertPojoEquals(createReqVO, tag, "id");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateTag_success() {
|
|
|
+ // mock 数据
|
|
|
+ TagDO dbTag = randomPojo(TagDO.class);
|
|
|
+ tagMapper.insert(dbTag);// @Sql: 先插入出一条存在的数据
|
|
|
+ // 准备参数
|
|
|
+ TagSaveReqVO updateReqVO = randomPojo(TagSaveReqVO.class, o -> {
|
|
|
+ o.setId(dbTag.getId()); // 设置更新的 ID
|
|
|
+ });
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ tagService.updateTag(updateReqVO);
|
|
|
+ // 校验是否更新正确
|
|
|
+ TagDO tag = tagMapper.selectById(updateReqVO.getId()); // 获取最新的
|
|
|
+ assertPojoEquals(updateReqVO, tag);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateTag_notExists() {
|
|
|
+ // 准备参数
|
|
|
+ TagSaveReqVO updateReqVO = randomPojo(TagSaveReqVO.class);
|
|
|
+
|
|
|
+ // 调用, 并断言异常
|
|
|
+ assertServiceException(() -> tagService.updateTag(updateReqVO), TAG_NOT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteTag_success() {
|
|
|
+ // mock 数据
|
|
|
+ TagDO dbTag = randomPojo(TagDO.class);
|
|
|
+ tagMapper.insert(dbTag);// @Sql: 先插入出一条存在的数据
|
|
|
+ // 准备参数
|
|
|
+ Long id = dbTag.getId();
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ tagService.deleteTag(id);
|
|
|
+ // 校验数据不存在了
|
|
|
+ assertNull(tagMapper.selectById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteTag_notExists() {
|
|
|
+ // 准备参数
|
|
|
+ Long id = randomLongId();
|
|
|
+
|
|
|
+ // 调用, 并断言异常
|
|
|
+ assertServiceException(() -> tagService.deleteTag(id), TAG_NOT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
|
|
+ public void testGetTagPage() {
|
|
|
+ // mock 数据
|
|
|
+ TagDO dbTag = randomPojo(TagDO.class, o -> { // 等会查询到
|
|
|
+ o.setNameCn(null);
|
|
|
+ o.setNameEn(null);
|
|
|
+ o.setType(null);
|
|
|
+ o.setParentId(null);
|
|
|
+ o.setLevel(null);
|
|
|
+ o.setCreateTime(null);
|
|
|
+ });
|
|
|
+ tagMapper.insert(dbTag);
|
|
|
+ // 测试 nameCn 不匹配
|
|
|
+ tagMapper.insert(cloneIgnoreId(dbTag, o -> o.setNameCn(null)));
|
|
|
+ // 测试 nameEn 不匹配
|
|
|
+ tagMapper.insert(cloneIgnoreId(dbTag, o -> o.setNameEn(null)));
|
|
|
+ // 测试 type 不匹配
|
|
|
+ tagMapper.insert(cloneIgnoreId(dbTag, o -> o.setType(null)));
|
|
|
+ // 测试 parentId 不匹配
|
|
|
+ tagMapper.insert(cloneIgnoreId(dbTag, o -> o.setParentId(null)));
|
|
|
+ // 测试 level 不匹配
|
|
|
+ tagMapper.insert(cloneIgnoreId(dbTag, o -> o.setLevel(null)));
|
|
|
+ // 测试 createTime 不匹配
|
|
|
+ tagMapper.insert(cloneIgnoreId(dbTag, o -> o.setCreateTime(null)));
|
|
|
+ // 准备参数
|
|
|
+ TagPageReqVO reqVO = new TagPageReqVO();
|
|
|
+ reqVO.setNameCn(null);
|
|
|
+ reqVO.setNameEn(null);
|
|
|
+ reqVO.setType(null);
|
|
|
+ reqVO.setParentId(null);
|
|
|
+ reqVO.setLevel(null);
|
|
|
+ reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ PageResult<TagDO> pageResult = tagService.getTagPage(reqVO);
|
|
|
+ // 断言
|
|
|
+ assertEquals(1, pageResult.getTotal());
|
|
|
+ assertEquals(1, pageResult.getList().size());
|
|
|
+ assertPojoEquals(dbTag, pageResult.getList().get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|