예제 #1
0
        public void DefaultsToQuantityOfOne()
        {
            var basket = new Basket();

            basket.AddItem(_testCatalogItemId, _testUnitPrice);

            var firstItem = basket.Items.Single();

            Assert.Equal(1, firstItem.Quantity);
        }
예제 #2
0
    public void ReturnsTotalQuantityWithOneItem()
    {
        var basket = new Basket(_buyerId);

        basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);

        var result = basket.TotalItems;

        Assert.Equal(_testQuantity, result);
    }
예제 #3
0
        public void RemoveItem_From_Basket_Test()
        {
            var basket = new Basket(_username);

            basket.AddItem(_itemId, _price);

            basket.RemoveItem(_itemId);

            Assert.Equal(0, basket.BasketItems.Count);
        }
예제 #4
0
        // [InlineData(10)]
        public async Task SetQuantityToZero_Removes_Item_From_Basket(
            int numInitiallItemsBasket, int numItemsToRemove
            )
        {
            if (numInitiallItemsBasket < numItemsToRemove)
            {
                throw new Exception();
            }
            var random    = new Random();
            var basketId  = 10;
            var basket    = new Basket();
            var itemPrice = 10;

            // CREATE INITIAL BASKET
            foreach (var itemId in Enumerable.Range(1, numInitiallItemsBasket))
            {
                  {
                    var initialQty = random.Next(1, 10);

                    basket.AddItem(itemId, itemPrice, initialQty);
                }
            }
            foreach (var item in  basket.Items)
            {
                item.Id = item.CatalogItemId;
            }
            // END INTIAL BASKET
            var initialItemsCount = basket.Items.Count;

            // targetItem.Id = itemId;
            _mockBasketRepo.Setup(
                x => x.GetByIdAsync(basketId)).ReturnsAsync(basket);

            var basketService = new BasketService(_mockBasketRepo.Object, null);
            // BEGIN DECIDE ITEMS TO REMOVE
            // var itemIdToRemove = random.Next(1, numInitiallItemsBasket);
            // var itemToRemove =  basket.Items.Where(item => item.Id == itemIdToRemove).First();
            var quantities = new System.Collections.Generic.Dictionary <string, int>();

            foreach (var itemToRemove in basket.Items.Take(numItemsToRemove))
            {
                quantities.Add(itemToRemove.Id.ToString(), 0);
            }
            // var numItemsToRemove = quantities.Count;
            // END DECIDE ITEMS TO REMOVE

            await basketService.SetQuantities(
                basketId,
                quantities);

            var expectedCount = initialItemsCount - numItemsToRemove;

            Assert.True(basket.Items.Count == expectedCount);
            _mockBasketRepo.Verify(x => x.UpdateAsync(basket), Times.Once());
        }
예제 #5
0
        public void WhenAnItemIsAdded_ThenBasketItemCountIncreases()
        {
            //Arrange
            var basket = new Basket();

            //Act
            basket.AddItem(Guid.NewGuid(), 1, 1);

            //Assert
            basket.Items.Count.Should().Be(1);
        }
예제 #6
0
        public ActionResult AddToBasketWithoutSize(Basket basket, int clothesID)
        {
            Product wear = _repository.Products.GetAll().Where(item => item.Article == clothesID).FirstOrDefault();

            if (wear != null)
            {
                basket.AddItem(wear, 1);
            }

            return(PartialView("_AddToBasketPartial", basket));
        }
예제 #7
0
        public void AddItem_WithNonExistentBasketItem_ShouldReturnNewlyCreatedItem()
        {
            // Arrange
            var basket = new Basket();

            // Act
            var item = basket.AddItem(1, 1, Pounds.Of(1));

            // Assert
            Assert.NotNull(item);
        }
예제 #8
0
        public void AddItem_WithNonExistentBasketItem_ShouldAddItemToList()
        {
            // Arrange
            var basket = new Basket();

            // Act
            basket.AddItem(1, 1, Pounds.Of(1));

            // Assert
            Assert.Equal(1, basket.Items.Count);
        }
예제 #9
0
        public void AfterFiveDiffrerentItemThereShoulBeFiveItemsInTheBasket()
        {
            var basket = new Basket();

            for (int i = 1; i < 6; i++)
            {
                basket.AddItem(i.ToString(), i, i, null);
            }

            basket.Items.Count.ShouldBe(5);
        }
예제 #10
0
        public RedirectToRouteResult AddOneToBasket(Basket basket, int Id, string returnUrl)
        {
            Product product = repository.Products
                              .FirstOrDefault(p => p.Id == Id);

            if (product != null)
            {
                basket.AddItem(product, 1);
            }
            return(RedirectToAction("Index", new { returnUrl }));
        }
예제 #11
0
        public void AppliesMilkDiscountFor8()
        {
            var basket = new Basket();

            basket.Id = _testBasketId;
            basket.AddItem(_testProductMilk);
            basket.ChangeItemQuantity(_testProductMilk, 8);
            basket.ApplyDiscount();

            Assert.AreEqual(6.90m, basket.Total());
        }
예제 #12
0
        public void should_be_able_to_create_a_basket_and_apply_offers()
        {
            BasketService basketService = BasketServiceSetup();

            var basket = new Basket();

            basket.AddItem(new Product()
            {
                Price = 0.80m,
                Name  = "Butter"
            }, 2);
            basket.AddItem(new Product()
            {
                Price = 1m,
                Name  = "Bread"
            }, 1);

            basketService.ApplyOffers(basket);
            Assert.Equal(2.10m, basket.Total);
        }
        public void Setup()
        {
            product    = fixture.Create <Product>();
            offersMock = fixture.Freeze <IList <Mock <IOffer> > >();
            productRepoMock.Setup(p => p.GetProductByName(It.IsAny <string>()))
            .Returns(() => product);


            resultTotals = fixture.CreateMany <double>(offersMock.Count).ToList();
            for (int i = 0; i < resultTotals.Count; i++)
            {
                offersMock[i].Setup(o => o.ApplyDiscount(It.IsAny <Basket>(), It.IsAny <double>()))
                .Returns(resultTotals[i]);
            }

            basket = fixture.Create <Basket>();
            basket.AddItem(fixture.Create <Product>(), fixture.Create <int>());
            basket.AddItem(fixture.Create <Product>(), fixture.Create <int>());
            basket.AddItem(fixture.Create <Product>(), fixture.Create <int>());
        }
예제 #14
0
        public RedirectToRouteResult AddToBasket(Basket basket, int bookId, string returnUrl)
        {
            Book book = repository.Books.FirstOrDefault(b => b.BookId == bookId);

            if (book != null)
            {
                basket.AddItem(book, 1);
            }

            return(RedirectToAction("Index", new { returnUrl }));
        }
예제 #15
0
        public void AddItem_To_Basket_With_Quantity_Test()
        {
            var basket = new Basket(_username);

            basket.AddItem(_itemId, _price, _quantity);
            var item = basket.BasketItems.First();

            Assert.Equal(_itemId, item.ItemId);
            Assert.Equal(_price, item.Price);
            Assert.Equal(_quantity, item.Quantity);
        }
예제 #16
0
        public void Save_added_items()
        {
            basketRepository = new Mock <IBasketRepository>();
            summaryPrinter   = new Mock <ISummaryPrinter>();
            var basket  = new Basket(basketRepository.Object, summaryPrinter.Object);
            var product = new Product("Bread", 1.50);
            var item    = new BasketItem(product, 1);

            basket.AddItem(item);

            basketRepository.Verify(x => x.AddItem(item));
        }
예제 #17
0
        public void PercentageOffPromotion_MultipleD_ReturnsTotalDiscountedPrice()
        {
            //Arrange
            var priceStrategies = new IItemPriceStrategy[]
            {
                new BasketItemSkuDItemPriceStrategy()
            };

            var basket = new Basket();

            basket.AddItem(new BasketItemSkuD());
            basket.AddItem(new BasketItemSkuD());

            var basketService = new BasketService(basket, priceStrategies);

            //Act
            var totalPrice = basketService.GetTotal();

            //Assert
            Assert.Equal((decimal)82.50, totalPrice);
        }
예제 #18
0
        public void AddsBasketItemIfNotPresent()
        {
            var basket = new Basket(_buyerId);

            basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);

            var firstItem = basket.Items.Single();

            Assert.Equal(_testCatalogItemId, firstItem.CatalogItemId);
            Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
            Assert.Equal(_testQuantity, firstItem.Quantity);
        }
예제 #19
0
        public void AddItem_SingleItemOnce_SingleLineWithQuantity1()
        {
            var basket = new Basket();

            basket.AddItem(_mockStockItemA);

            Assert.That(basket.LineItems.Count(), Is.EqualTo(1));
            var lineItem = basket.LineItems.Single();

            Assert.That(lineItem.Quantity, Is.EqualTo(1));
            Assert.That(lineItem.StockItem.SKU, Is.EqualTo(_mockStockItemA.SKU));
        }
예제 #20
0
        public void Given_IHaveSelectedToAddAnItemToTheBasket_ThenTheItemShouldBeAddedToTheBasket()
        {
            // Arrange
            var basket = new Basket();
            var item   = new Item(sku: 'A', unitPrice: 10);

            // Act
            basket.AddItem(item);

            // Assert
            Assert.AreEqual(expected: 1, actual: basket.ItemsCount);
        }
예제 #21
0
        public void ItemAdded()
        {
            //Arrange
            var basket     = new Basket();
            var basketItem = new BasketItemSkuA();

            //Act
            basket.AddItem(basketItem);

            //Assert
            Assert.NotEmpty(basket.Items);
        }
예제 #22
0
        public void WhenAProductsAreAddedAgainThenTheBasketContainsTheCorrectItems()
        {
            var basket = new Basket();

            basket.AddItem(new Product("Dove Soap", 39.99m), 5);
            basket.AddItem(new Product("Dove Soap", 39.99m), 3);

            basket.Items().Should().BeEquivalentTo(
                new[]
                {
                    new
                    {
                        Product = new
                        {
                            Name = "Dove Soap",
                            Price = 39.99m
                        },
                        Quantity = 8
                    }
                });
        }
예제 #23
0
        public void CalculatesPricesWithoutDiscounts()
        {
            var basket = new Basket();

            basket.Id = _testBasketId;
            basket.AddItem(_testProductButter);
            basket.ChangeItemQuantity(_testProductButter, 2);
            basket.AddItem(_testProductMilk);
            basket.ChangeItemQuantity(_testProductMilk, 8);
            basket.AddItem(_testProductBread);
            basket.ChangeItemQuantity(_testProductBread, 1);

            var basketItemButter = basket.Items.FirstOrDefault(i => i.Product.Name == "Butter");
            var basketItemMilk   = basket.Items.FirstOrDefault(i => i.Product.Name == "Milk");
            var basketItemBread  = basket.Items.FirstOrDefault(i => i.Product.Name == "Bread");

            Assert.AreEqual(1.60m, basketItemButter.Total);
            Assert.AreEqual(9.20m, basketItemMilk.Total);
            Assert.AreEqual(1m, basketItemBread.Total);
            Assert.AreEqual(11.80m, basket.Total());
        }
예제 #24
0
        public void TestQuantityIfOnlyOneExists()
        {
            var basket = new Basket();

            basket.AddItem(_testItemdId, _testQuantity, _testUnitPrice, _testName);

            var firstItem = basket.Items.Single();

            firstItem.Quantity.ShouldBe(_testQuantity);
            firstItem.Name.ShouldBe(_testName);
            firstItem.UnitPrice.ShouldBe(_testUnitPrice);
        }
예제 #25
0
        public void AddsBasketItemIfNotPresent()
        {
            Basket basket = new Basket();

            basket.AddItem(_productItemId, _testUnitPrice, _testQuantity);

            BasketItem firstItem = basket.Items.Single();

            firstItem.ProductItemId.Should().Be(_productItemId);
            firstItem.UnitPrice.Should().Be(_testUnitPrice);
            firstItem.Quantity.Should().Be(_testQuantity);
        }
예제 #26
0
        public void Given_IHaveSelectedToAddAnItemToTheBasket_Then_TheItemShouldBeAddedToTheBasket()
        {
            //Arrange
            var item   = new ItemA();
            var basket = new Basket();

            //Act
            basket.AddItem(item);

            //Assert
            Assert.AreEqual(1, basket.GetItems().Count);
        }
예제 #27
0
        public void ItemPresentInBasketCanBeRemoved()
        {
            var sut = new Basket();

            sut.AddItem(_catalogueItemId, _unitPrice, _quantity);

            sut.RemoveItem(_catalogueItemId);

            var item = sut.Items.FirstOrDefault();

            Assert.IsNull(item);
        }
예제 #28
0
        public ActionResult AddToBasketWithSize(Basket basket, int clothesID, string size)
        {
            Product wear     = _repository.Products.GetAll().Where(item => item.Article == clothesID).FirstOrDefault();
            Size    sizeName = wear.Sizes.First(x => x.SizeName == size);

            if (wear != null && sizeName != null)
            {
                basket.AddItem(wear, 1, size);
            }

            return(PartialView("_AddToBasketPartial", basket));
        }
예제 #29
0
        public void AddItem_AddTwoItemD_CorrectPrice()
        {
            // Arrange
            double expected = 82.5;
            Basket checkout = new Basket();
            Item   A        = new Item(0, 10, "none");
            Item   B        = new Item(0, 15, "3 for 40");
            Item   C        = new Item(0, 40, "none");
            Item   D        = new Item(0, 55, "25% off for every 2 purchased together");

            Item[] basket = { A, B, C, D };

            // Act
            checkout.AddItem(D, basket);
            checkout.AddItem(D, basket);

            // Assert
            double actual = checkout.basketTotal;

            Assert.AreEqual(expected, actual);
        }
예제 #30
0
        public void Quantity_in_Basket_cannot_be_negative()
        {
            var expectedItem = new BasketItem(new CatalogItem("Item 1", "1234item", 10.50), 5);

            var basket = new Basket(new List <BasketItem>());

            basket.AddItem(expectedItem);
            Assert.IsTrue(basket.ReturnItemQuantity(expectedItem.CatalogItem.ItemId) == 5);

            expectedItem.SetQuantity(100);
            basket.RemoveItem(expectedItem);
            Assert.IsTrue(basket.ReturnItemQuantity(expectedItem.CatalogItem.ItemId) == 0);
        }