From 34936092cadf16a300e734691609dbf74c53aa40 Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Sun, 30 Jun 2019 21:32:46 +0200 Subject: [PATCH] Adapt date and time scalar types to core-next --- graphene/types/datetime.py | 9 +++++---- graphene/types/tests/test_datetime.py | 25 ++++++++++++++++--------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/graphene/types/datetime.py b/graphene/types/datetime.py index fecdd88a..c533d23e 100644 --- a/graphene/types/datetime.py +++ b/graphene/types/datetime.py @@ -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 diff --git a/graphene/types/tests/test_datetime.py b/graphene/types/tests/test_datetime.py index bb6f212c..c08af7e7 100644 --- a/graphene/types/tests/test_datetime.py +++ b/graphene/types/tests/test_datetime.py @@ -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)