Esempio n. 1
0
        public async Task ShouldFailToRemoveFoodTableIdNotFound()
        {
            RemoveFoodTableCommand command = new RemoveFoodTableCommand
            {
                FoodTableId = Guid.NewGuid()
            };

            Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>();

            foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ReturnsAsync(() => null);

            FoodTableCommandHandler handler = new FoodTableCommandHandler(
                foodTableRepository.Object,
                GetIdentityService(),
                GetMediator(),
                GetUnitOfWork(),
                GetLogger()
                );

            CommandResult commandResult = await handler.Handle(command, default(CancellationToken));

            Assert.IsFalse(commandResult.Success);
            Assert.AreEqual("Nenhuma categoria de alimentos com esse Id foi encontrada no banco de dados.", commandResult.Notifications.FirstOrDefault().Description);
        }
Esempio n. 2
0
        public async Task <IActionResult> RemoveAsync(Guid id)
        {
            RemoveFoodTableCommand command = new RemoveFoodTableCommand
            {
                FoodTableId = id,
            };

            return(await CreateCommandResponse(command));
        }
        public async Task <CommandResult> Handle(RemoveFoodTableCommand request, CancellationToken cancellationToken)
        {
            CustomFoodTable customFoodTable = await _foodTableRepository.GetCustomByIdAsync(request.FoodTableId, _currentProfileId);

            if (customFoodTable == null)
            {
                return(FailureDueToCustomFoodTableNotFound());
            }

            await _foodTableRepository.RemoveAsync(customFoodTable);

            return(await CommitAndPublishDefaultAsync());
        }
Esempio n. 4
0
        public async Task ShouldRemoveFoodTable()
        {
            Guid            profileId         = Guid.NewGuid();
            CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes");
            bool            isRemoved         = false;

            Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>();

            foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ReturnsAsync(() =>
            {
                if (!isRemoved)
                {
                    return(existingFoodTable);
                }

                return(null);
            });

            foodTableRepository.Setup(repo => repo.RemoveAsync(It.IsAny <CustomFoodTable>()))
            .Callback((CustomFoodTable foodTable) =>
            {
                isRemoved = true;
            });

            RemoveFoodTableCommand command = new RemoveFoodTableCommand
            {
                FoodTableId = existingFoodTable.Id,
                Name        = "Bacon após testes",
                Description = "Registrando um bacon de testes",
            };

            CustomFoodTable insertedFoodTable = await foodTableRepository.Object.GetCustomByIdAsync(existingFoodTable.Id, profileId);

            Assert.AreEqual("Bacon inicial", insertedFoodTable.Name);

            FoodTableCommandHandler handler = new FoodTableCommandHandler(
                foodTableRepository.Object,
                GetIdentityService(profileId),
                GetMediator(),
                GetUnitOfWork(),
                GetLogger()
                );

            CommandResult commandResult = await handler.Handle(command, default(CancellationToken));

            CustomFoodTable updatedInsertedFoodTable = await foodTableRepository.Object.GetCustomByIdAsync(Guid.NewGuid(), profileId);

            Assert.IsTrue(commandResult.Success);
            Assert.IsNull(updatedInsertedFoodTable);
        }
Esempio n. 5
0
        public async Task ShouldFailToRemoveFoodTableUserNotOwner()
        {
            Guid            profileId         = Guid.NewGuid();
            CustomFoodTable existingFoodTable = new CustomFoodTable(profileId, "Bacon inicial", "Bacon antes de sofrer os testes");

            RemoveFoodTableCommand command = new RemoveFoodTableCommand
            {
                FoodTableId = existingFoodTable.Id
            };

            Mock <IFoodTableRepository> foodTableRepository = new Mock <IFoodTableRepository>();

            foodTableRepository.Setup(repo => repo.GetCustomByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ReturnsAsync((Guid id, Guid pid) =>
            {
                if (pid == existingFoodTable.ProfileId && id == existingFoodTable.Id)
                {
                    return(existingFoodTable);
                }

                return(null);
            });

            FoodTableCommandHandler handler = new FoodTableCommandHandler(
                foodTableRepository.Object,
                GetIdentityService(),
                GetMediator(),
                GetUnitOfWork(),
                GetLogger()
                );

            CommandResult commandResult = await handler.Handle(command, default(CancellationToken));

            Assert.IsFalse(commandResult.Success);
            Assert.AreEqual("Nenhuma categoria de alimentos com esse Id foi encontrada no banco de dados.", commandResult.Notifications.FirstOrDefault().Description);
        }