def test_discounts_are_released_by_refunds(self): self.add_discount_prod_1_includes_prod_2(quantity=2) cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) # Enable the discount discounts = discount.available_discounts( self.USER_1, [], [self.PROD_2], ) self.assertEqual(1, len(discounts)) cart.cart.active = False # Keep discount enabled cart.next_cart() cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_2, 2) # The discount will be exhausted cart.next_cart() discounts = discount.available_discounts( self.USER_1, [], [self.PROD_2], ) self.assertEqual(0, len(discounts)) cart.cart.released = True cart.next_cart() discounts = discount.available_discounts( self.USER_1, [], [self.PROD_2], ) self.assertEqual(1, len(discounts))
def test_enumerate_no_discounts_if_condition_not_met(self): self.add_discount_prod_1_includes_cat_2(quantity=1) discounts = discount.available_discounts( self.USER_1, [], [self.PROD_3], ) self.assertEqual(0, len(discounts)) discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(0, len(discounts))
def test_product_discount_does_not_appear_with_category(self): self.add_discount_prod_1_includes_prod_2(quantity=1) cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) # Enable the discount discounts = discount.available_discounts(self.USER_1, [self.CAT_1], []) self.assertEqual(0, len(discounts))
def test_product_discount_enabled_twice_appears_twice(self): self.add_discount_prod_1_includes_prod_3_and_prod_4(quantity=2) cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) # Enable the discount discounts = discount.available_discounts( self.USER_1, [], [self.PROD_3, self.PROD_4], ) self.assertEqual(2, len(discounts))
def test_discount_quantity_is_correct_after_first_purchase(self): self.test_discount_quantity_is_correct_before_first_purchase() cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_3, 1) # Exhaust the quantity discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(1, discounts[0].quantity) cart.next_cart()
def test_discount_quantity_is_correct_before_first_purchase(self): self.add_discount_prod_1_includes_cat_2(quantity=2) cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) # Enable the discount cart.add_to_cart(self.PROD_3, 1) # Exhaust the quantity discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(2, discounts[0].quantity) cart.next_cart()
def handle_products(request, category, products, prefix): ''' Handles a products list form in the given request. Returns the form instance, the discounts applicable to this form, and whether the contents were handled. ''' current_cart = CartController.for_user(request.user) ProductsForm = forms.ProductsForm(category, products) # Create initial data for each of products in category items = commerce.ProductItem.objects.filter( product__in=products, cart=current_cart.cart, ).select_related("product") quantities = [] seen = set() for item in items: quantities.append((item.product, item.quantity)) seen.add(item.product) zeros = set(products) - seen for product in zeros: quantities.append((product, 0)) products_form = ProductsForm( request.POST or None, product_quantities=quantities, prefix=prefix, ) if request.method == "POST" and products_form.is_valid(): if products_form.has_changed(): set_quantities_from_products_form(products_form, current_cart) # If category is required, the user must have at least one # in an active+valid cart if category.required: carts = commerce.Cart.objects.filter(user=request.user) items = commerce.ProductItem.objects.filter( product__category=category, cart=carts, ) if len(items) == 0: products_form.add_error( None, "You must have at least one item from this category", ) handled = False if products_form.errors else True discounts = discount.available_discounts(request.user, [], products) return products_form, discounts, handled
def test_category_discount_appears_once_with_two_valid_product(self): self.add_discount_prod_1_includes_cat_2(quantity=1) cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) # Enable the discount discounts = discount.available_discounts( self.USER_1, [], [self.PROD_3, self.PROD_4] ) self.assertEqual(1, len(discounts))
def test_category_discount_appears_once_if_met_twice(self): self.add_discount_prod_1_includes_cat_2(quantity=1) cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) # Enable the discount discounts = discount.available_discounts( self.USER_1, [self.CAT_2], [self.PROD_3], ) self.assertEqual(1, len(discounts))
def test_discount_is_gone_after_quantity_exhausted(self): self.test_discount_quantity_is_correct_after_first_purchase() discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(0, len(discounts))
def test_enumerate_no_discounts_for_no_input(self): discounts = discount.available_discounts(self.USER_1, [], []) self.assertEqual(0, len(discounts))