Skip to content

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

Fix WeightScalar

Michał Sipa6aad495edfa8
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.py
index 27f537c663..dd31ec4091 100644
--- a/saleor/graphql/core/scalars.py
+++ b/saleor/graphql/core/scalars.py
@@ -1,6 +1,7 @@
import decimal
import graphene
+from graphql.language import ast
from measurement.measures import Weight
from ...core.weight import convert_weight, get_default_weight_unit
@@ -34,14 +35,19 @@ class Decimal(graphene.Float):
class WeightScalar(graphene.Scalar):
@staticmethod
def parse_value(value):
- # Expects value to be a string "amount unit" separated by a single
- # space.
- try:
- value = decimal.Decimal(value)
- except decimal.DecimalException:
- return None
- default_unit = get_default_weight_unit()
- return Weight(**{default_unit: value})
+ weight = None
+ if isinstance(value, dict):
+ weight = Weight(**{value["unit"]: value["value"]})
+ else:
+ try:
+ value = decimal.Decimal(value)
+ except decimal.DecimalException:
+ return None
+ default_unit = get_default_weight_unit()
+ weight = Weight(**{default_unit: value})
+ if not weight:
+ raise ValueError()
+ return weight
@staticmethod
def serialize(weight):
@@ -54,4 +60,35 @@ class WeightScalar(graphene.Scalar):
@staticmethod
def parse_literal(node):
- return node
+ weight = None
+ if isinstance(node, ast.ObjectValue):
+ weight = WeightScalar.parse_literal_object(node)
+ else:
+ weight = WeightScalar.parse_literal_decimal(node)
+ if not weight:
+ raise ValueError()
+ return weight
+
+ @staticmethod
+ def parse_literal_decimal(node):
+ try:
+ value = decimal.Decimal(node.value)
+ except decimal.DecimalException:
+ return None
+ default_unit = get_default_weight_unit()
+ return Weight(**{default_unit: value})
+
+ @staticmethod
+ def parse_literal_object(node):
+ value = 0
+ unit = get_default_weight_unit()
+
+ for field in node.fields:
+ if field.name.value == "value":
+ try:
+ value = decimal.Decimal(field.value.value)
+ except decimal.DecimalException:
+ return None
+ if field.name.value == "unit":
+ unit = field.value.value
+ return Weight(**{unit: value})
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 145de38c88..0108c02e7e 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -3961,3 +3961,196 @@ def test_update_or_create_variant_stocks_empty_stocks_data(variant, warehouses):
stock = variant.stocks.first()
assert stock.warehouse == warehouses[0]
assert stock.quantity == 5
+
+
+# Because we use Scalars for Weight this test query tests only a scenario when weight
+# value is passed by a variable
+MUTATION_CREATE_PRODUCT_WITH_WEIGHT_GQL_VARIABLE = """
+mutation createProduct(
+ $productType: ID!,
+ $category: ID!
+ $name: String!,
+ $sku: String,
+ $quantity: Int,
+ $basePrice: Decimal!
+ $weight: WeightScalar
+ $trackInventory: Boolean)
+ {
+ productCreate(
+ input: {
+ category: $category,
+ productType: $productType,
+ name: $name,
+ sku: $sku,
+ trackInventory: $trackInventory,
+ quantity: $quantity,
+ basePrice: $basePrice,
+ weight: $weight
+ })
+ {
+ product {
+ id
+ name
+ variants{
+ id
+ sku
+ trackInventory
+ quantity
+ }
+ weight{
+ value
+ unit
+ }
+ }
+ productErrors {
+ message
+ field
+ code
+ }
+ }
+ }
+ """
+
+# Because we use Scalars for Weight this test query tests only a scenario when weight
+# value is passed by directly in input
+MUTATION_CREATE_PRODUCT_WITH_WEIGHT_GQL_INPUT = """
+mutation createProduct(
+ $productType: ID!,
+ $category: ID!
+ $name: String!,
+ $sku: String,
+ $quantity: Int,
+ $basePrice: Decimal!
+ $trackInventory: Boolean)
+ {{
+ productCreate(
+ input: {{
+ category: $category,
+ productType: $productType,
+ name: $name,
+ sku: $sku,
+ trackInventory: $trackInventory,
+ quantity: $quantity,
+ basePrice: $basePrice,
+ weight: {weight}
+ }})
+ {{
+ product {{
+ id
+ name
+ variants{{
+ id
+ sku
+ trackInventory
+ quantity
+ }}
+ weight{{
+ value
+ unit
+ }}
+ }}
+ productErrors {{
+ message
+ field
+ code
+ }}
+ }}
+ }}
+ """
+
+
+ "weight, expected_weight_value, expected_weight_unit",
+ (
+ (11.11, 11.11, "kg"),
+ (11, 11.0, "kg"),
+ ("11.11", 11.11, "kg"),
+ ({"value": 11.11, "unit": "kg"}, 11.11, "kg",),
+ ({"value": 11, "unit": "g"}, 11.0, "g",),
+ ({"value": "11.11", "unit": "ounce"}, 11.11, "oz",),
+ ),
+)
+def test_create_product_with_weight_variable(
+ weight,
+ expected_weight_value,
+ expected_weight_unit,
+ staff_api_client,
+ category,
+ permission_manage_products,
+ product_type_without_variant,
+ warehouse,
+):
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_type_id = graphene.Node.to_global_id(
+ "ProductType", product_type_without_variant.pk
+ )
+ variables = {
+ "category": category_id,
+ "productType": product_type_id,
+ "name": "Test",
+ "quantity": 8,
+ "sku": "23434",
+ "trackInventory": True,
+ "basePrice": Decimal("19"),
+ "weight": weight,
+ }
+ response = staff_api_client.post_graphql(
+ MUTATION_CREATE_PRODUCT_WITH_WEIGHT_GQL_VARIABLE,
+ variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+ from pprint import pprint
+
+ pprint(content)
+ result_weight = content["data"]["productCreate"]["product"]["weight"]
+ assert result_weight["value"] == expected_weight_value
+ assert result_weight["unit"] == expected_weight_unit
+
+
+ "weight, expected_weight_value, expected_weight_unit",
+ (
+ ("11.11", 11.11, "kg"),
+ ("11", 11.0, "kg"),
+ ('"11.11"', 11.11, "kg"),
+ ('{value: 11.11, unit: "kg"}', 11.11, "kg",),
+ ('{value: 11, unit: "g"}', 11.0, "g",),
+ ('{value: "11.11", unit: "ounce"}', 11.11, "oz",),
+ ),
+)
+def test_create_product_with_weight_input(
+ weight,
+ expected_weight_value,
+ expected_weight_unit,
+ staff_api_client,
+ category,
+ permission_manage_products,
+ product_type_without_variant,
+ warehouse,
+):
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_type_id = graphene.Node.to_global_id(
+ "ProductType", product_type_without_variant.pk
+ )
+ variables = {
+ "category": category_id,
+ "productType": product_type_id,
+ "name": "Test",
+ "quantity": 8,
+ "sku": "23434",
+ "trackInventory": True,
+ "basePrice": Decimal("19"),
+ }
+ response = staff_api_client.post_graphql(
+ MUTATION_CREATE_PRODUCT_WITH_WEIGHT_GQL_INPUT.format(weight=weight),
+ variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+ from pprint import pprint
+
+ pprint(content)
+ result_weight = content["data"]["productCreate"]["product"]["weight"]
+ assert result_weight["value"] == expected_weight_value
+ assert result_weight["unit"] == expected_weight_unit