def test_add_and_remove_and_clear(): product = create_product('fractionable', fractional=True) complete_product(product) supplier = get_default_supplier() request = get_request_with_basket() basket = request.basket with pytest.raises(ValidationError): # Ordering antimatter is not supported basket_commands.handle_add(request, basket, product_id=product.pk, quantity=-3) # These will get merged into one line... basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 1, "supplier_id": supplier.pk}) basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 2}) # Fractions should also be supported basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 0.75}) # ... so there will be 3 products but one line assert basket.product_count == 3.75 lines = basket.get_lines() assert len(lines) == 1 # ... and deleting that line will clear the basket... basket_commands.handle_del(request, basket, lines[0].line_id) assert basket.product_count == 0 # ... and adding another product will create a new line... basket_commands.handle_add(request, basket, product_id=product.pk, quantity=1) assert basket.product_count == 1 # ... that can be cleared. basket_commands.handle_clear(request, basket) assert basket.product_count == 0
def test_basket_update(): request = get_request_with_basket() basket = request.basket product = create_product('fractionable', fractional=True) complete_product(product) basket_commands.handle_add(request, basket, product_id=product.pk, quantity=1.75) assert basket.product_count == 1.75 line_id = basket.get_lines()[0].line_id basket_commands.handle_update(request, basket, **{"q_%s" % line_id: "2"}) assert basket.product_count == 2 basket_commands.handle_update(request, basket, **{"delete_%s" % line_id: "1"}) assert basket.product_count == 0
def test_add_and_invalid_product(): shop = get_default_shop() product = create_product('fractionable', fractional=True) complete_product(product) supplier = get_default_supplier() request = get_request_with_basket() basket = request.basket # remove the shop product product.get_shop_instance(shop).delete() with pytest.raises(ValidationError) as exc: basket_commands.handle_add(request, basket, **{ "product_id": product.pk, "quantity": 1, "supplier_id": supplier.pk }) assert "Product not available in this shop" in exc.value.message