Skip to content

Update enum tests #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions graphql/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,15 @@ def complete_leaf_value(return_type, result):
"""
Complete a Scalar or Enum by serializing to a valid value, returning null if serialization is not possible.
"""
# serialize = getattr(return_type, 'serialize', None)
# assert serialize, 'Missing serialize method on type'
assert hasattr(return_type, 'serialize'), 'Missing serialize method on type'
serialized_result = return_type.serialize(result)

return return_type.serialize(result)
if serialized_result is None:
raise GraphQLError(
('Expected a value of type "{}" but ' +
'received: {}').format(return_type, result)
)
return serialized_result


def complete_abstract_value(exe_context, return_type, field_asts, info, result):
Expand Down
19 changes: 17 additions & 2 deletions graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ def __init__(self, name, values, description=None):

self.values = define_enum_values(self, values)

def get_values(self):
return self.values

def get_value(self, name):
return self._name_lookup.get(name)

def serialize(self, value):
if isinstance(value, collections.Hashable):
enum_value = self._value_lookup.get(value)
Expand Down Expand Up @@ -463,23 +469,32 @@ def define_enum_values(type, value_map):
)
value = copy.copy(value)
value.name = value_name
if value.value is None:
if value.value == Undefined:
value.value = value_name

values.append(value)

return values


class Undefined(object):
"""A representation of an Undefined value distinct from a None value"""
pass


class GraphQLEnumValue(object):
__slots__ = 'name', 'value', 'deprecation_reason', 'description'

def __init__(self, value=None, deprecation_reason=None, description=None, name=None):
def __init__(self, value=Undefined, deprecation_reason=None, description=None, name=None):
self.name = name
self.value = value
self.deprecation_reason = deprecation_reason
self.description = description

@property
def is_deprecated(self):
return bool(self.deprecation_reason)

def __eq__(self, other):
return (
self is other or (
Expand Down
59 changes: 42 additions & 17 deletions graphql/type/tests/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ def test_defines_a_subscription_schema():
# assert subscription.name == 'articleSubscribe'


def test_defines_an_enum_type_with_deprecated_value():
EnumTypeWithDeprecatedValue = GraphQLEnumType('EnumWithDeprecatedValue', {
'foo': GraphQLEnumValue(deprecation_reason='Just because'),
})
value = EnumTypeWithDeprecatedValue.get_values()[0]
assert value.name == 'foo'
assert value.description is None
assert value.is_deprecated is True
assert value.deprecation_reason == 'Just because'
assert value.value == 'foo'


def test_defines_an_enum_type_with_a_value_of_none():
EnumTypeWithNoneValue = GraphQLEnumType('EnumWithNullishValue', {
'NULL': GraphQLEnumValue(None),
})

value = EnumTypeWithNoneValue.get_values()[0]
assert value.name == 'NULL'
assert value.description is None
assert value.is_deprecated is False
assert value.deprecation_reason is None
assert value.value is None


def test_defines_an_object_type_with_deprecated_field():
TypeWithDeprecatedField = GraphQLObjectType('foo', fields={
'bar': GraphQLField(
Expand Down Expand Up @@ -178,6 +203,23 @@ def test_includes_nested_input_objects_in_the_map():
assert schema.get_type_map()['NestedInputObject'] is NestedInputObject


def test_includes_interface_possible_types_in_the_type_map():
SomeInterface = GraphQLInterfaceType('SomeInterface', fields={'f': GraphQLField(GraphQLInt)})
SomeSubtype = GraphQLObjectType(
name='SomeSubtype',
fields={'f': GraphQLField(GraphQLInt)},
interfaces=[SomeInterface],
is_type_of=lambda: None
)
schema = GraphQLSchema(
query=GraphQLObjectType(
name='Query',
fields={
'iface': GraphQLField(SomeInterface)}),
types=[SomeSubtype])
assert schema.get_type_map()['SomeSubtype'] == SomeSubtype


def test_includes_interfaces_thunk_subtypes_in_the_type_map():
SomeInterface = GraphQLInterfaceType(
name='SomeInterface',
Expand Down Expand Up @@ -205,23 +247,6 @@ def test_includes_interfaces_thunk_subtypes_in_the_type_map():
assert schema.get_type_map()['SomeSubtype'] is SomeSubtype


def test_includes_interfaces_subtypes_in_the_type_map():
SomeInterface = GraphQLInterfaceType('SomeInterface', fields={'f': GraphQLField(GraphQLInt)})
SomeSubtype = GraphQLObjectType(
name='SomeSubtype',
fields={'f': GraphQLField(GraphQLInt)},
interfaces=[SomeInterface],
is_type_of=lambda: None
)
schema = GraphQLSchema(
query=GraphQLObjectType(
name='Query',
fields={
'iface': GraphQLField(SomeInterface)}),
types=[SomeSubtype])
assert schema.get_type_map()['SomeSubtype'] == SomeSubtype


def test_stringifies_simple_types():
assert str(GraphQLInt) == 'Int'
assert str(BlogArticle) == 'Article'
Expand Down
47 changes: 42 additions & 5 deletions graphql/type/tests/test_enum_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections import OrderedDict

from rx import Observable
from pytest import raises

from graphql import graphql
from graphql.type import (GraphQLArgument, GraphQLEnumType, GraphQLEnumValue,
Expand Down Expand Up @@ -114,13 +113,28 @@ def test_does_not_accept_string_literals():
'Expected type "Color", found "GREEN".'


def test_does_not_accept_values_not_in_the_enum():
result = graphql(Schema, '{ colorEnum(fromEnum: GREENISH) }')
assert not result.data
assert result.errors[0].message == 'Argument "fromEnum" has invalid value GREENISH.\n' \
'Expected type "Color", found GREENISH.'


def test_does_not_accept_values_with_incorrect_casing():
result = graphql(Schema, '{ colorEnum(fromEnum: green) }')
assert not result.data
assert result.errors[0].message == 'Argument "fromEnum" has invalid value green.\n' \
'Expected type "Color", found green.'


def test_does_not_accept_incorrect_internal_value():
result = graphql(Schema, '{ colorEnum(fromString: "GREEN") }')
assert result.data == {'colorEnum': None}
assert not result.errors
assert result.errors[0].message == 'Expected a value of type "Color" ' \
'but received: GREEN'


def test_does_not_accept_internal_value_in_placeof_enum_literal():
def test_does_not_accept_internal_value_in_place_of_enum_literal():
result = graphql(Schema, '{ colorEnum(fromEnum: 1) }')
assert not result.data
assert result.errors[0].message == 'Argument "fromEnum" has invalid value 1.\n' \
Expand All @@ -135,8 +149,11 @@ def test_does_not_accept_enum_literal_in_place_of_int():


def test_accepts_json_string_as_enum_variable():
result = graphql(Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', variable_values={
'color': 'BLUE'})
result = graphql(
Schema,
'query test($color: Color!) { colorEnum(fromEnum: $color) }',
variable_values={'color': 'BLUE'}
)
assert not result.errors
assert result.data == {'colorEnum': 'BLUE'}

Expand Down Expand Up @@ -195,6 +212,26 @@ def test_enum_inputs_may_be_nullable():
assert result.data == {'colorEnum': None, 'colorInt': None}


def test_presents_a_get_values_api():
values = ColorType.get_values()
assert len(values) == 3
assert values[0].name == 'RED'
assert values[0].value == 0
assert values[1].name == 'GREEN'
assert values[1].value == 1
assert values[2].name == 'BLUE'
assert values[2].value == 2


def test_presents_a_get_value_api():
oneValue = ColorType.get_value('RED')
assert oneValue.name == 'RED'
assert oneValue.value == 0

badUsage = ColorType.get_value(0)
assert badUsage is None


def test_sorts_values_if_not_using_ordered_dict():
enum = GraphQLEnumType(name='Test', values={
'c': GraphQLEnumValue(),
Expand Down