From 5e708cc919d30a4eb99b63a0ebfb87b65e73e251 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sun, 6 Dec 2015 15:29:54 -0800 Subject: [PATCH] Improved tests. Improved schema.type getter. Remove duplicated Scalar code --- graphene/core/classtypes/base.py | 2 +- graphene/core/schema.py | 26 +++++++++----------- graphene/core/types/__init__.py | 5 ++-- graphene/core/types/scalars.py | 19 +------------- graphene/core/types/tests/test_field.py | 2 +- graphene/core/types/tests/test_scalars.py | 30 ++--------------------- 6 files changed, 19 insertions(+), 65 deletions(-) diff --git a/graphene/core/classtypes/base.py b/graphene/core/classtypes/base.py index 4f2de009..214edf6f 100644 --- a/graphene/core/classtypes/base.py +++ b/graphene/core/classtypes/base.py @@ -1,7 +1,7 @@ import copy import inspect -from functools import partial from collections import OrderedDict +from functools import partial import six diff --git a/graphene/core/schema.py b/graphene/core/schema.py index 1b0ce8f9..a8ef29e3 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -38,22 +38,20 @@ class Schema(object): def __repr__(self): return '' % (str(self.name), hash(self)) - def T(self, object_type): - if not object_type: + def T(self, _type): + if not _type: return - if inspect.isclass(object_type) and issubclass( - object_type, (BaseType, ClassType)) or isinstance( - object_type, BaseType): - if object_type not in self._types: - internal_type = object_type.internal_type(self) - self._types[object_type] = internal_type - is_objecttype = inspect.isclass( - object_type) and issubclass(object_type, ClassType) - if is_objecttype: - self.register(object_type) - return self._types[object_type] + is_classtype = inspect.isclass(_type) and issubclass(_type, ClassType) + is_instancetype = isinstance(_type, BaseType) + if is_classtype or is_instancetype: + if _type not in self._types: + internal_type = _type.internal_type(self) + self._types[_type] = internal_type + if is_classtype: + self.register(_type) + return self._types[_type] else: - return object_type + return _type @property def executor(self): diff --git a/graphene/core/types/__init__.py b/graphene/core/types/__init__.py index 9260476c..0ffa52bb 100644 --- a/graphene/core/types/__init__.py +++ b/graphene/core/types/__init__.py @@ -4,7 +4,7 @@ from .definitions import List, NonNull # Compatibility import from .objecttype import Interface, ObjectType, Mutation, InputObjectType -from .scalars import String, ID, Boolean, Int, Float, Scalar +from .scalars import String, ID, Boolean, Int, Float from .field import Field, InputField __all__ = [ @@ -26,5 +26,4 @@ __all__ = [ 'ID', 'Boolean', 'Int', - 'Float', - 'Scalar'] + 'Float'] diff --git a/graphene/core/types/scalars.py b/graphene/core/types/scalars.py index 75cd70a3..d0d315b4 100644 --- a/graphene/core/types/scalars.py +++ b/graphene/core/types/scalars.py @@ -1,5 +1,5 @@ from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID, - GraphQLInt, GraphQLScalarType, GraphQLString) + GraphQLInt, GraphQLString) from .base import MountedType @@ -22,20 +22,3 @@ class ID(MountedType): class Float(MountedType): T = GraphQLFloat - - -class Scalar(MountedType): - - @classmethod - def internal_type(cls, schema): - serialize = getattr(cls, 'serialize') - parse_literal = getattr(cls, 'parse_literal') - parse_value = getattr(cls, 'parse_value') - - return GraphQLScalarType( - name=cls.__name__, - description=cls.__doc__, - serialize=serialize, - parse_value=parse_value, - parse_literal=parse_literal - ) diff --git a/graphene/core/types/tests/test_field.py b/graphene/core/types/tests/test_field.py index 8253ed20..df72a6f7 100644 --- a/graphene/core/types/tests/test_field.py +++ b/graphene/core/types/tests/test_field.py @@ -13,7 +13,7 @@ from ..scalars import String def test_field_internal_type(): resolver = lambda *args: 'RESOLVED' - field = Field(String, description='My argument', resolver=resolver) + field = Field(String(), description='My argument', resolver=resolver) class Query(ObjectType): my_field = field diff --git a/graphene/core/types/tests/test_scalars.py b/graphene/core/types/tests/test_scalars.py index 8b8d930f..39fd6063 100644 --- a/graphene/core/types/tests/test_scalars.py +++ b/graphene/core/types/tests/test_scalars.py @@ -1,9 +1,9 @@ from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID, - GraphQLInt, GraphQLScalarType, GraphQLString) + GraphQLInt, GraphQLString) from graphene.core.schema import Schema -from ..scalars import ID, Boolean, Float, Int, Scalar, String +from ..scalars import ID, Boolean, Float, Int, String schema = Schema() @@ -26,29 +26,3 @@ def test_id_scalar(): def test_float_scalar(): assert schema.T(Float()) == GraphQLFloat - - -def test_custom_scalar(): - import datetime - from graphql.core.language import ast - - class DateTimeScalar(Scalar): - '''DateTimeScalar Documentation''' - @staticmethod - def serialize(dt): - return dt.isoformat() - - @staticmethod - def parse_literal(node): - if isinstance(node, ast.StringValue): - return datetime.datetime.strptime( - node.value, "%Y-%m-%dT%H:%M:%S.%f") - - @staticmethod - def parse_value(value): - return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f") - - scalar_type = schema.T(DateTimeScalar) - assert isinstance(scalar_type, GraphQLScalarType) - assert scalar_type.name == 'DateTimeScalar' - assert scalar_type.description == 'DateTimeScalar Documentation'