Skip to content

Commit 75448d0

Browse files
committed
Implement test_enum_type and fix issue in GraphQLEnumType.parse_value()
#23
1 parent ae02a3a commit 75448d0

File tree

3 files changed

+164
-3
lines changed

3 files changed

+164
-3
lines changed

graphql/core/execution/values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_variable_value(schema, definition_ast, input):
9797
)
9898

9999
raise GraphQLError(
100-
'Variable "${}" expected value of type "{}" but got: {}'.format(
100+
'Variable "${}" expected value of type "{}" but got: {}.'.format(
101101
variable.name.value,
102102
print_ast(definition_ast.type),
103103
json.dumps(input)

graphql/core/type/definition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,10 @@ def serialize(self, value):
481481

482482
def parse_value(self, value):
483483
if isinstance(value, collections.Hashable):
484-
enum_value = self._get_value_lookup().get(value)
484+
enum_value = self._get_name_lookup().get(value)
485485

486486
if enum_value:
487-
return enum_value.name
487+
return enum_value.value
488488

489489
return None
490490

tests/core_type/test_enum_type.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
from collections import OrderedDict
2+
from pytest import raises
3+
from graphql.core.type import (
4+
GraphQLEnumType,
5+
GraphQLEnumValue,
6+
GraphQLObjectType,
7+
GraphQLField,
8+
GraphQLArgument,
9+
GraphQLInt,
10+
GraphQLString,
11+
GraphQLSchema
12+
)
13+
14+
from graphql.core import graphql
15+
16+
ColorType = GraphQLEnumType(
17+
name='Color',
18+
values=OrderedDict([
19+
('RED', GraphQLEnumValue(0)),
20+
('GREEN', GraphQLEnumValue(1)),
21+
('BLUE', GraphQLEnumValue(2))
22+
])
23+
)
24+
25+
26+
def get_first(args, *keys):
27+
for key in keys:
28+
if key in args:
29+
return args[key]
30+
31+
return None
32+
33+
34+
QueryType = GraphQLObjectType(
35+
name='Query',
36+
fields={
37+
'colorEnum': GraphQLField(
38+
type=ColorType,
39+
args={
40+
'fromEnum': GraphQLArgument(ColorType),
41+
'fromInt': GraphQLArgument(GraphQLInt),
42+
'fromString': GraphQLArgument(GraphQLString)
43+
},
44+
resolver=lambda value, args, info: get_first(args, 'fromInt', 'fromString', 'fromEnum')
45+
),
46+
'colorInt': GraphQLField(
47+
type=GraphQLInt,
48+
args={
49+
'fromEnum': GraphQLArgument(ColorType),
50+
'fromInt': GraphQLArgument(GraphQLInt),
51+
},
52+
resolver=lambda value, args, info: get_first(args, 'fromInt', 'fromEnum')
53+
)
54+
}
55+
)
56+
57+
MutationType = GraphQLObjectType(
58+
name='Mutation',
59+
fields={
60+
'favoriteEnum': GraphQLField(
61+
type=ColorType,
62+
args={
63+
'color': GraphQLArgument(ColorType)
64+
},
65+
resolver=lambda value, args, info: args.get('color')
66+
)
67+
}
68+
)
69+
70+
Schema = GraphQLSchema(query=QueryType, mutation=MutationType)
71+
72+
73+
def test_accepts_enum_literals_as_input():
74+
result = graphql(Schema, '{ colorInt(fromEnum: GREEN) }')
75+
assert not result.errors
76+
assert result.data == {
77+
'colorInt': 1
78+
}
79+
80+
81+
def test_enum_may_be_output_type():
82+
result = graphql(Schema, '{ colorEnum(fromInt: 1) }')
83+
assert not result.errors
84+
assert result.data == {
85+
'colorEnum': 'GREEN'
86+
}
87+
88+
89+
def test_enum_may_be_both_input_and_output_type():
90+
result = graphql(Schema, '{ colorEnum(fromEnum: GREEN) }')
91+
92+
assert not result.errors
93+
assert result.data == {
94+
'colorEnum': 'GREEN'
95+
}
96+
97+
98+
def test_does_not_accept_string_literals():
99+
result = graphql(Schema, '{ colorEnum(fromEnum: "GREEN") }')
100+
assert not result.data
101+
assert result.errors[0].message == 'Argument "fromEnum" expected type "Color" but got: "GREEN".'
102+
103+
104+
def test_does_not_accept_incorrect_internal_value():
105+
result = graphql(Schema, '{ colorEnum(fromString: "GREEN") }')
106+
assert result.data == {'colorEnum': None}
107+
assert not result.errors
108+
109+
110+
def test_does_not_accept_internal_value_in_placeof_enum_literal():
111+
result = graphql(Schema, '{ colorEnum(fromEnum: 1) }')
112+
assert not result.data
113+
assert result.errors[0].message == 'Argument "fromEnum" expected type "Color" but got: 1.'
114+
115+
116+
def test_does_not_accept_enum_literal_in_place_of_int():
117+
result = graphql(Schema, '{ colorEnum(fromInt: GREEN) }')
118+
assert not result.data
119+
assert result.errors[0].message == 'Argument "fromInt" expected type "Int" but got: GREEN.'
120+
121+
122+
def test_accepts_json_string_as_enum_variable():
123+
result = graphql(Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', None, {'color': 'BLUE'})
124+
assert not result.errors
125+
assert result.data == {'colorEnum': 'BLUE'}
126+
127+
128+
def test_accepts_enum_literals_as_input_arguments_to_mutations():
129+
result = graphql(Schema, 'mutation x($color: Color!) { favoriteEnum(color: $color) }', None, {'color': 'GREEN'})
130+
assert not result.errors
131+
assert result.data == {'favoriteEnum': 'GREEN'}
132+
133+
134+
def test_does_not_accept_internal_value_as_enum_variable():
135+
result = graphql(Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', None, {'color': 2})
136+
assert not result.data
137+
assert result.errors[0].message == 'Variable "$color" expected value of type "Color!" but got: 2.'
138+
139+
140+
def test_does_not_accept_string_variables_as_enum_input():
141+
result = graphql(Schema, 'query test($color: String!) { colorEnum(fromEnum: $color) }', None, {'color': 'BLUE'})
142+
assert not result.data
143+
assert result.errors[0].message == 'Variable "color" of type "String!" used in position expecting type "Color".'
144+
145+
146+
def test_does_not_accept_internal_value_as_enum_input():
147+
result = graphql(Schema, 'query test($color: Int!) { colorEnum(fromEnum: $color) }', None, {'color': 2})
148+
assert not result.data
149+
assert result.errors[0].message == 'Variable "color" of type "Int!" used in position expecting type "Color".'
150+
151+
152+
def test_enum_value_may_have_an_internal_value_of_0():
153+
result = graphql(Schema, '{ colorEnum(fromEnum: RED) colorInt(fromEnum: RED) }')
154+
assert not result.errors
155+
assert result.data == {'colorEnum': 'RED', 'colorInt': 0}
156+
157+
158+
def test_enum_inputs_may_be_nullable():
159+
result = graphql(Schema, '{ colorEnum colorInt }')
160+
assert not result.errors
161+
assert result.data == {'colorEnum': None, 'colorInt': None}

0 commit comments

Comments
 (0)