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_has_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) self.assertFalse(discount.has_regular_product_discount(apple)) discount.add_product_discount(apple, 70, True, start_date, end_date) self.assertTrue(discount.has_regular_product_discount(apple))
def test_has_conditional_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) self.assertFalse(discount.has_conditional_product_discount(apple)) discount.add_product_discount(apple, 70, True, start_date, end_date, condition=('And', pear, mango)) self.assertTrue(discount.has_conditional_product_discount(apple))
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_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'])
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