public async Task Test_03_AddCommentIncludeBaseComment() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); CommentXCommentHandler cxcHandler = new CommentXCommentHandler(dbContext); CommentXContentHandler ccHandler = new CommentXContentHandler(dbContext); Person rain = CreatePerson("TestRain", "TestRain", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(rain.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); //1. test normal. List<long> contentIds = new List<long>(); Content content = new Content() { ContentPath = "testBeCommentPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(content); contentHandler.SaveChanges(); contentIds.Add(content.ID); Comment beComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hey, i am be comment test.", photoContentIds: contentIds); Comment test_beComment = await commentHandler.GetByIdAsync(beComment.ID); Assert.IsNotNull(test_beComment); Assert.AreEqual(test_beComment.PersonID, commentPerson.ID); Assert.AreEqual(test_beComment.BlogID, testBlog.ID); Assert.AreEqual(test_beComment.Content, "hey, i am be comment test."); Assert.AreEqual(test_beComment.CommentXContents.Count, 1); Assert.AreEqual(test_beComment.CommentXContents.First().Content.ContentPath, "testBeCommentPath"); Assert.AreEqual(test_beComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(test_beComment.CommentXContents.First().Content.ContentBinary, new byte[] { 1, 3, 5 }); contentIds = new List<long>(); Content newCommentContent = new Content() { ContentPath = "testBeCommentPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(newCommentContent); contentHandler.SaveChanges(); contentIds.Add(newCommentContent.ID); Comment newComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hey, i am new comment test.", photoContentIds: contentIds, baseCommentId: test_beComment.ID); Comment test_newComment = await commentHandler.GetByIdAsync(newComment.ID); Assert.IsNotNull(test_newComment); Assert.AreEqual(test_newComment.PersonID, commentPerson.ID); Assert.AreEqual(test_newComment.BlogID, testBlog.ID); Assert.AreEqual(test_newComment.Content, "hey, i am new comment test."); Assert.AreEqual(test_newComment.CommentXContents.Count, 1); Assert.AreEqual(test_newComment.NewCommentXComments.Count, 1); Assert.AreEqual(test_newComment.CommentXContents.First().Content.ContentPath, "testBeCommentPath"); Assert.AreEqual(test_newComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(test_newComment.CommentXContents.First().Content.ContentBinary, new byte[] { 1, 3, 5 }); Assert.AreEqual(test_newComment.NewCommentXComments.First().BaseComment.ID, test_beComment.ID); Assert.AreEqual(test_newComment.NewCommentXComments.First().NewComment.ID, test_newComment.ID); Assert.AreEqual(test_newComment.NewCommentXComments.First().BaseComment.Content, "hey, i am be comment test."); Assert.AreEqual(test_newComment.NewCommentXComments.First().NewComment.Content, "hey, i am new comment test."); //2. delete a comment and test it. bool isChecked = false; try { long tmpCommentId = test_beComment.ID; //delete comment. //2.1 first, delete all navigation for comment. for (int i = 0; i < test_beComment.BaseCommentXComments.Count; i++) { cxcHandler.MarkAsDeleted(test_beComment.BaseCommentXComments.ElementAt(i)); } await cxcHandler.SaveChangesAsync(); for (int i = 0; i < test_beComment.CommentXContents.Count; i++) { ccHandler.MarkAsDeleted(test_beComment.CommentXContents.ElementAt(i)); } await ccHandler.SaveChangesAsync(); //2.2 then, delete comment. commentHandler.MarkAsDeleted(test_beComment); await commentHandler.SaveChangesAsync(); Comment test_notExistComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hey, i am new comment test.", baseCommentId: tmpCommentId); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "此评论不存在或者已经被删除"); } Assert.IsTrue(isChecked); } }
public async Task Test_01_AddCommentWithoutContent() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); Person sam = CreatePerson("TestSam", "TestSam", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(sam.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); Person shelly = CreatePerson("TestShelly", "TestShelly", AllowablePersonForComment.All, true); //1. normal test. Comment testComment = await commentHandler.AddCommentAsync(shelly.ID, testBlog.ID, "hello, i am a comment test"); Comment testNormalComment = await commentHandler.GetByIdAsync(testComment.ID); Assert.IsNotNull(testNormalComment); Assert.AreEqual(testNormalComment.PersonID, shelly.ID); Assert.AreEqual(testNormalComment.BlogID, testBlog.ID); Assert.AreEqual(testNormalComment.Content, "hello, i am a comment test"); //2. set error blog id and test it. bool isChecked = false; try { Comment testComment_1 = await commentHandler.AddCommentAsync(commentPerson.ID, 12313121, "hello, i am a comment test"); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "要评论的Blog不存在或者已经被删除"); } Assert.IsTrue(isChecked); //3. test black list comment. Group blackListGroup = new Group() { PersonID = sam.ID, Name = "TestBlackList", Type = GroupType.BlackList }; groupHandler.Add(blackListGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByBlack = new GroupMember() { GroupID = blackListGroup.ID, PersonID = commentPerson.ID }; gmHandler.Add(GroupMemberByBlack); gmHandler.SaveChanges(); isChecked = false; try { Comment testComment_2 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a comment test 4 black list."); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你无法回复评论。"); } Assert.IsTrue(isChecked); //4. remove black list member. gmHandler.MarkAsDeleted(GroupMemberByBlack); gmHandler.SaveChanges(); } }
public async Task Test_02_AddCommentIncludeContent() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); Person jay = CreatePerson("TestJay", "TestJay", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(jay.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); //1. normal test. List<long> contentIds = new List<long>(); Content content = new Content() { ContentPath = "testPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(content); contentHandler.SaveChanges(); contentIds.Add(content.ID); Comment testComment = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a comment include content", photoContentIds: contentIds); Comment testNormalComment = await commentHandler.GetByIdAsync(testComment.ID); Assert.IsNotNull(testNormalComment); Assert.AreEqual(testNormalComment.PersonID, commentPerson.ID); Assert.AreEqual(testNormalComment.BlogID, testBlog.ID); Assert.AreEqual(testNormalComment.Content, "hello, i am a comment include content"); Assert.AreEqual(testNormalComment.CommentXContents.Count, 1); Assert.AreEqual(testNormalComment.CommentXContents.First().Content.ContentPath, "testPath"); Assert.AreEqual(testNormalComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(testNormalComment.CommentXContents.First().Content.ContentBinary, new byte[] { 1, 3, 5 }); //2. test multiple content. contentIds = new List<long>(); for (int i = 0; i < 5; i++) { Content photoContent = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 3, 6, 9 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(photoContent); contentHandler.SaveChanges(); contentIds.Add(photoContent.ID); } Comment testComment_1 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a multiple comment include content oh yes", photoContentIds: contentIds); Comment testMultipleComment = await commentHandler.GetByIdAsync(testComment_1.ID); Assert.IsNotNull(testMultipleComment); Assert.AreEqual(testMultipleComment.PersonID, commentPerson.ID); Assert.AreEqual(testMultipleComment.BlogID, testBlog.ID); Assert.AreEqual(testMultipleComment.Content, "hello, i am a multiple comment include content oh yes"); Assert.AreEqual(testMultipleComment.CommentXContents.Count, 5); Assert.AreEqual(testMultipleComment.CommentXContents.First().Content.ContentPath, "testMultiplePath"); Assert.AreEqual(testMultipleComment.CommentXContents.First().Content.Type, ContentType.Photo); Assert.AreEqual(testMultipleComment.CommentXContents.First().Content.ContentBinary, new byte[] { 3, 6, 9 }); //3. set the content type is video or music and test it. bool isChecked = false; try { contentIds = new List<long>(); for (int i = 0; i < 5; i++) { Content photoContent = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 3, 6, 9 }, Type = i % 2 == 0 ? ContentType.Music : ContentType.Video, MimeType = "jpg" }; contentHandler.Add(photoContent); contentHandler.SaveChanges(); contentIds.Add(photoContent.ID); } Comment testComment_2 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a multiple comment include content but type not photo", photoContentIds: contentIds); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "评论只能附件图片"); } Assert.IsTrue(isChecked); } }