Adapt date and time scalar types to core-next

This commit is contained in:
Christoph Zwerschke 2019-06-30 21:32:46 +02:00 committed by Mel van Londen
parent d259bf6113
commit 34936092ca
2 changed files with 21 additions and 13 deletions

View File

@ -3,7 +3,8 @@ from __future__ import absolute_import
import datetime import datetime
from aniso8601 import parse_date, parse_datetime, parse_time from aniso8601 import parse_date, parse_datetime, parse_time
from graphql.language.ast import StringValueNode from graphql.error import INVALID
from graphql.language import StringValueNode
from .scalars import Scalar from .scalars import Scalar
@ -37,7 +38,7 @@ class Date(Scalar):
elif isinstance(value, str): elif isinstance(value, str):
return parse_date(value) return parse_date(value)
except ValueError: except ValueError:
return None return INVALID
class DateTime(Scalar): class DateTime(Scalar):
@ -67,7 +68,7 @@ class DateTime(Scalar):
elif isinstance(value, str): elif isinstance(value, str):
return parse_datetime(value) return parse_datetime(value)
except ValueError: except ValueError:
return None return INVALID
class Time(Scalar): class Time(Scalar):
@ -97,4 +98,4 @@ class Time(Scalar):
elif isinstance(value, str): elif isinstance(value, str):
return parse_time(value) return parse_time(value)
except ValueError: except ValueError:
return None return INVALID

View File

@ -76,12 +76,15 @@ def test_time_query(sample_time):
def test_bad_datetime_query(): def test_bad_datetime_query():
not_a_date = "Some string that's not a date" not_a_date = "Some string that's not a datetime"
result = schema.execute("""{ datetime(in: "%s") }""" % not_a_date) result = schema.execute("""{ datetime(in: "%s") }""" % not_a_date)
assert len(result.errors) == 1 assert result.errors and len(result.errors) == 1
assert isinstance(result.errors[0], GraphQLError) error = result.errors[0]
assert isinstance(error, GraphQLError)
assert error.message == (
"Expected type DateTime, found \"Some string that's not a datetime\".")
assert result.data is None assert result.data is None
@ -90,18 +93,22 @@ def test_bad_date_query():
result = schema.execute("""{ date(in: "%s") }""" % not_a_date) result = schema.execute("""{ date(in: "%s") }""" % not_a_date)
assert len(result.errors) == 1 error = result.errors[0]
assert isinstance(result.errors[0], GraphQLError) assert isinstance(error, GraphQLError)
assert error.message == (
"Expected type Date, found \"Some string that's not a date\".")
assert result.data is None assert result.data is None
def test_bad_time_query(): def test_bad_time_query():
not_a_date = "Some string that's not a date" not_a_date = "Some string that's not a time"
result = schema.execute("""{ time(at: "%s") }""" % not_a_date) result = schema.execute("""{ time(at: "%s") }""" % not_a_date)
assert len(result.errors) == 1 error = result.errors[0]
assert isinstance(result.errors[0], GraphQLError) assert isinstance(error, GraphQLError)
assert error.message == (
"Expected type Time, found \"Some string that's not a time\".")
assert result.data is None assert result.data is None
@ -174,7 +181,7 @@ def test_bad_variables(sample_date, sample_datetime, sample_time):
), ),
variables={"input": input_}, variables={"input": input_},
) )
assert len(result.errors) == 1 assert result.errors and len(result.errors) == 1
# when `input` is not JSON serializable formatting the error message in # when `input` is not JSON serializable formatting the error message in
# `graphql.utils.is_valid_value` line 79 fails with a TypeError # `graphql.utils.is_valid_value` line 79 fails with a TypeError
assert isinstance(result.errors[0], GraphQLError) assert isinstance(result.errors[0], GraphQLError)