public async Task<Group> CreateGroupAsync(long personId, string groupName, GroupType groupType) { AssertUtil.Waterfall() .AreBigger(personId, 0, "personId must be greater than zero") .IsNotNull(groupType, "groupType can't be null") .Done(); Group group = new Group() { PersonID = personId, Name = groupName, Type = groupType }; this.Add(group); return await SaveChangesAsync() > 0 ? group : null; }
public async Task Test_04_Normal_GetBlogsAsync() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); PersonHandler perHandler = new PersonHandler(dbContext); PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext); //1. test normal. bool isChecked = false; try { List<Blog> invalidPersonIdBlogs = await blogHandler.GetBlogsAsync(999999999); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.Message, "该用户不存在"); } Assert.IsTrue(isChecked); Person master = CreatePerson("TestMasterPer", "TestMasterMind"); Person mary = CreatePerson("TestMary", "TestMary"); Person nick = CreatePerson("TestNick", "TestNick"); Person tony = CreatePerson("TestTony", "TestTony"); //1. create some blog and test it. await blogHandler.CreateBlogAsync(master.ID, "TestContentByMaster", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(nick.ID, "TestContentByNickOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(tony.ID, "TestContentByTonyOne", BlogInfoAccessInfo.All); Follow(master.ID, mary.ID, nick.ID, tony.ID); List<Blog> blogs = await blogHandler.GetBlogsAsync(master.ID); Assert.AreEqual(blogs.Count, 5); Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2); Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == tony.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1); //2. add a group and add some group member test it. Group masterGroup = new Group() { PersonID = master.ID, Name = "TestMasterGroup", Type = GroupType.GroupList }; groupHandler.Add(masterGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByMary = new GroupMember() { GroupID = masterGroup.ID, PersonID = mary.ID }; GroupMember GroupMemberByNick = new GroupMember() { GroupID = masterGroup.ID, PersonID = nick.ID }; gmHandler.Add(GroupMemberByMary); gmHandler.Add(GroupMemberByNick); gmHandler.SaveChanges(); //3. test get blog by group. blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID); Assert.AreEqual(blogs.Count, 4); Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2); Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1); //4. add shield group and test it. Person mike = CreatePerson("TestMike", "TestMike"); Person yoyo = CreatePerson("TestYOYO", "TestYOYO"); Person pipi = CreatePerson("TestPIPI", "TestPIPI"); Person poko = CreatePerson("TestPoko", "TestPoko"); Follow(master.ID, mike.ID, yoyo.ID, pipi.ID, poko.ID); await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(pipi.ID, "TestContentByPipiOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoThree", BlogInfoAccessInfo.All); Group masterShieldGroup = new Group() { PersonID = master.ID, Name = "TestMasterShield", Type = GroupType.ShieldList }; groupHandler.Add(masterShieldGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByPoko = new GroupMember() { GroupID = masterShieldGroup.ID, PersonID = poko.ID }; gmHandler.Add(GroupMemberByPoko); gmHandler.SaveChanges(); blogs = await blogHandler.GetBlogsAsync(master.ID); Assert.AreEqual(blogs.Count, 10); foreach (var blog in blogs) { Assert.AreNotEqual(blog.PersonID, poko.ID); Assert.AreNotEqual(blog.Content, "TestContentByPokoOne"); Assert.AreNotEqual(blog.Content, "TestContentByPokoTwo"); Assert.AreNotEqual(blog.Content, "TestContentByPokoThree"); } //5. add group member to normal group and test it. GroupMember GroupMemberByPoko_Normal = new GroupMember() { GroupID = masterGroup.ID, PersonID = poko.ID }; gmHandler.Add(GroupMemberByMary); blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID); Assert.AreEqual(blogs.Count, 4); Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2); Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1); } }
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(); } }