def execute_transaction(self, username: str, shopping_cart, payment_method: str, payment_details, address: DeliveryAddress, account_number: int): if not self.check_availability_of_products(shopping_cart): return MessageResponse( False, 1, "Not all the products are available in this amount") TransactionManagement.transaction_counter += 1 transaction = Transaction(self.transaction_counter, shopping_cart['products'], username, payment_method, payment_details, address, account_number, shopping_cart['store_number']) cart = ShoppingCart(shopping_cart["store_number"], shopping_cart['products']) # until here yet transaction.total_price = cart.calculate_price() if self.payment_system.charge(username, payment_details[0], payment_details[1], payment_details[2], payment_details[3], payment_details[4]) == 'declined': return MessageResponse(False, 1, "payment problem") transaction.update_payment_status() if self.delivery_system.delivery(transaction.products, address) == 'undelivered': return MessageResponse(False, 1, 'delivery problem, payment were cancelled') transaction.update_delivery_status() self.transactions.append(transaction) self.remove_products_from_store(shopping_cart) return MessageResponse( transaction, 1, 'succeed transaction' + str(transaction.transaction_id))
def test_contains_product(self): store = Store('schnitzale', 23444, 4, None, None) cart = ShoppingCart(store) product = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'], 35) self.assertFalse(cart.contains_product(product)) store.add_new_product(product) store.inc_product_amount(product.catalog_number, 5) cart.products[product.catalog_number] = { 'product': product, 'amount': 1 } self.assertTrue(cart.contains_product(product))
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 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 check_conditional_discount(self, condition, shopping_cart: ShoppingCart): if isinstance(condition, int): return shopping_cart.contains_product(condition) elif condition[0] == 'Or': return self.check_conditional_discount(condition[1], shopping_cart) or \ self.check_conditional_discount(condition[2], shopping_cart) elif condition[0] == 'And': return self.check_conditional_discount(condition[1], shopping_cart) and \ self.check_conditional_discount(condition[2], shopping_cart)
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_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)
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_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_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_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_remove_product(self): store = Store('schnitzale', 23444, 4, None, None) cart = ShoppingCart(store) product = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'], 35) store.add_new_product(product) store.inc_product_amount(product.catalog_number, 5) cart.products[product.catalog_number] = { 'product': product, 'amount': 2 } cart.remove_product(product) self.assertEqual(cart.products[product.catalog_number], { 'product': product, 'amount': 1 }) cart.remove_product(product) self.assertEqual(len(cart.products), 0)
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_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)