-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #726 from picturedots/issue-703
Issue 703 -- support for Decimal type
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import absolute_import | ||
|
||
from decimal import Decimal as _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): | ||
if isinstance(dec, str): | ||
dec = _Decimal(dec) | ||
assert isinstance(dec, _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(value) | ||
except ValueError: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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)} | ||
assert decimal.Decimal(result.data["decimal"]) == 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)} | ||
assert decimal.Decimal(result.data["decimal"]) == 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 is None |