From 49258193edb7f8debe3f53c941c3bae70f796766 Mon Sep 17 00:00:00 2001 From: Mark Chackerian Date: Tue, 15 May 2018 11:36:08 -0400 Subject: [PATCH 1/6] adds decimal type and associated tests --- graphene/__init__.py | 1 + graphene/types/__init__.py | 1 + graphene/types/decimal.py | 29 ++++++++++++++++++ graphene/types/tests/test_decimal.py | 44 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 graphene/types/decimal.py create mode 100644 graphene/types/tests/test_decimal.py diff --git a/graphene/__init__.py b/graphene/__init__.py index 57e6b0ec..695cdb1d 100644 --- a/graphene/__init__.py +++ b/graphene/__init__.py @@ -12,6 +12,7 @@ from .types import ( Scalar, String, ID, Int, Float, Boolean, Date, DateTime, Time, + Decimal, JSONString, UUID, List, NonNull, diff --git a/graphene/types/__init__.py b/graphene/types/__init__.py index b5b9427c..52a7244a 100644 --- a/graphene/types/__init__.py +++ b/graphene/types/__init__.py @@ -6,6 +6,7 @@ from .interface import Interface from .mutation import Mutation from .scalars import Scalar, String, ID, Int, Float, Boolean from .datetime import Date, DateTime, Time +from .decimal import Decimal from .json import JSONString from .uuid import UUID from .schema import Schema diff --git a/graphene/types/decimal.py b/graphene/types/decimal.py new file mode 100644 index 00000000..8965ecad --- /dev/null +++ b/graphene/types/decimal.py @@ -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 diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py new file mode 100644 index 00000000..ac8d2d58 --- /dev/null +++ b/graphene/types/tests/test_decimal.py @@ -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 From d40ef4be2b05820843463e914f20aa8af695d8f4 Mon Sep 17 00:00:00 2001 From: Mark Chackerian Date: Tue, 15 May 2018 11:42:43 -0400 Subject: [PATCH 2/6] adds Decimal to types __all__ --- graphene/types/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/graphene/types/__init__.py b/graphene/types/__init__.py index 52a7244a..ef1b6942 100644 --- a/graphene/types/__init__.py +++ b/graphene/types/__init__.py @@ -41,6 +41,7 @@ __all__ = [ 'Date', 'DateTime', 'Time', + 'Decimal', 'JSONString', 'UUID', 'Boolean', From de050fa6db2acdb9086abb8c3b86a570737304ef Mon Sep 17 00:00:00 2001 From: Mark Chackerian Date: Tue, 15 May 2018 12:09:12 -0400 Subject: [PATCH 3/6] fix linting error --- graphene/types/decimal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene/types/decimal.py b/graphene/types/decimal.py index 8965ecad..29db46b9 100644 --- a/graphene/types/decimal.py +++ b/graphene/types/decimal.py @@ -15,7 +15,7 @@ class Decimal(Scalar): 'Received not compatible Decimal "{}"'.format(repr(dec)) ) return str(dec) - + @classmethod def parse_literal(cls, node): if isinstance(node, ast.StringValue): From 0fdc2ca3eb206c7b7ffd7f160ea4bd314a85a480 Mon Sep 17 00:00:00 2001 From: Mark Chackerian Date: Mon, 16 Jul 2018 17:20:49 -0400 Subject: [PATCH 4/6] should fix some import issues with python 2.7 --- graphene/types/decimal.py | 10 +++++++--- graphene/types/tests/test_decimal.py | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/graphene/types/decimal.py b/graphene/types/decimal.py index 29db46b9..1bc07b01 100644 --- a/graphene/types/decimal.py +++ b/graphene/types/decimal.py @@ -1,4 +1,6 @@ -import decimal +from __future__ import absolute_import + +from decimal import Decimal as _Decimal from graphql.language import ast @@ -11,7 +13,9 @@ class Decimal(Scalar): """ @staticmethod def serialize(dec): - assert isinstance(dec, decimal.Decimal), ( + if isinstance(dec, str): + dec = _Decimal(dec) + assert isinstance(dec, _Decimal), ( 'Received not compatible Decimal "{}"'.format(repr(dec)) ) return str(dec) @@ -24,6 +28,6 @@ class Decimal(Scalar): @staticmethod def parse_value(value): try: - return decimal.Decimal(value) + return _Decimal(value) except ValueError: return None diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py index ac8d2d58..cde048e4 100644 --- a/graphene/types/tests/test_decimal.py +++ b/graphene/types/tests/test_decimal.py @@ -21,6 +21,7 @@ def test_decimal_string_query(): assert result.data == { 'decimal': str(decimal_value) } + assert decimal.Decimal(result.data['decimal']) == decimal_value def test_decimal_string_query_variable(): @@ -34,6 +35,7 @@ def test_decimal_string_query_variable(): assert result.data == { 'decimal': str(decimal_value) } + assert decimal.Decimal(result.data['decimal']) == decimal_value def test_bad_decimal_query(): From c076412ba55af4c6134c654e5a3042415a9b266e Mon Sep 17 00:00:00 2001 From: Mark Chackerian Date: Mon, 16 Jul 2018 18:20:04 -0400 Subject: [PATCH 5/6] automatically generated linting fixes --- graphene/types/decimal.py | 5 +++-- graphene/types/tests/test_decimal.py | 27 ++++++++++++--------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/graphene/types/decimal.py b/graphene/types/decimal.py index 1bc07b01..2f99134d 100644 --- a/graphene/types/decimal.py +++ b/graphene/types/decimal.py @@ -11,12 +11,13 @@ 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)) + assert isinstance(dec, _Decimal), 'Received not compatible Decimal "{}"'.format( + repr(dec) ) return str(dec) diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py index cde048e4..e4c08ffc 100644 --- a/graphene/types/tests/test_decimal.py +++ b/graphene/types/tests/test_decimal.py @@ -11,36 +11,33 @@ class Query(ObjectType): 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) + 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 + 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') + decimal_value = decimal.Decimal("1969.1974") result = schema.execute( - '''query Test($decimal: Decimal){ decimal(input: $decimal) }''', - variable_values={'decimal': decimal_value} + """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 + 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) + + result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal) assert len(result.errors) == 1 assert result.data == None From 8ca7b855acce015e1e52cc99b73990700f988577 Mon Sep 17 00:00:00 2001 From: Mark Chackerian Date: Mon, 16 Jul 2018 18:31:32 -0400 Subject: [PATCH 6/6] more flake8 fixes --- graphene/__init__.py | 1 + graphene/types/tests/test_decimal.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/graphene/__init__.py b/graphene/__init__.py index e0f6c51a..2884939c 100644 --- a/graphene/__init__.py +++ b/graphene/__init__.py @@ -66,6 +66,7 @@ __all__ = [ "Date", "DateTime", "Time", + "Decimal", "JSONString", "UUID", "List", diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py index e4c08ffc..abc4a6c4 100644 --- a/graphene/types/tests/test_decimal.py +++ b/graphene/types/tests/test_decimal.py @@ -40,4 +40,4 @@ def test_bad_decimal_query(): result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal) assert len(result.errors) == 1 - assert result.data == None + assert result.data is None