Exemple #1
0
        public async Task test_adicionar_produto(
            int produtoId,
            int numeroComanda,
            bool temLimiteProduto,
            bool limiteExcedido,
            string mensagem,
            bool ok)
        {
            var controleComanda = new ControleComanda
            {
                NumeroComanda = numeroComanda,
                Produto_Id    = produtoId,
            };

            _mockLimiteProdutoRepositorio.Setup(s => s.GetAll())
            .Returns(temLimiteProduto ?
                     new List <LimiteProduto>
            {
                new LimiteProduto {
                    Produto_Id       = produtoId,
                    QuantidadeLimite = 1
                }
            } : new List <LimiteProduto>());

            _mockControleComandaRepositorio
            .Setup(s => s.GetAllByNumeroComanda(It.IsAny <int>()))
            .Returns(
                limiteExcedido ?
                new List <ControleComanda> {
                new ControleComanda {
                    Produto_Id = produtoId
                }
            }
                        : new List <ControleComanda>());

            _mockControleComandaRepositorio.Setup(s => s.Create(It.IsAny <ControleComanda>()))
            .Returns(Task.FromResult(1));

            var response = _comandaServico.AdicionarProduto(controleComanda);

            Assert.NotNull(response);
            Assert.Equal(response.Result.Ok, ok);
            Assert.Equal(response.Result.Mensagem, mensagem);

            _mockLimiteProdutoRepositorio.Verify(
                mock => mock.GetAll(),
                Times.Exactly(produtoId > 0 && numeroComanda > 0 ? 1 : 0));

            _mockControleComandaRepositorio.Verify(
                mock => mock.GetAllByNumeroComanda(It.IsAny <int>()),
                Times.Exactly(produtoId > 0 && numeroComanda > 0 && temLimiteProduto ? 1 : 0));

            _mockControleComandaRepositorio.Verify(
                mock => mock.Create(It.IsAny <ControleComanda>()),
                Times.Exactly(Convert.ToInt32(ok)));
        }
        public async Task <Retorno <SemConteudo> > AdicionarProduto(ControleComanda controleComanda)
        {
            if (controleComanda.Produto_Id == 0)
            {
                return(new Retorno <SemConteudo>
                {
                    Mensagem = "Informar o Id do Produto.",
                    Ok = false,
                });
            }

            if (controleComanda.NumeroComanda == 0)
            {
                return(new Retorno <SemConteudo>
                {
                    Mensagem = "Informar um número de comanda válido.",
                    Ok = false,
                });
            }

            var limites = _limiteProdutoRepositorio.GetAll().Where(w => w.Produto_Id == controleComanda.Produto_Id);

            if (limites.Any())
            {
                var quantidade = _controleComandaRepositorio.GetAllByNumeroComanda(controleComanda.NumeroComanda).Count(c => c.Produto_Id == controleComanda.Produto_Id);

                if (limites.FirstOrDefault().QuantidadeLimite == quantidade)
                {
                    return(new Retorno <SemConteudo>
                    {
                        Mensagem = $"Limite de produto excedido, não é permitido pedir mais desse item.",
                        Ok = false
                    });
                }
            }

            await _controleComandaRepositorio.Create(controleComanda);

            return(new Retorno <SemConteudo> {
                Ok = true
            });
        }