Esempio n. 1
0
        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);
            }
        }