Skip to content

Commit

Permalink
Merge pull request #3457 from mirumee/3428/Fix_decimal_value_from_arg…
Browse files Browse the repository at this point in the history
…uments

Fix decimal value argument in GraphQL
  • Loading branch information
maarcingebala committed Dec 14, 2018
2 parents 9460fb2 + 4c61daf commit 7f2ec04
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Add payment authorize and charge mutation - #3426 by @jxltom
Do not show `Pay For Order` if order is partly paid since partial payment is not supported - #3398 by @jxltom
Add alt text to `Product` `thumbnail` and `background_image` of `Collection` and `Category` - #3429 by @fowczarek
Improve several payment validations - #3418 by @jxltom
Fix decimal value argument in GraphQL = #3457 by @fowczarek
7 changes: 7 additions & 0 deletions saleor/graphql/core/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Decimal(graphene.Float):
parses float to the Decimal on the way back.
"""

@staticmethod
def parse_literal(node):
try:
return decimal.Decimal(node.value)
except decimal.DecimalException:
return None

@staticmethod
def parse_value(value):
try:
Expand Down
56 changes: 56 additions & 0 deletions tests/api/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,59 @@ def test_total_count_query(api_client, product):
response = api_client.post_graphql(query)
content = get_graphql_content(response)
assert content['data']['products']['totalCount'] == Product.objects.count()


def test_mutation_decimal_input(
staff_api_client, variant, permission_manage_products):
query = """
mutation decimalInput($id: ID!, $cost: Decimal) {
productVariantUpdate(id: $id,
input: {costPrice: $cost}) {
errors {
field
message
}
productVariant {
costPrice{
amount
}
}
}
}
"""
variables = {
'id': graphene.Node.to_global_id('ProductVariant', variant.id),
'cost': 12.12}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products])
content = get_graphql_content(response)
data = content['data']['productVariantUpdate']
assert data['errors'] == []


def test_mutation_decimal_input_without_arguments(
staff_api_client, variant, permission_manage_products):
query = """
mutation {
productVariantUpdate(id: "%(variant_id)s",
input: {costPrice: "%(cost)s"}) {
errors {
field
message
}
productVariant {
costPrice{
amount
}
}
}
}
""" % {
'variant_id': graphene.Node.to_global_id('ProductVariant', variant.id),
'cost': 12.12
}
response = staff_api_client.post_graphql(
query, permissions=[permission_manage_products])
content = get_graphql_content(response)
data = content['data']['productVariantUpdate']
assert data['errors'] == []

0 comments on commit 7f2ec04

Please sign in to comment.