Debian Codegraph @

tests/utilities/test_ast_from_value.py

raw
1 from math import inf, nan
2
3 from pytest import raises
4
5 from graphql.error import GraphQLError
6 from graphql.language import (
7 BooleanValueNode,
8 EnumValueNode,
9 FloatValueNode,
10 IntValueNode,
11 ListValueNode,
12 NameNode,
13 NullValueNode,
14 ObjectFieldNode,
15 ObjectValueNode,
16 StringValueNode,
17 )
18 from graphql.pyutils import Undefined
19 from graphql.type import (
20 GraphQLBoolean,
21 GraphQLEnumType,
22 GraphQLFloat,
23 GraphQLID,
24 GraphQLInputField,
25 GraphQLInputObjectType,
26 GraphQLInt,
27 GraphQLList,
28 GraphQLNonNull,
29 GraphQLScalarType,
30 GraphQLString,
31 )
32 from graphql.utilities import ast_from_value
33
34
35 def describe_ast_from_value():
36 def converts_boolean_values_to_asts():
37 assert ast_from_value(True, GraphQLBoolean) == BooleanValueNode(value=True)
38
39 assert ast_from_value(False, GraphQLBoolean) == BooleanValueNode(value=False)
40
41 assert ast_from_value(Undefined, GraphQLBoolean) is None
42
43 assert ast_from_value(None, GraphQLBoolean) == NullValueNode()
44
45 assert ast_from_value(0, GraphQLBoolean) == BooleanValueNode(value=False)
46
47 assert ast_from_value(1, GraphQLBoolean) == BooleanValueNode(value=True)
48
49 non_null_boolean = GraphQLNonNull(GraphQLBoolean)
50 assert ast_from_value(0, non_null_boolean) == BooleanValueNode(value=False)
51
52 def converts_int_values_to_int_asts():
53 assert ast_from_value(-1, GraphQLInt) == IntValueNode(value="-1")
54
55 assert ast_from_value(123.0, GraphQLInt) == IntValueNode(value="123")
56
57 assert ast_from_value(1e4, GraphQLInt) == IntValueNode(value="10000")
58
59 # GraphQL spec does not allow coercing non-integer values to Int to
60 # avoid accidental data loss.
61 with raises(GraphQLError) as exc_info:
62 assert ast_from_value(123.5, GraphQLInt)
63 msg = str(exc_info.value)
64 assert msg == "Int cannot represent non-integer value: 123.5"
65
66 # Note: outside the bounds of 32bit signed int.
67 with raises(GraphQLError) as exc_info:
68 assert ast_from_value(1e40, GraphQLInt)
69 msg = str(exc_info.value)
70 assert msg == "Int cannot represent non 32-bit signed integer value: 1e+40"
71
72 with raises(GraphQLError) as exc_info:
73 ast_from_value(nan, GraphQLInt)
74 msg = str(exc_info.value)
75 assert msg == "Int cannot represent non-integer value: nan"
76
77 def converts_float_values_to_float_asts():
78 # luckily in Python we can discern between float and int
79 assert ast_from_value(-1, GraphQLFloat) == FloatValueNode(value="-1")
80
81 assert ast_from_value(123.0, GraphQLFloat) == FloatValueNode(value="123")
82
83 assert ast_from_value(123.5, GraphQLFloat) == FloatValueNode(value="123.5")
84
85 assert ast_from_value(1e4, GraphQLFloat) == FloatValueNode(value="10000")
86
87 assert ast_from_value(1e40, GraphQLFloat) == FloatValueNode(value="1e+40")
88
89 assert ast_from_value(123456789.01234567, GraphQLFloat) == FloatValueNode(
90 value="123456789.01234567"
91 )
92
93 assert ast_from_value(1.1, GraphQLFloat) == FloatValueNode(value="1.1")
94
95 def converts_string_values_to_string_asts():
96 assert ast_from_value("hello", GraphQLString) == StringValueNode(value="hello")
97
98 assert ast_from_value("VALUE", GraphQLString) == StringValueNode(value="VALUE")
99
100 assert ast_from_value("VA\nLUE", GraphQLString) == StringValueNode(
101 value="VA\nLUE"
102 )
103
104 assert ast_from_value(123, GraphQLString) == StringValueNode(value="123")
105
106 assert ast_from_value(False, GraphQLString) == StringValueNode(value="false")
107
108 assert ast_from_value(None, GraphQLString) == NullValueNode()
109
110 assert ast_from_value(Undefined, GraphQLString) is None
111
112 def converts_id_values_to_int_or_string_asts():
113 assert ast_from_value("hello", GraphQLID) == StringValueNode(value="hello")
114
115 assert ast_from_value("VALUE", GraphQLID) == StringValueNode(value="VALUE")
116
117 # Note: EnumValues cannot contain non-identifier characters
118 assert ast_from_value("VA\nLUE", GraphQLID) == StringValueNode(value="VA\nLUE")
119
120 # Note: IntValues are used when possible.
121 assert ast_from_value(-1, GraphQLID) == IntValueNode(value="-1")
122
123 assert ast_from_value(123, GraphQLID) == IntValueNode(value="123")
124
125 assert ast_from_value("123", GraphQLID) == IntValueNode(value="123")
126
127 assert ast_from_value("01", GraphQLID) == StringValueNode(value="01")
128
129 with raises(GraphQLError) as exc_info:
130 assert ast_from_value(False, GraphQLID)
131 assert str(exc_info.value) == "ID cannot represent value: False"
132
133 assert ast_from_value(None, GraphQLID) == NullValueNode()
134
135 assert ast_from_value(Undefined, GraphQLString) is None
136
137 def converts_using_serialize_from_a_custom_scalar_type():
138 pass_through_scalar = GraphQLScalarType(
139 "PassThroughScalar",
140 serialize=lambda value: value,
141 )
142
143 assert ast_from_value("value", pass_through_scalar) == StringValueNode(
144 value="value"
145 )
146
147 with raises(TypeError) as exc_info:
148 assert ast_from_value(nan, pass_through_scalar)
149 assert str(exc_info.value) == "Cannot convert value to AST: nan."
150
151 with raises(TypeError) as exc_info:
152 ast_from_value(inf, pass_through_scalar)
153 assert str(exc_info.value) == "Cannot convert value to AST: inf."
154
155 return_null_scalar = GraphQLScalarType(
156 "ReturnNullScalar",
157 serialize=lambda value: None,
158 )
159
160 assert ast_from_value("value", return_null_scalar) is None
161
162 class SomeClass:
163 pass
164
165 return_custom_class_scalar = GraphQLScalarType(
166 "ReturnCustomClassScalar",
167 serialize=lambda value: SomeClass(),
168 )
169
170 with raises(TypeError) as exc_info:
171 ast_from_value("value", return_custom_class_scalar)
172 msg = str(exc_info.value)
173 assert msg == "Cannot convert value to AST: <SomeClass instance>."
174
175 def does_not_convert_non_null_values_to_null_value():
176 non_null_boolean = GraphQLNonNull(GraphQLBoolean)
177 assert ast_from_value(None, non_null_boolean) is None
178
179 complex_value = {"someArbitrary": "complexValue"}
180
181 my_enum = GraphQLEnumType(
182 "MyEnum", {"HELLO": None, "GOODBYE": None, "COMPLEX": complex_value}
183 )
184
185 def converts_string_values_to_enum_asts_if_possible():
186 assert ast_from_value("HELLO", my_enum) == EnumValueNode(value="HELLO")
187
188 assert ast_from_value(complex_value, my_enum) == EnumValueNode(value="COMPLEX")
189
190 # Note: case sensitive
191 with raises(GraphQLError) as exc_info:
192 ast_from_value("hello", my_enum)
193 assert exc_info.value.message == "Enum 'MyEnum' cannot represent value: 'hello'"
194
195 # Note: not a valid enum value
196 with raises(GraphQLError) as exc_info:
197 ast_from_value("UNKNOWN_VALUE", my_enum)
198 assert (
199 exc_info.value.message
200 == "Enum 'MyEnum' cannot represent value: 'UNKNOWN_VALUE'"
201 )
202
203 def converts_list_values_to_list_asts():
204 assert ast_from_value(
205 ["FOO", "BAR"], GraphQLList(GraphQLString)
206 ) == ListValueNode(
207 values=[StringValueNode(value="FOO"), StringValueNode(value="BAR")]
208 )
209
210 assert ast_from_value(
211 ["HELLO", "GOODBYE"], GraphQLList(my_enum)
212 ) == ListValueNode(
213 values=[EnumValueNode(value="HELLO"), EnumValueNode(value="GOODBYE")]
214 )
215
216 def list_generator():
217 yield 1
218 yield 2
219 yield 3
220
221 assert ast_from_value(list_generator(), GraphQLList(GraphQLInt)) == (
222 ListValueNode(
223 values=[
224 IntValueNode(value="1"),
225 IntValueNode(value="2"),
226 IntValueNode(value="3"),
227 ]
228 )
229 )
230
231 def converts_list_singletons():
232 assert ast_from_value("FOO", GraphQLList(GraphQLString)) == StringValueNode(
233 value="FOO"
234 )
235
236 def skips_invalid_list_items():
237 ast = ast_from_value(
238 ["FOO", None, "BAR"], GraphQLList(GraphQLNonNull(GraphQLString))
239 )
240
241 assert ast == ListValueNode(
242 values=[StringValueNode(value="FOO"), StringValueNode(value="BAR")]
243 )
244
245 input_obj = GraphQLInputObjectType(
246 "MyInputObj",
247 {"foo": GraphQLInputField(GraphQLFloat), "bar": GraphQLInputField(my_enum)},
248 )
249
250 def converts_input_objects():
251 assert ast_from_value({"foo": 3, "bar": "HELLO"}, input_obj) == ObjectValueNode(
252 fields=[
253 ObjectFieldNode(
254 name=NameNode(value="foo"), value=FloatValueNode(value="3")
255 ),
256 ObjectFieldNode(
257 name=NameNode(value="bar"), value=EnumValueNode(value="HELLO")
258 ),
259 ]
260 )
261
262 def converts_input_objects_with_explicit_nulls():
263 assert ast_from_value({"foo": None}, input_obj) == ObjectValueNode(
264 fields=[ObjectFieldNode(name=NameNode(value="foo"), value=NullValueNode())]
265 )
266
267 def does_not_convert_non_object_values_as_input_objects():
268 assert ast_from_value(5, input_obj) is None