Exemplo n.º 1
0
        public void Buy_With_Discount_Returns_TotalAmount()
        {
            // Arrange

            this._context.Catalog = new List <Book> {
                new Book {
                    Name = "J.K Rowling - Goblet Of fire", Category = "Fantastique", Price = 8, Quantity = 2
                },
                new Book {
                    Name = "Isaac Asimov - Foundation", Category = "Science Fiction", Price = 16, Quantity = 1
                },
            };

            this._context.Category = new List <CategoryWithDiscount> {
                new CategoryWithDiscount {
                    Name = "Fantastique", Discount = 0.1
                },
                new CategoryWithDiscount {
                    Name = "Science Fiction", Discount = 0.05
                }
            };

            var service = new StoreService(this._context);

            // Act
            var total = service.Buy("J.K Rowling - Goblet Of fire", "J.K Rowling - Goblet Of fire", "Isaac Asimov - Foundation");

            // Assert

            // 8*0.9 + 8 + 16
            Assert.Equal(31.20, total);
        }
Exemplo n.º 2
0
        public void Buy_Filters_BasketNames()
        {
            this._context.Catalog = new List <Book> {
                new Book {
                    Name = "J.K Rowling - Goblet Of fire", Category = "Fantastique", Price = 8, Quantity = 2
                },
                new Book {
                    Name = "Isaac Asimov - Foundation", Category = "Science Fiction", Price = 16, Quantity = 1
                },
            };

            this._context.Category = new List <CategoryWithDiscount> {
                new CategoryWithDiscount {
                    Name = "Fantastique", Discount = 0.1
                },
                new CategoryWithDiscount {
                    Name = "Science Fiction", Discount = 0.05
                }
            };

            var service = new StoreService(this._context);

            // Act
            var total = service.Buy(null, "", "J.K Rowling - Goblet Of fire");

            // Assert
            Assert.Equal(8.00, total);
        }
Exemplo n.º 3
0
        public void Buy_Invalid_Product_Throws_BookNotFoundException()
        {
            // Arrange
            var service = new StoreService(this._context);

            // Act

            // Assert
            Assert.Throws <NotEnoughInventoryException>(() => service.Buy("csharp"));
        }
Exemplo n.º 4
0
        public void Buy_ShouldReturnPriceOfBasket_WhenListOfBookNamesIsGiven(string[] bookNames, double expectedPriceOfBasket)
        {
            // Arrange
            StoreService storeService = new StoreService();

            storeService.Import(VALID_JSON_STRING, out string errorMessage);

            // Act
            double basketPrice = storeService.Buy(bookNames);

            // Assert
            Assert.Equal(expectedPriceOfBasket, basketPrice);
        }
Exemplo n.º 5
0
        public void Import_Then_Buy_Returns_TotalAmount_Ex1()
        {
            // Arrange
            var service = new StoreService(this._context);

            // Act
            service.Import(this._catalogAsString);

            var total = service.Buy("J.K Rowling - Goblet Of fire",
                                    "Robin Hobb - Assassin Apprentice",
                                    "Robin Hobb - Assassin Apprentice");

            // Assert

            Assert.Equal(30.0, total);
        }
Exemplo n.º 6
0
        public ActionResult Buy(int productId, int number)
        {
            var service = new StoreService();

            try
            {
                var product = service.GetProduct(productId);
                if (product != null)
                {
                    service.Buy(productId, number);
                }
            }
            catch (Exception ex) {
                //return error or redirecto error page
            }

            return(PartialView("~/Views/Partials/_ProductTable.cshtml"));
        }
Exemplo n.º 7
0
        public void Import_Then_Buy_Returns_TotalAmount_Ex2()
        {
            // Arrange
            var service = new StoreService(this._context);

            // Act
            service.Import(this._catalogAsString);

            var total = service.Buy("Ayn Rand - FountainHead",
                                    "Isaac Asimov - Foundation",
                                    "Isaac Asimov - Robot series",
                                    "J.K Rowling - Goblet Of fire",
                                    "J.K Rowling - Goblet Of fire",
                                    "Robin Hobb - Assassin Apprentice",
                                    "Robin Hobb - Assassin Apprentice");

            // Assert

            Assert.Equal(69.95, total);
        }
Exemplo n.º 8
0
        public IActionResult Buy()
        {
            var currentUser = user.FindByNameAsync(User.Identity.Name);

            return(Ok(service.Buy(currentUser.Result.Id)));
        }
Exemplo n.º 9
0
        public void Buy_ShouldThrowExceptionWithListOfBookNotAvailable_IfQuantityIsNotEnough()
        {
            // Arrange
            string[] bookNames = new string[] { "Isaac Asimov - Foundation", "Isaac Asimov - Foundation" };

            StoreService storeService = new StoreService();

            storeService.Import(VALID_JSON_STRING, out string errorMessage);

            //Act
            NotEnoughInventoryException ex = Assert.Throws <NotEnoughInventoryException>(() => storeService.Buy(bookNames));

            // Assert
            Assert.NotEmpty(ex.Missing);
            Assert.Single(ex.Missing);
            Assert.Equal(bookNames[0], ex.Missing.FirstOrDefault().Name);
            Assert.NotNull(ex.Message);
            Assert.Equal("NotEnoughInventoryException", ex.Message);
        }