Skip to content

Commit

Permalink
Merge pull request #4750 from mirumee/zero-quantity
Browse files Browse the repository at this point in the history
Delete checkout lines by setting the quantity to zero
  • Loading branch information
korycins committed Sep 20, 2019
2 parents 60d0a8d + 1f31e7f commit 27e5039
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion saleor/graphql/checkout/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def update_checkout_shipping_method_if_invalid(checkout: models.Checkout, discou
def check_lines_quantity(variants, quantities):
"""Check if stock is sufficient for each line in the list of dicts."""
for variant, quantity in zip(variants, quantities):
if quantity < 1:
if quantity < 0:
raise ValidationError(
{
"quantity": ValidationError(
Expand Down
35 changes: 30 additions & 5 deletions tests/api/test_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ def test_checkout_create(api_client, variant, graphql_address_data):
"The quantity should be higher than zero.",
CheckoutErrorCode.ZERO_QUANTITY,
),
(
0,
"The quantity should be higher than zero.",
CheckoutErrorCode.ZERO_QUANTITY,
),
(
51,
"Cannot add more than 50 times this item.",
Expand Down Expand Up @@ -718,6 +713,36 @@ def test_checkout_line_delete(
mocked_update_shipping_method.assert_called_once_with(checkout, mock.ANY)


@mock.patch(
"saleor.graphql.checkout.mutations.update_checkout_shipping_method_if_invalid",
wraps=update_checkout_shipping_method_if_invalid,
)
def test_checkout_line_delete_by_zero_quantity(
mocked_update_shipping_method, user_api_client, checkout_with_item
):
checkout = checkout_with_item
assert checkout.lines.count() == 1
line = checkout.lines.first()
variant = line.variant
assert line.quantity == 3

variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)

variables = {
"checkoutId": checkout_id,
"lines": [{"variantId": variant_id, "quantity": 0}],
}
response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)
content = get_graphql_content(response)

data = content["data"]["checkoutLinesUpdate"]
assert not data["errors"]
checkout.refresh_from_db()
assert checkout.lines.count() == 0
mocked_update_shipping_method.assert_called_once_with(checkout, mock.ANY)


def test_checkout_customer_attach(user_api_client, checkout_with_item, customer_user):
checkout = checkout_with_item
assert checkout.user is None
Expand Down

0 comments on commit 27e5039

Please sign in to comment.