From 83fb8d533096dcd8370d0a8a219053f1e4a482ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E4=B8=80=E4=B8=AA=E4=BA=BA=E5=9C=A8=E8=BF=99?= =?UTF-8?q?=E5=84=BF=E5=B9=B2=E5=98=9B=E4=BD=A0=E6=98=AF=E6=9D=A5=E6=8B=89?= =?UTF-8?q?=E5=B1=8E=E7=9A=84=E5=90=A7?= <1421374934@qq.com> Date: Thu, 6 Apr 2023 19:41:15 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20:white=5Fcheck=5Fmark:=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/dev/forest.sql | 6 + .../rymcu/forest/service/RoleServiceTest.java | 40 ++++++ .../forest/service/SponsorServiceTest.java | 26 ++++ .../rymcu/forest/service/TagServiceTest.java | 125 +++++++++++++++++- .../forest/service/TopicServiceTest.java | 56 ++++++++ .../service/TransactionRecordServiceTest.java | 38 ++++++ 6 files changed, 287 insertions(+), 4 deletions(-) diff --git a/docker/dev/forest.sql b/docker/dev/forest.sql index 13ba877..ff72ce3 100644 --- a/docker/dev/forest.sql +++ b/docker/dev/forest.sql @@ -840,3 +840,9 @@ VALUES (2, 1, '100000002', 1207980.00000000, 2, '2020-11-26 21:37:18', '1'); INSERT INTO forest.forest_bank_account (id, id_bank, bank_account, account_balance, account_owner, created_time, account_type) VALUES (1, 1, '100000001', 997500000.00000000, 1, '2020-11-26 21:36:21', '1'); +INSERT INTO `forest`.`forest_bank_account` (`id`, `id_bank`, `bank_account`, `account_balance`, `account_owner`, + `created_time`, `account_type`) +VALUES (3, 1, '100000061', 100.00000000, 65001, '2020-11-26 21:37:18', '0'); +INSERT INTO `forest`.`forest_bank_account` (`id`, `id_bank`, `bank_account`, `account_balance`, `account_owner`, + `created_time`, `account_type`) +VALUES (4, 1, '100000063', 100.00000000, 65003, '2020-11-26 21:37:18', '0'); diff --git a/src/test/java/com/rymcu/forest/service/RoleServiceTest.java b/src/test/java/com/rymcu/forest/service/RoleServiceTest.java index 23d37e9..13f113a 100644 --- a/src/test/java/com/rymcu/forest/service/RoleServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/RoleServiceTest.java @@ -1,28 +1,68 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.entity.Role; +import com.rymcu.forest.entity.User; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * 角色service测试 + */ class RoleServiceTest extends BaseServiceTest { + @Autowired + private RoleService roleService; + @BeforeEach void setUp() { } @Test + @DisplayName("查询用户角色") void selectRoleByUser() { + User user = new User(); + user.setIdUser(1L); + List roles = roleService.selectRoleByUser(user); + assertFalse(roles.isEmpty()); + + user.setIdUser(0L); + roleService.selectRoleByUser(user); + assertFalse(roles.isEmpty()); } @Test + @DisplayName("查询用户角色") void findByIdUser() { + List roles = roleService.findByIdUser(1L); + assertFalse(roles.isEmpty()); + + roles = roleService.findByIdUser(0L); + assertTrue(roles.isEmpty()); } @Test + @DisplayName("更新角色状态") void updateStatus() { + boolean b = roleService.updateStatus(1L, "1"); + assertTrue(b); } @Test + @DisplayName("添加/更新角色") void saveRole() { + Role role = new Role(); + role.setName("test_role"); + role.setStatus("0"); + role.setWeights(1); + boolean b = roleService.saveRole(role); + assertTrue(b); } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/SponsorServiceTest.java b/src/test/java/com/rymcu/forest/service/SponsorServiceTest.java index eee611d..73b8d6c 100644 --- a/src/test/java/com/rymcu/forest/service/SponsorServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/SponsorServiceTest.java @@ -1,16 +1,42 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.entity.Sponsor; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import java.math.BigDecimal; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * 赞赏test + */ class SponsorServiceTest extends BaseServiceTest { + @Autowired + private SponsorService sponsorService; + @BeforeEach void setUp() { } @Test + @DisplayName("赞赏") void sponsorship() { + Sponsor sponsor = new Sponsor(); + assertThrows(NullPointerException.class, () -> sponsorService.sponsorship(sponsor)); + sponsor.setSponsor(1L); + sponsor.setSponsorshipMoney(BigDecimal.TEN); + sponsor.setDataType(200); + sponsor.setDataId(65001L); + + assertThrows(DataIntegrityViolationException.class, () -> sponsorService.sponsorship(sponsor)); + + sponsor.setDataType(3); + sponsorService.sponsorship(sponsor); } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/TagServiceTest.java b/src/test/java/com/rymcu/forest/service/TagServiceTest.java index e30c63e..f18c782 100644 --- a/src/test/java/com/rymcu/forest/service/TagServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/TagServiceTest.java @@ -1,28 +1,145 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.core.exception.BusinessException; +import com.rymcu.forest.dto.ArticleDTO; +import com.rymcu.forest.dto.ArticleTagDTO; +import com.rymcu.forest.dto.Author; +import com.rymcu.forest.dto.LabelModel; +import com.rymcu.forest.entity.Article; +import com.rymcu.forest.entity.Tag; +import com.rymcu.forest.entity.User; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * 标签test + */ class TagServiceTest extends BaseServiceTest { + /** + * 测试用的Article数据,用于该单元测试的一系列操作 + */ + private final ArticleDTO testArticle; + /** + * 与Article相关联的测试User数据(建表时提前放入) + */ + private final User testUser = new User(); + @Autowired + private TagService tagService; + @Autowired + private ArticleService articleService; + + { + // 构建数据之间的关联结构 + Author testAuthor = Author.builder() + .idUser(2L) + .userArticleCount("0") + .userAccount("testUser") + .userNickname("testUser") + .userAvatarURL(null) + .build(); + BeanUtils.copyProperties(testAuthor, testUser); + + ArticleTagDTO tagDTO = ArticleTagDTO.builder() + .tagTitle("Test") + .tagDescription("Test") + .idTag(111) + .tagAuthorId(testUser.getIdUser()) + .build(); + + List tags = new ArrayList<>(); + tags.add(tagDTO); + + testArticle = ArticleDTO.builder() + .articleAuthor(testAuthor) + .articleAuthorId(testAuthor.getIdUser()) + .articleContent("Test") + .articleLink("Test") + .articlePerfect("0") + .articlePermalink("Test") + .articleAuthorName(testAuthor.getUserNickname()) + .articleCommentCount(0) + .articleStatus("0") + .articleTags("Test") + .articleContentHtml("

Test

") + .articleTitle("Test") + .articleType("0") + .articlePreviewContent("Test") + .articleSponsorCount(12) + .articlePermalink("Test") + .articleViewCount(0) + .tags(tags) + .build(); + } + @BeforeEach - void setUp() { + void setUp() throws UnsupportedEncodingException { + Long articleId = articleService.postArticle(testArticle, testUser); + assertNotNull(articleId); + testArticle.setIdArticle(articleId); } @Test - void saveTagArticle() { + @DisplayName("保存文章标签") + void saveTagArticle() throws UnsupportedEncodingException { + Article article = new Article(); + BeanUtils.copyProperties(testArticle, article); + Integer integer = tagService.saveTagArticle(article, "article", testUser.getIdUser()); + assertEquals(1, integer); } @Test - void cleanUnusedTag() { + @DisplayName("清除未使用标签") + void cleanUnusedTag() throws Exception { + boolean b = tagService.cleanUnusedTag(); + assertFalse(b); + + Tag tag = new Tag(); + tag.setTagDescription("test"); + tag.setTagTitle("test"); + Tag tag1 = tagService.saveTag(tag); + assertNotNull(tag1.getIdTag()); + + b = tagService.cleanUnusedTag(); + assertTrue(b); } @Test - void saveTag() { + @DisplayName("添加/更新标签") + void saveTag() throws Exception { + List tagLabels = tagService.findTagLabels(); + assertTrue(tagLabels.isEmpty()); + + Tag tag = new Tag(); + tag.setTagDescription("test"); + + assertThrows(IllegalArgumentException.class, () -> tagService.saveTag(tag)); + + tag.setTagTitle("test"); + Tag tag1 = tagService.saveTag(tag); + assertNotNull(tag1.getIdTag()); + + tagLabels = tagService.findTagLabels(); + assertTrue(tagLabels.isEmpty()); + + tag.setIdTag(null); + assertThrows(BusinessException.class, () -> tagService.saveTag(tag)); } @Test + @DisplayName("获取标签列表") void findTagLabels() { + List tagLabels = tagService.findTagLabels(); + assertTrue(tagLabels.isEmpty()); } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/TopicServiceTest.java b/src/test/java/com/rymcu/forest/service/TopicServiceTest.java index 8d6a55e..0bc01cf 100644 --- a/src/test/java/com/rymcu/forest/service/TopicServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/TopicServiceTest.java @@ -1,40 +1,96 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.core.exception.BusinessException; +import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.dto.admin.TagDTO; +import com.rymcu.forest.dto.admin.TopicTagDTO; +import com.rymcu.forest.entity.Tag; +import com.rymcu.forest.entity.Topic; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * 主题test + */ class TopicServiceTest extends BaseServiceTest { + @Autowired + private TopicService topicService; + @BeforeEach void setUp() { } @Test + @DisplayName("获取导航主题数据") void findTopicNav() { + List topicNav = topicService.findTopicNav(); + assertTrue(topicNav.isEmpty()); } @Test + @DisplayName("根据 topicUri 获取主题信息及旗下标签数据") void findTopicByTopicUri() { + Topic topicNav = topicService.findTopicByTopicUri("rymcu.com/topic/123792"); + assertNull(topicNav); } @Test + @DisplayName("新增/更新主题信息") void saveTopic() { + Topic topic = new Topic(); + assertThrows(IllegalArgumentException.class, () -> topicService.saveTopic(topic)); + + topic.setTopicTitle("test"); + topicService.saveTopic(topic); + assertNotNull(topic.getIdTopic()); + + topic.setIdTopic(null); + assertThrows(BusinessException.class, () -> topicService.saveTopic(topic)); + } @Test + @DisplayName("查询未绑定标签") void findUnbindTagsById() { + List tags = topicService.findUnbindTagsById(1L, "test"); + assertTrue(tags.isEmpty()); } @Test + @DisplayName("绑定标签") void bindTopicTag() { + TopicTagDTO topicTagDTO = new TopicTagDTO(); + topicTagDTO.setIdTag(1L); + topicTagDTO.setIdTopic(1L); + topicService.bindTopicTag(topicTagDTO); + assertNotNull(topicTagDTO.getIdTag()); } @Test + @DisplayName("取消绑定标签") void unbindTopicTag() { + TopicTagDTO topicTagDTO = new TopicTagDTO(); + topicTagDTO.setIdTag(1L); + topicTagDTO.setIdTopic(1L); + assertThrows(ServiceException.class, () -> topicService.unbindTopicTag(topicTagDTO)); + + topicService.bindTopicTag(topicTagDTO); + + assertDoesNotThrow(() -> topicService.unbindTopicTag(topicTagDTO)); } @Test + @DisplayName("获取主题下标签列表") void findTagsByTopicUri() { + List tags = topicService.findTagsByTopicUri("test.com/topic/123792"); + assertNull(tags); } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/TransactionRecordServiceTest.java b/src/test/java/com/rymcu/forest/service/TransactionRecordServiceTest.java index 97500b5..c640508 100644 --- a/src/test/java/com/rymcu/forest/service/TransactionRecordServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/TransactionRecordServiceTest.java @@ -1,32 +1,70 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.dto.TransactionRecordDTO; +import com.rymcu.forest.entity.TransactionRecord; +import com.rymcu.forest.enumerate.TransactionEnum; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; class TransactionRecordServiceTest extends BaseServiceTest { + @Autowired + private TransactionRecordService transactionRecordService; + @BeforeEach void setUp() { } @Test void transfer() { + TransactionRecord transactionRecord = new TransactionRecord(); + transactionRecord.setToBankAccount("100000061"); + transactionRecord.setFormBankAccount("100000063"); + transactionRecord.setMoney(BigDecimal.TEN); + transactionRecordService.transfer(transactionRecord); + assertNotNull(transactionRecord.getIdTransactionRecord()); } @Test + @DisplayName("查询指定账户的交易记录") void findTransactionRecords() { + + List transactionRecords = transactionRecordService.findTransactionRecords("100000001", "2020-05-05", "2025-05-05"); + + assertTrue(transactionRecords.isEmpty()); } @Test + @DisplayName("根据用户主键进行交易") void userTransfer() { + TransactionRecord transactionRecord = transactionRecordService.userTransfer(65001L, 65003L, TransactionEnum.ArticleSponsor); + assertNotNull(transactionRecord); + assertNotNull(transactionRecord.getIdTransactionRecord()); } @Test + @DisplayName("社区银行转账/奖励发放") void bankTransfer() { + TransactionRecord transactionRecord = transactionRecordService.bankTransfer(65001L, TransactionEnum.Answer); + assertNotNull(transactionRecord); + assertNotNull(transactionRecord.getIdTransactionRecord()); } @Test + @DisplayName("发放新手奖励") void newbieRewards() { + TransactionRecord transactionRecord = new TransactionRecord(); + transactionRecord.setToBankAccount("100000061"); + transactionRecord = transactionRecordService.newbieRewards(transactionRecord); + assertNotNull(transactionRecord.getIdTransactionRecord()); } } \ No newline at end of file