Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Update tests

Mira Szewczyk245b50eb51a7
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 6a754dd67b..7f78926293 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -14,9 +14,11 @@ from ....order.utils import (
allocate_stock,
change_order_line_quantity,
delete_order_line,
+ get_order_country,
recalculate_order,
update_order_prices,
)
+from ....stock.availability import check_stock_quantity, get_available_quantity
from ...account.i18n import I18nMixin
from ...account.types import AddressInput
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
@@ -266,14 +268,16 @@ class DraftOrderComplete(BaseMutation):
order.shipping_address.delete()
order.save()
+ country = get_order_country(order)
oversold_items = []
for line in order:
try:
- line.variant.check_quantity(line.quantity)
- allocate_stock(line.variant, line.quantity)
+ check_stock_quantity(line.variant, country, line.quantity)
+ allocate_stock(line.variant, country, line.quantity)
except InsufficientStock:
- allocate_stock(line.variant, line.variant.quantity_available)
+ available_stock = get_available_quantity(line.variant, country)
+ allocate_stock(line.variant, country, available_stock)
oversold_items.append(str(line))
order_created(order, user=info.context.user, from_draft=True)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index c62a43952a..73ab2ccc89 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -24,6 +24,8 @@ def get_order_country(order) -> str:
address = order.billing_address
if order.is_shipping_required():
address = order.shipping_address
+ if address is None:
+ return settings.DEFAULT_COUNTRY
return address.country.code
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index ba6e3fb151..d4c1dbbd5e 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -27,6 +27,7 @@ from saleor.order.models import Order, OrderEvent
from saleor.payment import ChargeStatus, CustomPaymentChoices, PaymentError
from saleor.payment.models import Payment
from saleor.shipping.models import ShippingMethod
+from saleor.stock.models import Stock
from .utils import assert_no_permission, get_graphql_content
@@ -784,8 +785,10 @@ def test_draft_order_complete(
line_1, line_2 = order.lines.order_by("-quantity").all()
line_1.quantity = 1
line_1.save(update_fields=["quantity"])
- assert line_1.variant.quantity_available >= line_1.quantity
- assert line_2.variant.quantity_available < line_2.quantity
+ stock_1 = Stock.objects.get(product_variant=line_1.variant)
+ stock_2 = Stock.objects.get(product_variant=line_2.variant)
+ assert stock_1.quantity_available >= line_1.quantity
+ assert stock_2.quantity_available < line_2.quantity
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
@@ -1947,7 +1950,12 @@ MUTATION_CANCEL_ORDERS = """
def test_order_bulk_cancel_with_restock(
- staff_api_client, orders, order_with_lines, permission_manage_orders
+ staff_api_client,
+ orders,
+ order_with_lines,
+ permission_manage_orders,
+ address,
+ permission_manage_stocks,
):
assert order_with_lines.can_cancel()
orders.append(order_with_lines)
@@ -1957,7 +1965,9 @@ def test_order_bulk_cancel_with_restock(
"restock": True,
}
response = staff_api_client.post_graphql(
- MUTATION_CANCEL_ORDERS, variables, permissions=[permission_manage_orders]
+ MUTATION_CANCEL_ORDERS,
+ variables,
+ permissions=[permission_manage_orders, permission_manage_stocks],
)
order_with_lines.refresh_from_db()
content = get_graphql_content(response)
diff --git a/tests/api/test_stock.py b/tests/api/test_stock.py
index 2fad520651..efe0683d81 100644
--- a/tests/api/test_stock.py
+++ b/tests/api/test_stock.py
@@ -160,6 +160,26 @@ def test_create_stock_mutation(
assert len(content_errors) == 0
+def test_create_stock_negative_quantity(
+ staff_api_client, variant, warehouse, permission_manage_stocks
+):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ variables = {
+ "input": {
+ "productVariant": variant_id,
+ "warehouse": warehouse_id,
+ "quantity": -100,
+ }
+ }
+
+ response = staff_api_client.post_graphql(MUTATION_CREATE_STOCK, variables=variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["createStock"]["errors"]
+ assert len(errors) == 1
+
+
def test_update_stock_required_permission(staff_api_client, stock):
assert not staff_api_client.user.has_perm("stock.manage_stocks")
stock_id = graphene.Node.to_global_id("Stock", stock.pk)
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 0a74b54369..bb4ab21fc5 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -71,7 +71,6 @@ def test_create_variant(
$sku: String!,
$priceOverride: Decimal,
$costPrice: Decimal,
- $quantity: Int!,
$attributes: [AttributeValueInput]!,
$weight: WeightScalar,
$trackInventory: Boolean!) {
@@ -81,7 +80,6 @@ def test_create_variant(
sku: $sku,
priceOverride: $priceOverride,
costPrice: $costPrice,
- quantity: $quantity,
attributes: $attributes,
trackInventory: $trackInventory,
weight: $weight
@@ -101,7 +99,6 @@ def test_create_variant(
slug
}
}
- quantity
priceOverride {
currency
amount
@@ -150,7 +147,6 @@ def test_create_variant(
assert not content["productErrors"]
data = content["productVariant"]
assert data["name"] == variant_value
- assert data["quantity"] == quantity
assert data["costPrice"]["amount"] == cost_price
assert data["priceOverride"]["amount"] == price_override
assert data["sku"] == sku
@@ -322,7 +318,6 @@ def test_create_product_variant_update_with_new_attributes(
"costPrice": 10,
"id": variant_id,
"priceOverride": 0,
- "quantity": 4,
"sku": "21599567",
"trackInventory": True,
}
@@ -346,20 +341,17 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
$id: ID!,
$sku: String!,
$costPrice: Decimal,
- $quantity: Int!,
$trackInventory: Boolean!) {
productVariantUpdate(
id: $id,
input: {
sku: $sku,
costPrice: $costPrice,
- quantity: $quantity,
trackInventory: $trackInventory
}) {
productVariant {
name
sku
- quantity
costPrice {
currency
amount
@@ -374,12 +366,10 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
sku = "test sku"
cost_price = 3.3
- quantity = 123
variables = {
"id": variant_id,
"sku": sku,
- "quantity": quantity,
"costPrice": cost_price,
"trackInventory": True,
}
@@ -391,7 +381,6 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
content = get_graphql_content(response)
data = content["data"]["productVariantUpdate"]["productVariant"]
assert data["name"] == variant.name
- assert data["quantity"] == quantity
assert data["costPrice"]["amount"] == cost_price
assert data["sku"] == sku
@@ -419,7 +408,6 @@ def test_update_product_variant_unset_amounts(
productVariant {
name
sku
- quantity
costPrice {
currency
amount
@@ -928,40 +916,6 @@ def test_product_variant_bulk_create_with_new_attribute_value(
assert attribute_value_count + 1 == size_attribute.values.count()
-def test_product_variant_bulk_create_negative_quantity(
- staff_api_client, product, size_attribute, permission_manage_products
-):
- product_variant_count = ProductVariant.objects.count()
- product_id = graphene.Node.to_global_id("Product", product.pk)
- size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)
- variants = [
- {
- "sku": str(uuid4())[:12],
- "quantity": -1000,
- "attributes": [{"id": size_attribute_id, "values": ["Test-value"]}],
- },
- {
- "sku": str(uuid4())[:12],
- "quantity": 100,
- "attributes": [{"id": size_attribute_id, "values": ["Test-value2"]}],
- },
- ]
-
- variables = {"productId": product_id, "variants": variants}
- staff_api_client.user.user_permissions.add(permission_manage_products)
- response = staff_api_client.post_graphql(
- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["productVariantBulkCreate"]
- assert len(data["bulkProductErrors"]) == 1
- error = data["bulkProductErrors"][0]
- assert error["field"] == "quantity"
- assert error["code"] == ProductErrorCode.INVALID.name
- assert error["index"] == 0
- assert product_variant_count == ProductVariant.objects.count()
-
-
def test_product_variant_bulk_create_duplicated_sku(
staff_api_client,
product,