예제 #1
0
        public async Task Should_delete_reply_and_add_event()
        {
            var options = Shared.CreateContextOptions();

            var siteId     = Guid.NewGuid();
            var categoryId = Guid.NewGuid();
            var forumId    = Guid.NewGuid();
            var topicId    = Guid.NewGuid();
            var userId     = Guid.NewGuid();

            var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid());
            var forum    = new Forum(forumId, categoryId, "Forum", "my-forum", "My Forum", 1);
            var topic    = Post.CreateTopic(topicId, forumId, Guid.NewGuid(), "Title", "slug", "Content", StatusType.Published);
            var reply    = Post.CreateReply(Guid.NewGuid(), topicId, forumId, userId, "Content", StatusType.Published);
            var user     = new User(userId, Guid.NewGuid().ToString(), "Email", "Display Name");

            category.IncreaseRepliesCount();
            forum.IncreaseRepliesCount();
            topic.IncreaseRepliesCount();
            user.IncreaseRepliesCount();

            using (var dbContext = new AtlasDbContext(options))
            {
                dbContext.Categories.Add(category);
                dbContext.Forums.Add(forum);
                dbContext.Posts.Add(topic);
                dbContext.Posts.Add(reply);
                dbContext.Users.Add(user);
                await dbContext.SaveChangesAsync();
            }

            using (var dbContext = new AtlasDbContext(options))
            {
                var command = Fixture.Build <DeleteReply>()
                              .With(x => x.Id, reply.Id)
                              .With(x => x.SiteId, siteId)
                              .With(x => x.ForumId, forumId)
                              .With(x => x.TopicId, topicId)
                              .Create();

                var cacheManager    = new Mock <ICacheManager>();
                var createValidator = new Mock <IValidator <CreateReply> >();
                var updateValidator = new Mock <IValidator <UpdateReply> >();

                var sut = new ReplyService(dbContext,
                                           cacheManager.Object,
                                           createValidator.Object,
                                           updateValidator.Object);

                await sut.DeleteAsync(command);

                var replyDeleted = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == reply.Id);

                var replyEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == reply.Id);

                var updatedCategory = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category.Id);

                var updatedForum = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum.Id);

                var updatedTopic = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == topic.Id);

                var updatedUser = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == user.Id);

                Assert.AreEqual(StatusType.Deleted, replyDeleted.Status);
                Assert.NotNull(replyEvent);
                Assert.AreEqual(0, updatedCategory.RepliesCount);
                Assert.AreEqual(0, updatedForum.RepliesCount);
                Assert.AreEqual(0, updatedTopic.RepliesCount);
                Assert.AreEqual(0, updatedUser.RepliesCount);
            }
        }