fix(Decimal): parse integers and floats as decimal. Also enable py38 in tox

This commit is contained in:
Jason Kraus 2021-01-02 14:04:24 -08:00
parent e0d4bec2d8
commit 33d77854ce
3 changed files with 11 additions and 3 deletions

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import
from decimal import Decimal as _Decimal
from graphql.language.ast import StringValueNode
from graphql.language.ast import StringValueNode, IntValueNode, FloatValueNode
from .scalars import Scalar
@ -23,7 +23,7 @@ class Decimal(Scalar):
@classmethod
def parse_literal(cls, node):
if isinstance(node, StringValueNode):
if isinstance(node, (StringValueNode, IntValueNode, FloatValueNode)):
return cls.parse_value(node.value)
@staticmethod

View File

@ -41,3 +41,11 @@ def test_bad_decimal_query():
result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal)
assert len(result.errors) == 1
assert result.data is None
def test_decimal_string_query_integer():
decimal_value = 1
result = schema.execute("""{ decimal(input: %s) }""" % decimal_value)
assert not result.errors
assert result.data == {"decimal": str(decimal_value)}
assert decimal.Decimal(result.data["decimal"]) == decimal_value

View File

@ -8,7 +8,7 @@ deps =
setenv =
PYTHONPATH = .:{envdir}
commands =
py{36,37}: pytest --cov=graphene graphene examples {posargs}
py{36,37,38}: pytest --cov=graphene graphene examples {posargs}
[testenv:pre-commit]
basepython=python3.7