def test_has_store_discount(self): discount = DiscountService() start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) self.assertFalse(discount.has_store_discount()) discount.store_discount = ('StoreDiscount', 70, True, start_date, end_date) self.assertTrue(discount.has_store_discount())
def test_add_store_discount(self): discount = DiscountService() start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) self.assertEqual(discount.store_discount, None) discount.store_discount = ('StoreDiscount', 70, True, start_date, end_date) self.assertTrue( discount.store_discount[discount.DISCOUNT_TYPE] == 'StoreDiscount') self.assertEqual(discount.store_discount[discount.DISCOUNT_PERCENTAGE], 70) self.assertTrue(discount.store_discount[discount.DOUBLE_DEALS])
def test_check_conditional_discount(self): discount = DiscountService() mango = Product('mango', 'fruit', ['yellow', 'sweet'], 7) pear = Product('pear', 'fruit', ['green', 'sweet'], 4) shopping_cart = ShoppingCart(Store('effect', 123, 123, None, None)) shopping_cart.add_product(mango) self.assertFalse( discount.check_conditional_discount(('And', pear, mango), shopping_cart)) shopping_cart.add_product(pear) self.assertTrue( discount.check_conditional_discount(('And', pear, mango), shopping_cart))
def test_calculate_individual_discount(self): discount = DiscountService() apple = Product('apple', 'fruit', ['green', 'sweet'], 5) shopping_cart = ShoppingCart(Store('effect', 123, 123, None, None)) start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) discount.add_product_discount(apple, 20, True, start_date, end_date, coupon_code=123) x = discount.calculate_individual_discount( discount.product_discount_table[apple.catalog_number] ['CouponDiscount'], shopping_cart) self.assertEqual(x[0], 20) self.assertEqual(x[1], 'CouponDiscount')
def test_remove_cart(self): basket = ShoppingBasket() discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) cart = ShoppingCart(store) basket.shopping_carts.append(cart) basket.remove_cart(cart) self.assertEqual(basket.shopping_carts, [])
def test_contains_cart(self): basket = ShoppingBasket() discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) cart = ShoppingCart(store) self.assertFalse(basket.contains_cart(cart)) basket.shopping_carts.append(cart) self.assertTrue(basket.contains_cart(cart))
def test_delete_regular_product_discount(self): discount = DiscountService() apple = Product('apple', 'fruit', ['green', 'sweet'], 5) start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) discount.add_product_discount(apple, 70, True, start_date, end_date, coupon_code=123) self.assertTrue(discount.has_coupon_product_discount(apple)) discount.delete_coupon_product_discount(apple) self.assertFalse(discount.has_coupon_product_discount(apple))
def test_add_product_discount(self): discount = DiscountService() apple = Product('apple', 'fruit', ['green', 'sweet'], 5) start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) discount.add_product_discount(apple, 70, True, start_date, end_date) self.assertEqual(len(discount.product_discount_table), 1) self.assertTrue( discount.product_discount_table[apple.catalog_number] ['RegularDiscount'][discount.DISCOUNT_TYPE] == 'RegularDiscount') self.assertEqual( discount.product_discount_table[apple.catalog_number] ['RegularDiscount'][discount.DISCOUNT_PERCENTAGE], 70) self.assertTrue(discount.product_discount_table[apple.catalog_number] ['RegularDiscount'][discount.DOUBLE_DEALS]) discount.add_product_discount(apple, 60, False, start_date, end_date, coupon_code=123) self.assertEqual(len(discount.product_discount_table), 1) self.assertTrue( discount.product_discount_table[apple.catalog_number] ['CouponDiscount'][discount.DISCOUNT_TYPE] == 'CouponDiscount') self.assertTrue( discount.product_discount_table[apple.catalog_number] ['CouponDiscount'][discount.DISCOUNT_PERCENTAGE], 60) self.assertFalse(discount.product_discount_table[apple.catalog_number] ['CouponDiscount'][discount.DOUBLE_DEALS])
def test_remove_product_from_cart(self): basket = ShoppingBasket() discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) product = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'], 35) cart = ShoppingCart(store) self.assertFalse((basket.remove_product_from_cart(product, cart)).val) basket.shopping_carts.append(cart) self.assertFalse((basket.remove_product_from_cart(product, cart)).val) cart.add_product(product) self.assertTrue((basket.remove_product_from_cart(product, cart)).val)
def build_discount_service(self): self.discount_service = DiscountService() discount = get_reg_discount_of_store_db(self.store_number) if not (discount is False): self.discount_service.add_store_discount( discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d')) for p in self.products: discount = get_reg_discount_of_product_db(p[0]) print('discount: ', discount) if not (discount is False): self.discount_service.add_product_discount( discount[0], discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d')) discount = get_cond_discount_of_product_db(p[0]) if not (discount is False): self.discount_service.add_product_discount( discount[0], discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d'), condition=(discount[5], p[0], discount[6])) discount = get_coupon_discount_of_product_db(p[0]) if not (discount is False): self.discount_service.add_product_discount( discount[0], discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d'), coupon_code=123) print('\nproduct_discount_table: ', self.discount_service.product_discount_table) print('\nstore_discount: ', self.discount_service.store_discount)
def test_delete_condition_product_discount(self): discount = DiscountService() apple = Product('apple', 'fruit', ['green', 'sweet'], 5) mango = Product('mango', 'fruit', ['yellow', 'sweet'], 7) pear = Product('pear', 'fruit', ['green', 'sweet'], 4) start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) discount.add_product_discount(apple, 70, True, start_date, end_date, condition=('And', pear, mango)) self.assertTrue(discount.has_conditional_product_discount(apple)) discount.delete_conditional_product_discount(apple) self.assertFalse(discount.has_conditional_product_discount(apple))
def test_calculate_price(self): basket = ShoppingBasket() discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) cart = ShoppingCart(store) product_a = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'], 35) product_b = Product('israeli schnitzel', 'schnitzel', ['hot', 'casher'], 15) cart.add_product(product_a) basket.shopping_carts.append(cart) self.assertEqual(basket.calculate_price(), 35) cart.add_product(product_b) self.assertEqual(basket.calculate_price(), 50)
def test_calculate_product_discount_percentage(self): store = Store('effect', 123, 123, None, None) discount = DiscountService() apple = Product('apple', 'fruit', ['green', 'sweet'], 5) shopping_cart = ShoppingCart(store) start_date = datetime.datetime(2016, 2, 1, 12, 12, 12, 12) end_date = datetime.datetime(2020, 2, 1, 12, 12, 12, 12) discount.store_discount = ('StoreDiscount', 10, True, start_date, end_date) discount.add_product_discount(apple, 20, True, start_date, end_date, coupon_code=123) x = discount.calculate_product_discount_percentage( apple, shopping_cart) self.assertEqual(x[0], 30) self.assertEqual(x[1], ['CouponDiscount', 'StoreDiscount']) discount.add_product_discount(apple, 45, False, start_date, end_date) x = discount.calculate_product_discount_percentage( apple, shopping_cart) self.assertEqual(x[0], 45) self.assertEqual(x[1], ['RegularDiscount'])
def test_add_product_to_cart(self): basket = ShoppingBasket() discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) product_a = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'], 35) self.assertFalse((basket.add_product_to_cart(product_a, store)).val) store.add_new_product(product_a) self.assertFalse((basket.add_product_to_cart(product_a, store)).val) store.inc_product_amount(product_a.catalog_number, 5) self.assertTrue((basket.add_product_to_cart(product_a, store)).val) self.assertEqual(len(basket.shopping_carts), 1) product_b = Product('israeli schnitzel', 'schnitzel', ['hot', 'casher'], 15) store.add_new_product(product_b) store.inc_product_amount(product_b.catalog_number, 5) self.assertTrue((basket.add_product_to_cart(product_b, store)).val) self.assertEqual(len(basket.shopping_carts), 1) self.assertEqual(len(basket.shopping_carts[0].products), 2)
def test_make_purchase(self): user = User('ofek', 18) discount_service = DiscountService() buying_policy = BuyingPolicyService() address = DeliveryAddress(13, 'Israel') store = Store('effect', 123, 123, discount_service, buying_policy) Ecommerce.get_instance().stores.append(store) cart = ShoppingCart(store) product = Product('israeli schnitzel', 'schnitzel', ['hot', 'casher'], 15) store.products.append({'product': product, 'amount': 5}) cart.products[product.catalog_number] = { 'product': product, 'amount': 4 } user.shopping_basket.shopping_carts.append(cart) all_transactions_been_made = user.make_purchase( 'paypal', ['ofek', '4580160114431651', '7', '20', '458'], address) self.assertEqual(len(all_transactions_been_made), 1) self.assertEqual(store.products[0]['amount'], 1)
def test_get_transactions_by_identifier(self): discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) cart = ShoppingCart(store) product_a = Product('france schnitzel', 'schnitzel', ['hot', 'krispy'], 35) store.add_new_product(product_a) store.inc_product_amount(product_a.catalog_number, 5) cart.add_product(product_a) delivery_address = DeliveryAddress(123, 'Israel') trans = (self.transaction_management.execute_transaction( 123, cart, 'paypal', ['ofek', '4580160114431651', '7', '20', '458'], delivery_address, 123)).val self.assertEqual( self.transaction_management.get_transactions_by_identifier(123), [trans]) self.assertEqual( self.transaction_management.get_transactions_by_store(12), [])
def test_execute_transaction(self): discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('effect', 123, 123, discount_service, buying_policy) cart = ShoppingCart(store) product_a = Product('france schnitzel', 'schnitzel', ['hot', 'krispy'], 35) cart.add_product(product_a) delivery_address = DeliveryAddress(123, 'Israel') store.add_new_product(product_a) store.inc_product_amount(product_a.catalog_number, 5) trans = (self.transaction_management.execute_transaction( 123, cart, 'paypal', ['ofek', '4580160114431651', '7', '20', '458'], delivery_address, 123)).val self.assertEqual(trans.total_price, 35) self.assertEqual(trans.buyer_identifier, 123) self.assertEqual(trans.products[product_a.catalog_number]['product'], product_a) self.assertEqual(trans.store_number, store.store_number) self.assertEqual(trans.store_account, store.account_number) self.assertEqual(len(self.transaction_management.get_transactions()), 1) product_b = Product('israeli schnitzel', 'schnitzel', ['hot', 'casher'], 15) store.add_new_product(product_b) store.inc_product_amount(product_b.catalog_number, 5) cart.add_product(product_b) trans = (self.transaction_management.execute_transaction( 12, cart, 'paypal', ['ofek', '4580160114431651', '7', '20', '458'], delivery_address, 123)).val self.assertEqual(trans.total_price, 50) self.assertEqual(trans.buyer_identifier, 12) self.assertEqual(trans.products[product_a.catalog_number]['product'], product_a) self.assertEqual(trans.products[product_b.catalog_number]['product'], product_b) self.assertEqual(trans.store_number, store.store_number) self.assertEqual(trans.store_account, store.account_number) self.assertEqual(len(self.transaction_management.get_transactions()), 2)
def test_calculate_price(self): discount_service = DiscountService() buying_policy = BuyingPolicyService() store = Store('schnitzale', 23444, 4, discount_service, buying_policy) cart = ShoppingCart(store) product_a = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'], 35) product_b = Product('israeli schnitzel', 'schnitzel', ['hot', 'casher'], 15) store.add_new_product(product_a) store.inc_product_amount(product_a.catalog_number, 5) store.add_new_product(product_b) store.inc_product_amount(product_b.catalog_number, 5) cart.products[product_a.catalog_number] = { 'product': product_a, 'amount': 2 } self.assertEqual(cart.calculate_price(), 70) cart.products[product_b.catalog_number] = { 'product': product_b, 'amount': 1 } self.assertEqual(cart.calculate_price(), 85)
class ShoppingCart: def __init__(self, store_number, products): self.store_number = store_number self.products = products self.discount_service = None self.build_discount_service() def build_discount_service(self): self.discount_service = DiscountService() discount = get_reg_discount_of_store_db(self.store_number) if not (discount is False): self.discount_service.add_store_discount( discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d')) for p in self.products: discount = get_reg_discount_of_product_db(p[0]) print('discount: ', discount) if not (discount is False): self.discount_service.add_product_discount( discount[0], discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d')) discount = get_cond_discount_of_product_db(p[0]) if not (discount is False): self.discount_service.add_product_discount( discount[0], discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d'), condition=(discount[5], p[0], discount[6])) discount = get_coupon_discount_of_product_db(p[0]) if not (discount is False): self.discount_service.add_product_discount( discount[0], discount[1], discount[2], datetime.datetime.strptime(discount[3], '%Y-%m-%d'), datetime.datetime.strptime(discount[4], '%Y-%m-%d'), coupon_code=123) print('\nproduct_discount_table: ', self.discount_service.product_discount_table) print('\nstore_discount: ', self.discount_service.store_discount) def calculate_price(self): price_sum = 0 for product_dict in self.products: discount_percentage = \ self.discount_service.calculate_product_discount_percentage(product_dict[0], self) discount_percentage = discount_percentage[0] print('\ndiscount_percentage:', discount_percentage) if discount_percentage > 80: discount_percentage = 80 price_sum += (product_dict[2] * ( (100 - discount_percentage) / 100)) * product_dict[1] return price_sum def contains_product(self, catalog_number): for p in self.products: if p[0] == catalog_number: return True return False