|
@@ -0,0 +1,175 @@
|
|
|
+package cn.iocoder.yudao.module.promotion.service.articlecategory;
|
|
|
+
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
|
+import cn.iocoder.yudao.module.promotion.controller.admin.articlecategory.vo.ArticleCategoryCreateReqVO;
|
|
|
+import cn.iocoder.yudao.module.promotion.controller.admin.articlecategory.vo.ArticleCategoryExportReqVO;
|
|
|
+import cn.iocoder.yudao.module.promotion.controller.admin.articlecategory.vo.ArticleCategoryPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.promotion.controller.admin.articlecategory.vo.ArticleCategoryUpdateReqVO;
|
|
|
+import cn.iocoder.yudao.module.promotion.dal.dataobject.articlecategory.ArticleCategoryDO;
|
|
|
+import cn.iocoder.yudao.module.promotion.dal.mysql.articlecategory.ArticleCategoryMapper;
|
|
|
+import org.junit.jupiter.api.Disabled;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
|
|
+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.randomLongId;
|
|
|
+import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
|
+import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.ARTICLE_CATEGORY_NOT_EXISTS;
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@link ArticleCategoryServiceImpl} 的单元测试类
|
|
|
+ *
|
|
|
+ * @author HUIHUI
|
|
|
+ */
|
|
|
+@Import(ArticleCategoryServiceImpl.class)
|
|
|
+public class ArticleCategoryServiceImplTest extends BaseDbUnitTest {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ArticleCategoryServiceImpl articleCategoryService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ArticleCategoryMapper articleCategoryMapper;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCreateArticleCategory_success() {
|
|
|
+ // 准备参数
|
|
|
+ ArticleCategoryCreateReqVO reqVO = randomPojo(ArticleCategoryCreateReqVO.class);
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ Long articleCategoryId = articleCategoryService.createArticleCategory(reqVO);
|
|
|
+ // 断言
|
|
|
+ assertNotNull(articleCategoryId);
|
|
|
+ // 校验记录的属性是否正确
|
|
|
+ ArticleCategoryDO articleCategory = articleCategoryMapper.selectById(articleCategoryId);
|
|
|
+ assertPojoEquals(reqVO, articleCategory);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateArticleCategory_success() {
|
|
|
+ // mock 数据
|
|
|
+ ArticleCategoryDO dbArticleCategory = randomPojo(ArticleCategoryDO.class);
|
|
|
+ articleCategoryMapper.insert(dbArticleCategory);// @Sql: 先插入出一条存在的数据
|
|
|
+ // 准备参数
|
|
|
+ ArticleCategoryUpdateReqVO reqVO = randomPojo(ArticleCategoryUpdateReqVO.class, o -> {
|
|
|
+ o.setId(dbArticleCategory.getId()); // 设置更新的 ID
|
|
|
+ });
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ articleCategoryService.updateArticleCategory(reqVO);
|
|
|
+ // 校验是否更新正确
|
|
|
+ ArticleCategoryDO articleCategory = articleCategoryMapper.selectById(reqVO.getId()); // 获取最新的
|
|
|
+ assertPojoEquals(reqVO, articleCategory);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateArticleCategory_notExists() {
|
|
|
+ // 准备参数
|
|
|
+ ArticleCategoryUpdateReqVO reqVO = randomPojo(ArticleCategoryUpdateReqVO.class);
|
|
|
+
|
|
|
+ // 调用, 并断言异常
|
|
|
+ assertServiceException(() -> articleCategoryService.updateArticleCategory(reqVO), ARTICLE_CATEGORY_NOT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteArticleCategory_success() {
|
|
|
+ // mock 数据
|
|
|
+ ArticleCategoryDO dbArticleCategory = randomPojo(ArticleCategoryDO.class);
|
|
|
+ articleCategoryMapper.insert(dbArticleCategory);// @Sql: 先插入出一条存在的数据
|
|
|
+ // 准备参数
|
|
|
+ Long id = dbArticleCategory.getId();
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ articleCategoryService.deleteArticleCategory(id);
|
|
|
+ // 校验数据不存在了
|
|
|
+ assertNull(articleCategoryMapper.selectById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteArticleCategory_notExists() {
|
|
|
+ // 准备参数
|
|
|
+ Long id = randomLongId();
|
|
|
+
|
|
|
+ // 调用, 并断言异常
|
|
|
+ assertServiceException(() -> articleCategoryService.deleteArticleCategory(id), ARTICLE_CATEGORY_NOT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
|
|
+ public void testGetArticleCategoryPage() {
|
|
|
+ // mock 数据
|
|
|
+ ArticleCategoryDO dbArticleCategory = randomPojo(ArticleCategoryDO.class, o -> { // 等会查询到
|
|
|
+ o.setName(null);
|
|
|
+ o.setPicUrl(null);
|
|
|
+ o.setStatus(null);
|
|
|
+ o.setSort(null);
|
|
|
+ o.setCreateTime(null);
|
|
|
+ });
|
|
|
+ articleCategoryMapper.insert(dbArticleCategory);
|
|
|
+ // 测试 name 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setName(null)));
|
|
|
+ // 测试 picUrl 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setPicUrl(null)));
|
|
|
+ // 测试 status 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setStatus(null)));
|
|
|
+ // 测试 sort 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setSort(null)));
|
|
|
+ // 测试 createTime 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setCreateTime(null)));
|
|
|
+ // 准备参数
|
|
|
+ ArticleCategoryPageReqVO reqVO = new ArticleCategoryPageReqVO();
|
|
|
+ reqVO.setName(null);
|
|
|
+ reqVO.setStatus(null);
|
|
|
+ reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ PageResult<ArticleCategoryDO> pageResult = articleCategoryService.getArticleCategoryPage(reqVO);
|
|
|
+ // 断言
|
|
|
+ assertEquals(1, pageResult.getTotal());
|
|
|
+ assertEquals(1, pageResult.getList().size());
|
|
|
+ assertPojoEquals(dbArticleCategory, pageResult.getList().get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
|
|
+ public void testGetArticleCategoryList() {
|
|
|
+ // mock 数据
|
|
|
+ ArticleCategoryDO dbArticleCategory = randomPojo(ArticleCategoryDO.class, o -> { // 等会查询到
|
|
|
+ o.setName(null);
|
|
|
+ o.setPicUrl(null);
|
|
|
+ o.setStatus(null);
|
|
|
+ o.setSort(null);
|
|
|
+ o.setCreateTime(null);
|
|
|
+ });
|
|
|
+ articleCategoryMapper.insert(dbArticleCategory);
|
|
|
+ // 测试 name 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setName(null)));
|
|
|
+ // 测试 picUrl 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setPicUrl(null)));
|
|
|
+ // 测试 status 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setStatus(null)));
|
|
|
+ // 测试 sort 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setSort(null)));
|
|
|
+ // 测试 createTime 不匹配
|
|
|
+ articleCategoryMapper.insert(cloneIgnoreId(dbArticleCategory, o -> o.setCreateTime(null)));
|
|
|
+ // 准备参数
|
|
|
+ ArticleCategoryExportReqVO reqVO = new ArticleCategoryExportReqVO();
|
|
|
+ reqVO.setName(null);
|
|
|
+ reqVO.setStatus(null);
|
|
|
+ reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ List<ArticleCategoryDO> list = articleCategoryService.getArticleCategoryList(reqVO);
|
|
|
+ // 断言
|
|
|
+ assertEquals(1, list.size());
|
|
|
+ assertPojoEquals(dbArticleCategory, list.get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|