mirror of
https://github.com/graphql-python/graphene.git
synced 2025-09-21 11:22:33 +03:00
Adapt date and time scalar types to core-next
This commit is contained in:
parent
d259bf6113
commit
34936092ca
|
@ -3,7 +3,8 @@ from __future__ import absolute_import
|
|||
import datetime
|
||||
|
||||
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
|
||||
|
||||
|
@ -37,7 +38,7 @@ class Date(Scalar):
|
|||
elif isinstance(value, str):
|
||||
return parse_date(value)
|
||||
except ValueError:
|
||||
return None
|
||||
return INVALID
|
||||
|
||||
|
||||
class DateTime(Scalar):
|
||||
|
@ -67,7 +68,7 @@ class DateTime(Scalar):
|
|||
elif isinstance(value, str):
|
||||
return parse_datetime(value)
|
||||
except ValueError:
|
||||
return None
|
||||
return INVALID
|
||||
|
||||
|
||||
class Time(Scalar):
|
||||
|
@ -97,4 +98,4 @@ class Time(Scalar):
|
|||
elif isinstance(value, str):
|
||||
return parse_time(value)
|
||||
except ValueError:
|
||||
return None
|
||||
return INVALID
|
||||
|
|
|
@ -76,12 +76,15 @@ def test_time_query(sample_time):
|
|||
|
||||
|
||||
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)
|
||||
|
||||
assert len(result.errors) == 1
|
||||
assert isinstance(result.errors[0], GraphQLError)
|
||||
assert result.errors and len(result.errors) == 1
|
||||
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
|
||||
|
||||
|
||||
|
@ -90,18 +93,22 @@ def test_bad_date_query():
|
|||
|
||||
result = schema.execute("""{ date(in: "%s") }""" % not_a_date)
|
||||
|
||||
assert len(result.errors) == 1
|
||||
assert isinstance(result.errors[0], GraphQLError)
|
||||
error = result.errors[0]
|
||||
assert isinstance(error, GraphQLError)
|
||||
assert error.message == (
|
||||
"Expected type Date, found \"Some string that's not a date\".")
|
||||
assert result.data is None
|
||||
|
||||
|
||||
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)
|
||||
|
||||
assert len(result.errors) == 1
|
||||
assert isinstance(result.errors[0], GraphQLError)
|
||||
error = result.errors[0]
|
||||
assert isinstance(error, GraphQLError)
|
||||
assert error.message == (
|
||||
"Expected type Time, found \"Some string that's not a time\".")
|
||||
assert result.data is None
|
||||
|
||||
|
||||
|
@ -174,7 +181,7 @@ def test_bad_variables(sample_date, sample_datetime, sample_time):
|
|||
),
|
||||
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
|
||||
# `graphql.utils.is_valid_value` line 79 fails with a TypeError
|
||||
assert isinstance(result.errors[0], GraphQLError)
|
||||
|
|
Loading…
Reference in New Issue
Block a user