mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Improved Scalars tests
This commit is contained in:
parent
2172edc033
commit
ee46d8c7be
|
@ -1,21 +1,65 @@
|
||||||
|
import datetime
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from graphene.utils.get_graphql_type import get_graphql_type
|
from graphene.utils.get_graphql_type import get_graphql_type
|
||||||
from graphql import GraphQLBoolean, GraphQLFloat, GraphQLInt, GraphQLString
|
from graphql import GraphQLBoolean, GraphQLFloat, GraphQLInt, GraphQLString, GraphQLScalarType, graphql
|
||||||
|
from graphql.language import ast
|
||||||
|
|
||||||
from ..field import Field
|
from ..field import Field
|
||||||
from ..objecttype import ObjectType
|
from ..objecttype import ObjectType
|
||||||
from ..scalars import Boolean, Float, Int, Scalar, String
|
from ..scalars import Boolean, Float, Int, Scalar, String
|
||||||
|
from ..schema import Schema
|
||||||
|
|
||||||
|
|
||||||
class DatetimeScalar(Scalar):
|
class DatetimeScalar(Scalar):
|
||||||
|
class Meta:
|
||||||
|
name = 'DateTime'
|
||||||
|
|
||||||
def serialize(value):
|
@staticmethod
|
||||||
return value.isoformat()
|
def serialize(dt):
|
||||||
|
assert isinstance(dt, datetime.datetime)
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_date_time(dt):
|
||||||
|
assert isinstance(dt, datetime.datetime)
|
||||||
|
return dt.isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def parse_literal(node):
|
||||||
|
if isinstance(node, ast.StringValue):
|
||||||
|
return datetime.datetime.strptime(node.value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||||
|
|
||||||
|
|
||||||
|
def parse_value(value):
|
||||||
|
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||||
|
|
||||||
|
|
||||||
|
GraphQLDateTimeType = GraphQLScalarType(
|
||||||
|
name='DateTime',
|
||||||
|
serialize=serialize_date_time,
|
||||||
|
parse_literal=parse_literal,
|
||||||
|
parse_value=parse_value
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class DatetimeScalarGraphQL(Scalar):
|
||||||
|
class Meta:
|
||||||
|
graphql_type = GraphQLDateTimeType
|
||||||
|
|
||||||
|
|
||||||
scalar_classes = {
|
scalar_classes = {
|
||||||
DatetimeScalar: DatetimeScalar._meta.graphql_type,
|
DatetimeScalar: DatetimeScalar._meta.graphql_type,
|
||||||
|
DatetimeScalarGraphQL: GraphQLDateTimeType,
|
||||||
String: GraphQLString,
|
String: GraphQLString,
|
||||||
Int: GraphQLInt,
|
Int: GraphQLInt,
|
||||||
Float: GraphQLFloat,
|
Float: GraphQLFloat,
|
||||||
|
@ -55,3 +99,41 @@ def test_custom_scalar_empty():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
assert """DatetimeScalar must provide "serialize" function.""" in str(excinfo.value)
|
assert """DatetimeScalar must provide "serialize" function.""" in str(excinfo.value)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("scalar_class", (DatetimeScalar, DatetimeScalarGraphQL))
|
||||||
|
def test_custom_scalar_empty(scalar_class):
|
||||||
|
class Query(ObjectType):
|
||||||
|
datetime = scalar_class(_in=scalar_class(name='in'))
|
||||||
|
|
||||||
|
def resolve_datetime(self, args, context, info):
|
||||||
|
return args.get('in')
|
||||||
|
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
isoformat = now.isoformat()
|
||||||
|
|
||||||
|
schema = Schema(query=Query)
|
||||||
|
|
||||||
|
response = graphql(schema, '''
|
||||||
|
{
|
||||||
|
datetime(in: "%s")
|
||||||
|
}
|
||||||
|
''' % isoformat)
|
||||||
|
|
||||||
|
assert not response.errors
|
||||||
|
assert response.data == {
|
||||||
|
'datetime': isoformat
|
||||||
|
}
|
||||||
|
|
||||||
|
response = graphql(schema, '''
|
||||||
|
query Test($date: DateTime) {
|
||||||
|
datetime(in: $date)
|
||||||
|
}
|
||||||
|
''', variable_values={
|
||||||
|
'date': isoformat
|
||||||
|
})
|
||||||
|
|
||||||
|
assert not response.errors
|
||||||
|
assert response.data == {
|
||||||
|
'datetime': isoformat
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user