CommentServiceTest.java 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package cn.itcast.article.service;

import cn.itcast.article.po.Comment;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceTest {
    @Autowired
    private CommentService commentService;

    /**
     * 查询所有文档
     */
    @Test
    public void testFindCommentList() {
        List<Comment> commentList = commentService.findCommentList();
        log.info(JSON.toJSONString(commentList));
    }

32 33 34
    /**
     * 根据id查询
     */
35 36 37
    @Test
    public void testFindCommentById() {
        Comment commentById = commentService.findCommentById("1");
38
        log.info(JSON.toJSONString(commentById));
39 40
    }

41 42 43
    /**
     * 新增评论
     */
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    @Test
    public void testSaveComment() {
        Comment comment = new Comment();
        comment.setArticleid("100000");
        comment.setContent("测试添加的数据");
        comment.setCreatedatetime(LocalDateTime.now());
        comment.setUserid("1003");
        comment.setNickname("凯撒大帝");
        comment.setState("1");
        comment.setLikenum(0);
        comment.setReplynum(0);
        commentService.saveComment(comment);
    }

58 59 60 61 62 63 64 65 66 67 68 69 70
    /**
     * 根据parentid分页查询
     */
    @Test
    public void testFindCommentListByUserid() {
        Page<Comment> page = commentService.findByUserid("1003", 1, 10);
        log.info(JSON.toJSONString(page.getTotalElements()));
        log.info(JSON.toJSONString(page.getContent()));
    }

    /**
     * 多字段查询
     */
71
    @Test
72 73 74 75
    public void testFindCommentListByUseridAndLikenum() {
        Page<Comment> page = commentService.findByUseridAndLikenum("1003", 3000, 1, 2);
        log.info(JSON.toJSONString(page.getTotalElements()));
        log.info(JSON.toJSONString(page.getContent()));
76 77
    }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    /**
     * 分页多字段查询,并且
     * 查询userid固定值,并且likenum大于等于的文档
     */
    @Test
    public void testFindByUseridContainingAndLikenumGreaterThanEqual() {
        Page<Comment> page = commentService.findByUseridContainingAndLikenumGreaterThanEqual("1003", 100, 1, 2);
        log.info(JSON.toJSONString(page.getTotalElements()));
        log.info(JSON.toJSONString(page.getContent()));
    }

    /**
     * 正则查询
     */
    @Test
    public void testFindByUseridAndLikenum() {
        List<Comment> list = commentService.findByUseridAndLikenum("1003", 100);
        log.info(JSON.toJSONString(list));
    }

    /**
     * 修改点赞数加1
     */
101 102 103 104 105
    @Test
    public void testUpdateCommentLikenum() {
        commentService.updateCommentLikenum("1");
    }
}