Ejemplo n.º 1
0
        private async Task <Guid> AddDiscountOrRuleAsync(Store store, string username, Guid storeId, Guid discountId1, Guid discountId2, Guid originalDiscountId = new Guid())
        {
            var rule1    = store.GetRuleByDiscountId(discountId1);
            var rule2    = store.GetRuleByDiscountId(discountId2);
            var orRule   = Rule.OrTwoRules(rule1, rule2);
            var discount = new ConditionDiscount(store.GetDiscountById(discountId1).Calc, orRule);

            if (!originalDiscountId.Equals(new Guid()))
            {
                discount.Id = originalDiscountId;
            }
            return(store.AddDiscount(username, discount));
        }
        public async Task CheckLegalPurcahseWithDiscount()
        {
            await MarketUsers.Instance.AddProductToCart(testUser.Username, product.id, 5);

            ConditionDiscount discount = new ConditionDiscount(new DiscountCalculator(return15));
            IRule             rule     = new Rule(CheckTotalWeightMoreThan400);

            discount.AddRule(rule);
            testStore.AddDiscount(testStore.GetFounder().Username, discount);
            Assert.AreEqual(PRICE1 * 5 * (1 - (DISCOUNT_VALUE / 100)), testUser.ShoppingCart.CalcPaySum());
            var v1 = await testUser.PurchaseShoppingCart(testUserCreditCard, "0544444444", testUserAddress);

            Assert.IsTrue(!v1.IsErr);
        }
        public void CheckAndDiscounts()
        {
            ConditionDiscount discount = new ConditionDiscount(discountCalc1.Object);
            Mock <IRule>      rule1    = new Mock <IRule>();

            rule1.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(true);
            discount.AddRule(rule1.Object);
            Mock <IRule> rule2 = new Mock <IRule>();

            rule2.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(true);
            ConditionDiscount addedDiscount = discount.And(rule2.Object);

            Assert.AreEqual(15, addedDiscount.ApplyDiscounts(shoppingBasket).Discount);
        }
        public void CheckSomeRulesFalse()
        {
            Mock <IRule> rule1 = new Mock <IRule>();

            rule1.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(true);
            Mock <IRule> rule2 = new Mock <IRule>();

            rule2.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(false);
            ConditionDiscount discount = new ConditionDiscount(discountCalc1.Object);

            discount.AddRule(rule1.Object);
            discount.AddRule(rule2.Object);
            Assert.AreEqual(false, discount.Available(shoppingBasket));
        }
        public void CheckOrDiscountsBothMyAndAddedRulesFalse()
        {
            ConditionDiscount discount = new ConditionDiscount(discountCalc1.Object);
            Mock <IRule>      rule1    = new Mock <IRule>();

            rule1.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(false);
            discount.AddRule(rule1.Object);
            Mock <IRule> rule2 = new Mock <IRule>();

            rule2.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(false);
            ConditionDiscount orDiscount = discount.Or(rule2.Object);

            Assert.AreEqual(0, orDiscount.ApplyDiscounts(shoppingBasket).Discount);
        }
Ejemplo n.º 6
0
        public async Task CheckLegalPurcahseWithDiscountPriceRemains()
        {
            await testUser.UpdateProductInShoppingBasket(testStore, product, 5);

            testStore.UpdateProduct(product);
            ConditionDiscount discount = new ConditionDiscount(new DiscountCalculator(return15));
            IRule             rule     = new Rule(CheckTotalWeightMoreThan400);

            discount.AddRule(rule);
            testStore.AddDiscount(testStore.GetFounder().Username, discount);
            var v1 = await testUser.PurchaseShoppingCart(testUserCreditCard, "0544444444", testUserAddress);

            Assert.AreEqual(PRICE1, product.Price);
            Assert.IsTrue(!v1.IsErr);
        }
        public void CheckXorDiscountsBothFalse()
        {
            ConditionDiscount discount1 = new ConditionDiscount(discountCalc1.Object);
            ConditionDiscount discount2 = new ConditionDiscount(discountCalc2.Object);
            Mock <IRule>      rule1     = new Mock <IRule>();

            rule1.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(false);
            discount1.AddRule(rule1.Object);
            Mock <IRule> rule2 = new Mock <IRule>();

            rule2.Setup(r => r.Check(It.IsAny <ShoppingBasket>())).Returns(false);
            discount2.AddRule(rule2.Object);
            ConditionDiscount xorDiscount = discount1.Xor(discount2, true);

            Assert.AreEqual(0, xorDiscount.ApplyDiscounts(shoppingBasket).Discount);
        }
Ejemplo n.º 8
0
        public async System.Threading.Tasks.Task <Guid> CreateConditionalDiscountAsync(Store store, string username, Guid storeId, RuleContext discountType, RuleType ruleType, double precent, string category = "", Guid productId = new Guid(),
                                                                                       double valueLessThan = int.MaxValue, double valueGreaterEQThan = 0, DateTime d1 = new DateTime(), DateTime d2 = new DateTime(), Guid originalDiscountId = new Guid())
        {
            if (store == null)
            {
                return(new Guid());
            }
            var d = CreateCalculator(discountType, precent, category, productId);
            var r = CreateRule(discountType, ruleType, category, productId, valueLessThan, valueGreaterEQThan, d1, d2);
            ConditionDiscount discount = new ConditionDiscount(d);

            if (!originalDiscountId.Equals(new Guid()))
            {
                discount.Id = originalDiscountId;
            }
            discount.AddRule(r);
            return(store.AddDiscount(username, discount));
        }
Ejemplo n.º 9
0
        public void ApplyTwoRelevantDiscounts()
        {
            shoppingBasket.UpdateProduct(product1, 30);
            Address    address = new Address("1", "1", "1", "1", "1");
            CreditCard card    = new CreditCard("1", "1", "1", "1", "1", "1");
            Store      store   = new Store("testStore", card, address);

            store.SetFounder(Founder.makeFounder(new MemberState("Founder"), store));
            store.UpdateProduct(product1);
            ConditionDiscount discount1 = new ConditionDiscount(new DiscountCalculator(return10));

            discount1.AddRule(new Rule(MoreThan10Products));
            ConditionDiscount discount2 = new ConditionDiscount(new DiscountCalculator(return20));

            discount2.AddRule(new Rule(MoreThan20Products));
            store.AddDiscount(store.Founder.Username, discount1);
            store.AddDiscount(store.Founder.Username, discount2);
            Assert.AreEqual(0.2 * product1.Price, store.ApplyDiscounts(shoppingBasket));
        }
Ejemplo n.º 10
0
        private async Task <Guid> AddDiscountAndRuleAsync(string username, Guid storeId, Guid discountId1, Guid discountId2, Guid originalDiscountId = new Guid())
        {
            Store store = await marketStores.GetStoreById(storeId);

            if (store == null)
            {
                return(new Guid());
            }
            var rule1    = store.GetRuleByDiscountId(discountId1);
            var rule2    = store.GetRuleByDiscountId(discountId2);
            var andRule  = Rule.AddTwoRules(rule1, rule2);
            var discount = new ConditionDiscount(store.GetDiscountById(discountId1).Calc, andRule);

            if (!originalDiscountId.Equals(new Guid()))
            {
                discount.Id = originalDiscountId;
            }
            return(store.AddDiscount(username, discount));
        }
        public void CheckApplyDiscounts()
        {
            ConditionDiscount discount = new ConditionDiscount(discountCalc1.Object);

            Assert.AreEqual(15, discount.ApplyDiscounts(shoppingBasket).Discount);
        }