Example #1
0
        public async Task Category_Delete()
        {
            var connection = TestHelper.GetConnection();
            var options    = TestHelper.GetMockDBOptions(connection);

            try
            {
                using (var context = new AppcentTodoContext(options))
                {
                    var service = new DeleteCategoryCommandHandler(context);
                    var command = new DeleteCategoryCommand();
                    command.Data = new DeleteCategoryRequest
                    {
                        CategoryId = 1,
                        UserName   = "******"
                    };
                    var result = await service.Execute(command);

                    Assert.True(result.Result.IsSuccess);
                }

                using (var context = new AppcentTodoContext(options))
                {
                    var task = context.AcCategories.FirstOrDefault(e => e.CategoryId == 1);
                    Assert.True(task.IsDeleted);
                }
            }
            finally
            {
                connection.Close();
            }
        }
Example #2
0
        public async Task GivenValidCategoryWithPost_ShouldThrowDeleteFailureException()
        {
            // Arrange
            var sut      = new DeleteCategoryCommandHandler(_context);
            var category = new Category("Delete Category 1");

            _context.Categories.Add(category);
            _context.SaveChanges();

            var post = new Post("Post 1", "Description", "Content", category.Id, null, true);

            _context.Posts.Add(post);
            _context.SaveChanges();

            // Act
            var ex = await Assert.ThrowsAsync <DeleteFailureException>(() => sut.Handle(new DeleteCategoryCommand
            {
                Id = category.Id
            },
                                                                                        CancellationToken.None));

            // Assert
            Assert.IsType <DeleteFailureException>(ex);
            Assert.NotNull(category);
        }
Example #3
0
        public void ShouldNotCallHandleIfCategoryNotExist()
        {
            dbSetCategory.Setup(x => x.FindAsync(id)).Returns(null);
            context.Setup(x => x.Categories).Returns(dbSetCategory.Object);

            DeleteCategoryCommandHandler deleteCategoryCommandHandler = new DeleteCategoryCommandHandler(context.Object, stringLocalizer.Object);
            DeleteCategoryCommand        deleteCategoryCommand        = new DeleteCategoryCommand(id);

            Func <Task> act = async() => await deleteCategoryCommandHandler.Handle(deleteCategoryCommand, new CancellationToken());

            act.Should().ThrowAsync <NotFoundException>();
        }
Example #4
0
        public void DeleteCategoryCommandHandler_NotExisting_NoChange()
        {
            //given
            var repository = LiteDbHelper.CreateMemoryDb();
            var handler    = new DeleteCategoryCommandHandler(repository);
            var command    = new DeleteCategoryCommand(new Category());

            //when
            handler.Execute(command);

            //then
            Assert.Empty(repository.Database.Query <Category>());
        }
Example #5
0
        public async Task DeleteCategoryHandler_Deletes_Category()
        {
            var category = await GetRandomCategory();

            var message = new DeleteCategoryCommand()
            {
                Id = category.Id
            };
            var handler = new DeleteCategoryCommandHandler(RequestDbContext);

            var result = await handler.Handle(message, CancellationToken.None);

            Assert.IsType <SuccessResult>(result);
        }
Example #6
0
        public async Task Delete_Category()
        {
            using (var context = GetContextWithData())
            {
                var handler = new DeleteCategoryCommandHandler(context);
                var command = new DeleteCategoryCommand
                {
                    Id = (await context.Categories.Skip(6).Take(1).FirstOrDefaultAsync()).Id
                };

                await handler.Handle(command, CancellationToken.None);

                Assert.Null(await context.Categories.FindAsync(command.Id));
            }
        }
Example #7
0
        public void ShouldNotCallHandleIfNotSavedChanges()
        {
            dbSetCategory.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Category>(Task.FromResult(new Category {
                Id = id
            })));
            context.Setup(x => x.Categories).Returns(dbSetCategory.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0));

            DeleteCategoryCommandHandler deleteCategoryCommandHandler = new DeleteCategoryCommandHandler(context.Object, stringLocalizer.Object);
            DeleteCategoryCommand        deleteCategoryCommand        = new DeleteCategoryCommand(id);

            Func <Task> act = async() => await deleteCategoryCommandHandler.Handle(deleteCategoryCommand, new CancellationToken());

            act.Should().ThrowAsync <RestException>();
        }
Example #8
0
        public async Task ShouldCallHandle()
        {
            dbSetCategory.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Category>(Task.FromResult(new Category {
                Id = id
            })));
            context.Setup(x => x.Categories).Returns(dbSetCategory.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1));

            DeleteCategoryCommandHandler deleteCategoryCommandHandler = new DeleteCategoryCommandHandler(context.Object, stringLocalizer.Object);
            DeleteCategoryCommand        deleteCategoryCommand        = new DeleteCategoryCommand(id);

            var result = await deleteCategoryCommandHandler.Handle(deleteCategoryCommand, new CancellationToken());

            result.Should().Be(Unit.Value);
        }
Example #9
0
        public async Task GivenInvalidRequest_ShouldThrowNotFoundException()
        {
            // Arrange
            var sut        = new DeleteCategoryCommandHandler(_context);
            var categoryId = 10;

            // Act
            var ex = await Assert.ThrowsAsync <NotFoundException>(() => sut.Handle(new DeleteCategoryCommand
            {
                Id = categoryId
            },
                                                                                   CancellationToken.None));

            // Assert
            Assert.IsType <NotFoundException>(ex);
        }
Example #10
0
        public async Task Handle_DeletingCategory_ShouldDeleteCategory()
        {
            // Arrange
            var categoryRepositoryMock = GetCategoryRepository();

            var handler = new DeleteCategoryCommandHandler(categoryRepositoryMock.Object);
            var request = new DeleteCategoryCommand
            {
                CategoryId = 1
            };

            // Act
            await handler.Handle(request, CancellationToken.None);

            // Assert
            categoryRepositoryMock.Verify(x => x.DeleteCategory(1), Times.Once);
        }
Example #11
0
        public async Task DeleteCategoryCommandTestAsync(string identityUserId, int categoryId, DeleteCategoryResponse result)
        {
            DeleteCategoryCommand request = new DeleteCategoryCommand
            {
                IdentityUserId = identityUserId,
                CategoryId     = categoryId,
            };
            DeleteCategoryCommandHandler handler = new DeleteCategoryCommandHandler(_deleteFixture.Context);
            var expectedResult = await handler.Handle(request, new CancellationToken());

            Assert.Equal(expectedResult.IsSuccessful, result.IsSuccessful);
            if (expectedResult.IsSuccessful)
            {
                Category category = await _deleteFixture.Context.Categories.FindAsync(expectedResult.Id);

                Assert.Null(category);
            }
            Assert.Equal(expectedResult.Message, result.Message);
        }
Example #12
0
        public void DeleteCategoryCommandHandler_MoreObjects_RemovedProperOne()
        {
            //given
            var targetCategory = new Category();
            var categories     = new[] { targetCategory, new Category(), new Category(), new Category() };
            var repository     = LiteDbHelper.CreateMemoryDb();
            var handler        = new DeleteCategoryCommandHandler(repository);
            var command        = new DeleteCategoryCommand(targetCategory);

            repository.Database.UpsertBulk(categories);

            //when
            handler.Execute(command);

            //then
            categories = categories.Skip(1).OrderBy(x => x.Id).ToArray();
            var actualCategories = repository.Database.Query <Category>().OrderBy(x => x.Id).ToArray();

            Assert.Equal(categories.Length, actualCategories.Length);
            Assert.Equal(categories, actualCategories);
        }
Example #13
0
        public void GivenValidRequest_ShouldSuccessfullyDeleteCategory()
        {
            // Arrange
            var sut    = new DeleteCategoryCommandHandler(_context);
            var entity = new Category("Delete Category 1");

            _context.Categories.Add(entity);
            _context.SaveChanges();

            // Act
            var result = sut.Handle(new DeleteCategoryCommand
            {
                Id = entity.Id
            },
                                    CancellationToken.None);
            var category = _context.Categories.FirstOrDefault(i => i.Id == entity.Id);

            // Assert
            Assert.IsType <Unit>(result.Result);
            Assert.Null(category);
        }
 public DeleteCategoryCommandTests() : base()
 {
     _handler = new DeleteCategoryCommandHandler(_context);
 }