dao层修改 单元测试

This commit is contained in:
禾几海
2020-05-25 13:50:11 +08:00
parent 190e1624ca
commit 9185ff8f58
3 changed files with 210 additions and 72 deletions

View File

@@ -8,8 +8,8 @@ import java.util.List;
/** /**
* @Author: 小海 * @Author: 小海
* @Date 2019/06/30 16:19 * @Date: 2019/06/30 16:19
* @Description * @Description:
*/ */
@Mapper @Mapper
@Repository @Repository
@@ -18,11 +18,9 @@ public interface CommentMapper {
int updateContent(String content, long id); int updateContent(String content, long id);
int updateResponder(String responder, long id);
int delete(long id); int delete(long id);
int deleteByArticleId(long articleId); int deleteByPagePath(String pagePath);
boolean existsById(long id); boolean existsById(long id);
@@ -30,19 +28,13 @@ public interface CommentMapper {
Comment getLastestComment(); Comment getLastestComment();
List<Comment> findAllByAuthorIDAndType(long id, boolean isComment); List<Comment> findAllByFromUser(long id);
List<Comment> findAllByPId(long pid); List<Comment> findAllByPid(long pid);
List<Comment> findAllByArticleID(long articleId); List<Comment> findAllByPagePath(String pagePath);
List<Comment> findAllByArticleIDAndPId(long articleID, long pid); List<Comment> findAllByPagePathAndPid(String pagePath, long pid);
List<Comment> findCommentsByTypeAndPId(boolean isComment, long pid); long countByPagePath(String pagePath);
List<Comment> findAllByPId(int pid);
List<Comment> findAllByType(boolean isComment);
long countByType(boolean isComment);
} }

View File

@@ -3,21 +3,44 @@
<mapper namespace="cn.celess.blog.mapper.CommentMapper"> <mapper namespace="cn.celess.blog.mapper.CommentMapper">
<resultMap id="commentResultMap" type="cn.celess.blog.entity.Comment"> <resultMap id="commentResultMap" type="cn.celess.blog.entity.Comment">
<id column="co_id" property="id"/> <id column="co_id" property="id"/>
<result column="co_article_id" property="articleID"/> <result column="co_page_path" property="pagePath"/>
<result column="is_comment" property="type"/> <result column="co_status" property="status"/>
<result column="author_id" property="authorID"/>
<result column="co_content" property="content"/> <result column="co_content" property="content"/>
<result column="co_date" property="date"/> <result column="co_date" property="date"/>
<result column="co_pid" property="pid"/> <result column="co_pid" property="pid"/>
<result column="co_response_id" property="responseId"/> <result column="co_from_author_id" property="fromUser.id"/>
<result column="co_to_author_id" property="toUser.id"/>
<result column="is_delete" property="delete"/>
</resultMap> </resultMap>
<insert id="insert"> <resultMap id="commentViewResultMap" type="cn.celess.blog.entity.Comment">
insert into comment (co_article_id, is_comment, author_id, co_content, co_date, co_pid) <id column="commentId" property="id"/>
VALUES (#{articleID}, #{type}, #{authorID}, #{content}, #{date}, #{pid}) <result column="pagePath" property="pagePath"/>
<selectKey resultType="java.lang.Long" keyProperty="id"> <result column="status" property="status"/>
SELECT LAST_INSERT_ID() AS id <result column="content" property="content"/>
</selectKey> <result column="date" property="date"/>
<result column="pid" property="pid"/>
<result column="fromAuthorId" property="fromUser.id"/>
<result column="toAuthorId" property="toUser.id"/>
<result column="isDelete" property="delete"/>
<association property="fromUser" column="fromAuthorId" javaType="cn.celess.blog.entity.User">
<id column="fromAuthorId" property="id"/>
<result column="fromAuthorEmail" property="email"/>
<result column="fromAuthorDisplayName" property="displayName"/>
<result column="fromAuthorAvatar" property="avatarImgUrl"/>
</association>
<association property="toUser" column="toAuthorId" javaType="cn.celess.blog.entity.User">
<id column="toAuthorId" property="id"/>
<result column="toAuthorEmail" property="email"/>
<result column="toAuthorDisplayName" property="displayName"/>
<result column="toAuthorAvatar" property="avatarImgUrl"/>
</association>
</resultMap>
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into comment (co_page_path, co_content, co_date, co_pid, co_from_author_id, co_to_author_id, is_delete)
VALUES (#{pagePath}, #{content}, now(), #{pid}, #{fromUser.id}, #{toUser.id}, false)
</insert> </insert>
<update id="updateContent"> <update id="updateContent">
@@ -26,71 +49,62 @@
where co_id = #{id} where co_id = #{id}
</update> </update>
<update id="updateResponder"> <update id="delete">
update comment update comment
set co_response_id =#{responder} set is_delete = true
where co_id = #{id} where co_id = #{id}
</update> </update>
<delete id="delete">
delete <update id="deleteByPagePath">
from comment update comment
where co_id = #{id} set is_delete = true
</delete> where co_page_path = #{path}
<delete id="deleteByArticleId"> </update>
delete
from comment
where co_article_id = #{articleId}
</delete>
<select id="existsById" resultType="java.lang.Boolean"> <select id="existsById" resultType="java.lang.Boolean">
SELECT EXISTS(SELECT * FROM comment WHERE co_id = #{id}) SELECT EXISTS(SELECT * FROM comment WHERE co_id = #{id})
</select> </select>
<select id="findCommentById" resultMap="commentResultMap">
<select id="findCommentById" resultMap="commentViewResultMap">
select * select *
from comment from commentView
where co_id = #{id} where commentId = #{id}
</select> </select>
<select id="findAllByAuthorIDAndType" resultMap="commentResultMap">
<select id="findAllByFromUser" resultMap="commentViewResultMap">
select * select *
from comment from commentView
where author_id = #{id} where fromAuthorId = #{id}
and is_comment = #{isComment}
</select> </select>
<select id="findAllByPId" resultMap="commentResultMap">
<select id="findAllByPid" resultMap="commentViewResultMap">
select * select *
from comment from commentView
where co_pid = #{pid} where pid = #{pid}
</select> </select>
<select id="findAllByArticleID" resultMap="commentResultMap">
<select id="findAllByPagePath" resultMap="commentViewResultMap">
select * select *
from comment from commentView
where co_article_id = #{articleId} where pagePath = #{pagePath}
</select> </select>
<select id="findAllByArticleIDAndPId" resultMap="commentResultMap">
<select id="findAllByPagePathAndPid" resultMap="commentViewResultMap">
select * select *
from comment from commentView
where co_article_id = #{articleID} where pagePath = #{pagePath}
and co_pid = #{pid} and pid = #{pid}
</select> </select>
<select id="findCommentsByTypeAndPId" resultMap="commentResultMap">
select * <select id="countByPagePath" resultType="java.lang.Long">
from comment
where is_comment = #{isComment}
and co_pid = #{pid}
</select>
<select id="findAllByType" resultMap="commentResultMap">
select *
from comment
where is_comment = #{isComment}
</select>
<select id="countByType" resultType="java.lang.Long">
select count(*) select count(*)
from comment from commentView
where is_comment = #{isComment} where pagePath = #{pagePath}
</select> </select>
<select id="getLastestComment" resultMap="commentResultMap"> <select id="getLastestComment" resultMap="commentViewResultMap">
select * select *
from comment from commentView
order by co_id desc order by commentId desc
limit 1 limit 1
</select> </select>

View File

@@ -0,0 +1,132 @@
package cn.celess.blog.mapper;
import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.Comment;
import cn.celess.blog.entity.User;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.Assert.*;
public class CommentMapperTest extends BaseTest {
@Autowired
UserMapper userMapper;
@Autowired
CommentMapper commentMapper;
@Test
public void insert() {
Comment comment = generateComment();
assertNotNull(comment.getId());
}
@Test
public void updateContent() {
Comment comment = generateComment();
comment.setContent(randomStr(10));
assertEquals(1, commentMapper.updateContent(comment.getContent(), comment.getId()));
}
@Test
public void delete() {
Comment comment = generateComment();
assertEquals(1, commentMapper.delete(comment.getId()));
Comment commentById = commentMapper.findCommentById(comment.getId());
assertTrue(commentById.isDelete());
}
@Test
public void deleteByPagePath() {
Comment comment = generateComment();
assertTrue(commentMapper.deleteByPagePath(comment.getPagePath()) >= 1);
Comment commentById = commentMapper.findCommentById(comment.getId());
assertTrue(commentById.isDelete());
}
@Test
public void existsById() {
Comment comment = generateComment();
assertTrue(commentMapper.existsById(comment.getId()));
}
@Test
public void findCommentById() {
Comment comment = generateComment();
assertNotNull(commentMapper.findCommentById(comment.getId()));
}
@Test
public void getLastestComment() {
Comment comment = generateComment();
Comment lastestComment = commentMapper.getLastestComment();
assertEquals(comment.getId(), lastestComment.getId());
}
@Test
public void findAllByFromUser() {
Comment comment = generateComment();
List<Comment> allByFromUser = commentMapper.findAllByFromUser(comment.getFromUser().getId());
assertNotEquals(0, allByFromUser);
allByFromUser.forEach(comment1 -> assertEquals(comment.getFromUser().getId(), comment1.getFromUser().getId()));
}
@Test
public void findAllByPid() {
Comment comment = generateComment();
List<Comment> allByPid = commentMapper.findAllByPid(comment.getPid());
assertTrue(allByPid.size() >= 1);
}
@Test
public void findAllByPagePath() {
Comment comment = generateComment();
List<Comment> allByPagePath = commentMapper.findAllByPagePath(comment.getPagePath());
assertTrue(allByPagePath.size() >= 1);
allByPagePath.forEach(comment1 -> assertEquals(comment.getPagePath(), comment1.getPagePath()));
}
@Test
public void findAllByPagePathAndPid() {
Comment comment = generateComment();
List<Comment> allByPagePathAndPid = commentMapper.findAllByPagePathAndPid(comment.getPagePath(), comment.getPid());
assertTrue(allByPagePathAndPid.size() >= 1);
allByPagePathAndPid.forEach(comment1 -> {
assertEquals(comment.getPagePath(), comment1.getPagePath());
assertEquals(comment.getPid(), comment1.getPid());
});
}
@Test
public void testFindAllByPid() {
Comment comment = generateComment();
List<Comment> findAllByPid = commentMapper.findAllByPid(comment.getPid());
assertTrue(findAllByPid.size() >= 1);
findAllByPid.forEach(comment1 -> assertEquals(comment.getPid(), comment1.getPid()));
}
@Test
public void countByType() {
Comment comment = generateComment();
long l = commentMapper.countByPagePath(comment.getPagePath());
assertTrue(l >= 1);
}
private Comment generateComment() {
User from = userMapper.findById(1);
assertNotNull(from);
User to = userMapper.findById(2);
assertNotNull(to);
Comment comment = new Comment();
comment.setContent(randomStr(8));
comment.setFromUser(from);
comment.setToUser(to);
comment.setPagePath("/tags");
comment.setPid(-1L);
commentMapper.insert(comment);
return comment;
}
}