Example #1
0
        public void CanApplyDiscount()
        {
            // Setup mock repositoris
            var mockCouponRepo = new Mock<IRepository<Coupon>>();
            mockCouponRepo.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new Coupon[]
                {
                    new Coupon {CouponId = 1, Threshold = 20M, Amount = 2M }
                }.AsQueryable());

            // Scenario 01: Apply bulk discount on quantity item
            // Bulk discount for a quantity item: Buy one Get the second one 50%
            var mockDiscountRepo01 = new Mock<IRepository<Discount>>();
            mockDiscountRepo01.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new Discount[]
                {
                    new Discount { DiscountPercentage = 50, DiscountQuantity = 1, DiscountAmount = 1 }
                }.AsQueryable());

            var myCart = new Cart(mockDiscountRepo01.Object, mockCouponRepo.Object);
            myCart.AddQuantityItem(new Product { ProductId = 1, Category = "Vegetable", Description = "Small Bag of carrots", Name = "Carrots_100gr", PricePerUnit = 4.99M, Quantitative = true }, 2);
            myCart.CalculateTotalPrice();

            // Check the discounted price
            Assert.AreEqual(7.485M, myCart.TotalPrice, "The calculated discounted price is not correct.");
            Assert.AreEqual(9.98M, myCart.GrossTotal, "The calculated gross total after applying coupon is not correct.");

            // Add a same item
            myCart.AddQuantityItem(new Product { ProductId = 1, Category = "Vegetable", Description = "Small Bag of carrots", Name = "Carrots_100gr", PricePerUnit = 4.99M, Quantitative = true }, 1);
            myCart.CalculateTotalPrice();

            // Check the discounted price
            Assert.AreEqual(12.475M, myCart.TotalPrice, "The calculated discounted price is not correct.");
            Assert.AreEqual(14.97M, myCart.GrossTotal, "The calculated gross total after applying coupon is not correct.");

            // Scenario 02: Apply bulk discount on quantity item
            // Bulk discount for a quantity item: Buy two Get the third one free
            var mockDiscountRepo02 = new Mock<IRepository<Discount>>();
            mockDiscountRepo02.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new Discount[]
                {
                    new Discount { DiscountPercentage = 100, DiscountQuantity = 2, DiscountAmount = 1 }
                }.AsQueryable());

            myCart = new Cart(mockDiscountRepo02.Object, mockCouponRepo.Object);
            myCart.AddQuantityItem(new Product { ProductId = 1, Category = "Vegetable", Description = "Small Bag of carrots", Name = "Carrots_100gr", PricePerUnit = 4.99M, Quantitative = true }, 5);
            myCart.CalculateTotalPrice();

            // Check the discounted price
            Assert.AreEqual(19.96M, myCart.TotalPrice, "The calculated discounted price is not correct.");
            Assert.AreEqual(24.95M, myCart.GrossTotal, "The calculated gross total after applying coupon is not correct.");

            // Scenario 03: Put a quantity item on sale
            // Percentage discount for a quantity item: Item is 30% off
            var mockDiscountRepo03 = new Mock<IRepository<Discount>>();
            mockDiscountRepo03.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new Discount[]
                {
                    new Discount { DiscountPercentage = 30, DiscountQuantity = 0, DiscountAmount = 0 }
                }.AsQueryable());

            myCart = new Cart(mockDiscountRepo03.Object, mockCouponRepo.Object);
            myCart.AddQuantityItem(new Product { ProductId = 1, Category = "Vegetable", Description = "Small Bag of carrots", Name = "Carrots_100gr", PricePerUnit = 4.99M, Quantitative = true }, 3);
            myCart.CalculateTotalPrice();

            // Check the discounted price
            Assert.AreEqual(10.479M, myCart.TotalPrice, "The calculated discounted price is not correct.");
            Assert.AreEqual(14.97M, myCart.GrossTotal, "The calculated gross total after applying coupon is not correct.");

            // Scenario 04: Put a weight item on sale
            // Percentage discount for a quantity item: Item is 25% off
            var mockDiscountRepo04 = new Mock<IRepository<Discount>>();
            mockDiscountRepo04.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new Discount[]
                {
                    new Discount { DiscountPercentage = 25, DiscountQuantity = 0, DiscountAmount = 0 }
                }.AsQueryable());

            myCart = new Cart(mockDiscountRepo04.Object, mockCouponRepo.Object);
            myCart.AddWeightItem(new Product { ProductId = 1, Category = "Meet", Description = "Extra Lean", Name = "Beef", PricePerUnit = 14.99M, Unit = "kg", Quantitative = false }, 2000);
            myCart.CalculateTotalPrice();

            // Check the discounted price
            Assert.AreEqual(22.485M, myCart.TotalPrice, "The calculated discounted price is not correct.");
            Assert.AreEqual(29.98M, myCart.GrossTotal, "The calculated gross total after applying coupon is not correct.");

            // Scenario 05: Apply discount and coupon together
            myCart.ApplyCoupon(1);

            // Check the discounted price
            Assert.AreEqual(20.485M, myCart.TotalPrice, "The calculated discounted price is not correct.");
            Assert.AreEqual(29.98M, myCart.GrossTotal, "The calculated gross total after applying coupon is not correct.");
        }
Example #2
0
        private static Cart CreateSimpleCart()
        {
            // Setup mock repositoris
            var mockDiscountRepo = new Mock<IRepository<Discount>>();
            mockDiscountRepo.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new Discount[] {}.AsQueryable());

            var mockCouponRepo = new Mock<IRepository<Coupon>>();
            mockCouponRepo.Setup(m => m.Get(It.IsAny<Filter>())).Returns(new[]
                {
                    new Coupon {CouponId = 1, Threshold = 15M, Amount = 2M }
                }.AsQueryable());

            var simpleCart = new Cart(mockDiscountRepo.Object, mockCouponRepo.Object);

            // Add items to the cart
            simpleCart.AddQuantityItem(new Product { ProductId = 1, Category = "Vegetable", Description = "Small Bag of carrots", Name = "Carrots_100gr", PricePerUnit = 4.99M, Quantitative = true }, 3);
            simpleCart.AddWeightItem(new Product { ProductId = 2, Category = "Fruite", Description = "Doll", Name = "Banana", PricePerUnit = 0.99M, Unit = "lbs", Quantitative = false }, 907.18);
            simpleCart.AddQuantityItem(new Product { ProductId = 1, Category = "Vegetable", Description = "Small Bag of carrots", Name = "Carrots_100gr", PricePerUnit = 4.99M, Quantitative = true }, 1);

            return simpleCart;
        }