mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-09 08:00:39 +03:00
adds decimal type and associated tests
This commit is contained in:
parent
12d4dab774
commit
49258193ed
|
@ -12,6 +12,7 @@ from .types import (
|
||||||
Scalar,
|
Scalar,
|
||||||
String, ID, Int, Float, Boolean,
|
String, ID, Int, Float, Boolean,
|
||||||
Date, DateTime, Time,
|
Date, DateTime, Time,
|
||||||
|
Decimal,
|
||||||
JSONString,
|
JSONString,
|
||||||
UUID,
|
UUID,
|
||||||
List, NonNull,
|
List, NonNull,
|
||||||
|
|
|
@ -6,6 +6,7 @@ from .interface import Interface
|
||||||
from .mutation import Mutation
|
from .mutation import Mutation
|
||||||
from .scalars import Scalar, String, ID, Int, Float, Boolean
|
from .scalars import Scalar, String, ID, Int, Float, Boolean
|
||||||
from .datetime import Date, DateTime, Time
|
from .datetime import Date, DateTime, Time
|
||||||
|
from .decimal import Decimal
|
||||||
from .json import JSONString
|
from .json import JSONString
|
||||||
from .uuid import UUID
|
from .uuid import UUID
|
||||||
from .schema import Schema
|
from .schema import Schema
|
||||||
|
|
29
graphene/types/decimal.py
Normal file
29
graphene/types/decimal.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import decimal
|
||||||
|
|
||||||
|
from graphql.language import ast
|
||||||
|
|
||||||
|
from .scalars import Scalar
|
||||||
|
|
||||||
|
|
||||||
|
class Decimal(Scalar):
|
||||||
|
"""
|
||||||
|
The `Decimal` scalar type represents a python Decimal.
|
||||||
|
"""
|
||||||
|
@staticmethod
|
||||||
|
def serialize(dec):
|
||||||
|
assert isinstance(dec, decimal.Decimal), (
|
||||||
|
'Received not compatible Decimal "{}"'.format(repr(dec))
|
||||||
|
)
|
||||||
|
return str(dec)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def parse_literal(cls, node):
|
||||||
|
if isinstance(node, ast.StringValue):
|
||||||
|
return cls.parse_value(node.value)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse_value(value):
|
||||||
|
try:
|
||||||
|
return decimal.Decimal(value)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
44
graphene/types/tests/test_decimal.py
Normal file
44
graphene/types/tests/test_decimal.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import decimal
|
||||||
|
|
||||||
|
from ..decimal import Decimal
|
||||||
|
from ..objecttype import ObjectType
|
||||||
|
from ..schema import Schema
|
||||||
|
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
decimal = Decimal(input=Decimal())
|
||||||
|
|
||||||
|
def resolve_decimal(self, info, input):
|
||||||
|
return input
|
||||||
|
|
||||||
|
schema = Schema(query=Query)
|
||||||
|
|
||||||
|
|
||||||
|
def test_decimal_string_query():
|
||||||
|
decimal_value = decimal.Decimal('1969.1974')
|
||||||
|
result = schema.execute('''{ decimal(input: "%s") }''' % decimal_value)
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {
|
||||||
|
'decimal': str(decimal_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_decimal_string_query_variable():
|
||||||
|
decimal_value = decimal.Decimal('1969.1974')
|
||||||
|
|
||||||
|
result = schema.execute(
|
||||||
|
'''query Test($decimal: Decimal){ decimal(input: $decimal) }''',
|
||||||
|
variable_values={'decimal': decimal_value}
|
||||||
|
)
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {
|
||||||
|
'decimal': str(decimal_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_decimal_query():
|
||||||
|
not_a_decimal = "Nobody expects the Spanish Inquisition!"
|
||||||
|
|
||||||
|
result = schema.execute('''{ decimal(input: "%s") }''' % not_a_decimal)
|
||||||
|
assert len(result.errors) == 1
|
||||||
|
assert result.data == None
|
Loading…
Reference in New Issue
Block a user