Skip to content

Commit d2a1766

Browse files
committed
test_value_from_ast: improve readability
Replicates graphql/graphql-js@f462347
1 parent 670d5f9 commit d2a1766

File tree

1 file changed

+108
-115
lines changed

1 file changed

+108
-115
lines changed

tests/utilities/test_value_from_ast.py

Lines changed: 108 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,56 @@
1818

1919

2020
def describe_value_from_ast():
21-
def _test_case(type_, value_text, expected):
22-
value_node = parse_value(value_text)
23-
value = value_from_ast(value_node, type_)
24-
if isinstance(expected, float) and isnan(expected):
25-
assert isnan(value)
26-
else:
27-
assert value == expected
28-
29-
def _test_case_with_vars(variables, type_, value_text, expected):
30-
value_node = parse_value(value_text)
31-
assert value_from_ast(value_node, type_, variables) == expected
21+
def _value_from(value_text, type_, variables=None):
22+
ast = parse_value(value_text)
23+
return value_from_ast(ast, type_, variables)
3224

3325
def rejects_empty_input():
3426
# noinspection PyTypeChecker
3527
assert value_from_ast(None, GraphQLBoolean) is Undefined
3628

3729
def converts_according_to_input_coercion_rules():
38-
_test_case(GraphQLBoolean, "true", True)
39-
_test_case(GraphQLBoolean, "false", False)
40-
_test_case(GraphQLInt, "123", 123)
41-
_test_case(GraphQLFloat, "123", 123)
42-
_test_case(GraphQLFloat, "123.456", 123.456)
43-
_test_case(GraphQLString, '"abc123"', "abc123")
44-
_test_case(GraphQLID, "123456", "123456")
45-
_test_case(GraphQLID, '"123456"', "123456")
30+
assert _value_from("true", GraphQLBoolean) is True
31+
assert _value_from("false", GraphQLBoolean) is False
32+
assert _value_from("123", GraphQLInt) == 123
33+
assert _value_from("123", GraphQLFloat) == 123
34+
assert _value_from("123.456", GraphQLFloat) == 123.456
35+
assert _value_from('"abc123"', GraphQLString) == "abc123"
36+
assert _value_from("123456", GraphQLID) == "123456"
37+
assert _value_from('"123456"', GraphQLID) == "123456"
4638

4739
def does_not_convert_when_input_coercion_rules_reject_a_value():
48-
_test_case(GraphQLBoolean, "123", Undefined)
49-
_test_case(GraphQLInt, "123.456", Undefined)
50-
_test_case(GraphQLInt, "true", Undefined)
51-
_test_case(GraphQLInt, '"123"', Undefined)
52-
_test_case(GraphQLFloat, '"123"', Undefined)
53-
_test_case(GraphQLString, "123", Undefined)
54-
_test_case(GraphQLString, "true", Undefined)
55-
_test_case(GraphQLID, "123.456", Undefined)
56-
57-
test_enum = GraphQLEnumType(
58-
"TestColor",
59-
{
60-
"RED": 1,
61-
"GREEN": 2,
62-
"BLUE": 3,
63-
"NULL": None,
64-
"NAN": nan,
65-
"NO_CUSTOM_VALUE": Undefined,
66-
},
67-
)
40+
assert _value_from("123", GraphQLBoolean) is Undefined
41+
assert _value_from("123.456", GraphQLInt) is Undefined
42+
assert _value_from("true", GraphQLInt) is Undefined
43+
assert _value_from('"123"', GraphQLInt) is Undefined
44+
assert _value_from('"123"', GraphQLFloat) is Undefined
45+
assert _value_from("123", GraphQLString) is Undefined
46+
assert _value_from("true", GraphQLString) is Undefined
47+
assert _value_from("123.456", GraphQLID) is Undefined
6848

6949
def converts_enum_values_according_to_input_coercion_rules():
70-
_test_case(test_enum, "RED", 1)
71-
_test_case(test_enum, "BLUE", 3)
72-
_test_case(test_enum, "YELLOW", Undefined)
73-
_test_case(test_enum, "3", Undefined)
74-
_test_case(test_enum, '"BLUE"', Undefined)
75-
_test_case(test_enum, "null", None)
76-
_test_case(test_enum, "NULL", None)
77-
_test_case(test_enum, "NAN", nan)
78-
_test_case(test_enum, "NO_CUSTOM_VALUE", Undefined)
50+
test_enum = GraphQLEnumType(
51+
"TestColor",
52+
{
53+
"RED": 1,
54+
"GREEN": 2,
55+
"BLUE": 3,
56+
"NULL": None,
57+
"NAN": nan,
58+
"NO_CUSTOM_VALUE": Undefined,
59+
},
60+
)
61+
62+
assert _value_from("RED", test_enum) == 1
63+
assert _value_from("BLUE", test_enum) == 3
64+
assert _value_from("YELLOW", test_enum) is Undefined
65+
assert _value_from("3", test_enum) is Undefined
66+
assert _value_from('"BLUE"', test_enum) is Undefined
67+
assert _value_from("null", test_enum) is None
68+
assert _value_from("NULL", test_enum) is None
69+
assert isnan(_value_from("NAN", test_enum))
70+
assert _value_from("NO_CUSTOM_VALUE", test_enum) is Undefined
7971

8072
# Boolean!
8173
non_null_bool = GraphQLNonNull(GraphQLBoolean)
@@ -89,41 +81,44 @@ def converts_enum_values_according_to_input_coercion_rules():
8981
non_null_list_of_non_mull_bool = GraphQLNonNull(list_of_non_null_bool)
9082

9183
def coerces_to_null_unless_non_null():
92-
_test_case(GraphQLBoolean, "null", None)
93-
_test_case(non_null_bool, "null", Undefined)
84+
assert _value_from("null", GraphQLBoolean) is None
85+
assert _value_from("null", non_null_bool) is Undefined
9486

9587
def coerces_lists_of_values():
96-
_test_case(list_of_bool, "true", [True])
97-
_test_case(list_of_bool, "123", Undefined)
98-
_test_case(list_of_bool, "null", None)
99-
_test_case(list_of_bool, "[true, false]", [True, False])
100-
_test_case(list_of_bool, "[true, 123]", Undefined)
101-
_test_case(list_of_bool, "[true, null]", [True, None])
102-
_test_case(list_of_bool, "{ true: true }", Undefined)
88+
assert _value_from("true", list_of_bool) == [True]
89+
assert _value_from("123", list_of_bool) is Undefined
90+
assert _value_from("null", list_of_bool) is None
91+
assert _value_from("[true, false]", list_of_bool) == [True, False]
92+
assert _value_from("[true, 123]", list_of_bool) is Undefined
93+
assert _value_from("[true, null]", list_of_bool) == [True, None]
94+
assert _value_from("{ true: true }", list_of_bool) is Undefined
10395

10496
def coerces_non_null_lists_of_values():
105-
_test_case(non_null_list_of_bool, "true", [True])
106-
_test_case(non_null_list_of_bool, "123", Undefined)
107-
_test_case(non_null_list_of_bool, "null", Undefined)
108-
_test_case(non_null_list_of_bool, "[true, false]", [True, False])
109-
_test_case(non_null_list_of_bool, "[true, 123]", Undefined)
110-
_test_case(non_null_list_of_bool, "[true, null]", [True, None])
97+
assert _value_from("true", non_null_list_of_bool) == [True]
98+
assert _value_from("123", non_null_list_of_bool) is Undefined
99+
assert _value_from("null", non_null_list_of_bool) is Undefined
100+
assert _value_from("[true, false]", non_null_list_of_bool) == [True, False]
101+
assert _value_from("[true, 123]", non_null_list_of_bool) is Undefined
102+
assert _value_from("[true, null]", non_null_list_of_bool) == [True, None]
111103

112104
def coerces_lists_of_non_null_values():
113-
_test_case(list_of_non_null_bool, "true", [True])
114-
_test_case(list_of_non_null_bool, "123", Undefined)
115-
_test_case(list_of_non_null_bool, "null", None)
116-
_test_case(list_of_non_null_bool, "[true, false]", [True, False])
117-
_test_case(list_of_non_null_bool, "[true, 123]", Undefined)
118-
_test_case(list_of_non_null_bool, "[true, null]", Undefined)
105+
assert _value_from("true", list_of_non_null_bool) == [True]
106+
assert _value_from("123", list_of_non_null_bool) is Undefined
107+
assert _value_from("null", list_of_non_null_bool) is None
108+
assert _value_from("[true, false]", list_of_non_null_bool) == [True, False]
109+
assert _value_from("[true, 123]", list_of_non_null_bool) is Undefined
110+
assert _value_from("[true, null]", list_of_non_null_bool) is Undefined
119111

120112
def coerces_non_null_lists_of_non_null_values():
121-
_test_case(non_null_list_of_non_mull_bool, "true", [True])
122-
_test_case(non_null_list_of_non_mull_bool, "123", Undefined)
123-
_test_case(non_null_list_of_non_mull_bool, "null", Undefined)
124-
_test_case(non_null_list_of_non_mull_bool, "[true, false]", [True, False])
125-
_test_case(non_null_list_of_non_mull_bool, "[true, 123]", Undefined)
126-
_test_case(non_null_list_of_non_mull_bool, "[true, null]", Undefined)
113+
assert _value_from("true", non_null_list_of_non_mull_bool) == [True]
114+
assert _value_from("123", non_null_list_of_non_mull_bool) is Undefined
115+
assert _value_from("null", non_null_list_of_non_mull_bool) is Undefined
116+
assert _value_from("[true, false]", non_null_list_of_non_mull_bool) == [
117+
True,
118+
False,
119+
]
120+
assert _value_from("[true, 123]", non_null_list_of_non_mull_bool) is Undefined
121+
assert _value_from("[true, null]", non_null_list_of_non_mull_bool) is Undefined
127122

128123
test_input_obj = GraphQLInputObjectType(
129124
"TestInput",
@@ -135,51 +130,48 @@ def coerces_non_null_lists_of_non_null_values():
135130
)
136131

137132
def coerces_input_objects_according_to_input_coercion_rules():
138-
_test_case(test_input_obj, "null", None)
139-
_test_case(test_input_obj, "123", Undefined)
140-
_test_case(test_input_obj, "[]", Undefined)
141-
_test_case(
142-
test_input_obj,
143-
"{ int: 123, requiredBool: false }",
144-
{"int": 123, "requiredBool": False},
145-
)
146-
_test_case(
147-
test_input_obj,
148-
"{ bool: true, requiredBool: false }",
149-
{"int": 42, "bool": True, "requiredBool": False},
133+
assert _value_from("null", test_input_obj) is None
134+
assert _value_from("[]", test_input_obj) is Undefined
135+
assert _value_from("123", test_input_obj) is Undefined
136+
assert _value_from("{ int: 123, requiredBool: false }", test_input_obj) == {
137+
"int": 123,
138+
"requiredBool": False,
139+
}
140+
assert _value_from("{ bool: true, requiredBool: false }", test_input_obj) == {
141+
"int": 42,
142+
"bool": True,
143+
"requiredBool": False,
144+
}
145+
assert (
146+
_value_from("{ int: true, requiredBool: true }", test_input_obj)
147+
is Undefined
150148
)
151-
_test_case(test_input_obj, "{ int: true, requiredBool: true }", Undefined)
152-
_test_case(test_input_obj, "{ requiredBool: null }", Undefined)
153-
_test_case(test_input_obj, "{ bool: true }", Undefined)
149+
assert _value_from("{ requiredBool: null }", test_input_obj) is Undefined
150+
assert _value_from("{ bool: true }", test_input_obj) is Undefined
154151

155152
def accepts_variable_values_assuming_already_coerced():
156-
_test_case_with_vars({}, GraphQLBoolean, "$var", Undefined)
157-
_test_case_with_vars({"var": True}, GraphQLBoolean, "$var", True)
158-
_test_case_with_vars({"var": None}, GraphQLBoolean, "$var", None)
153+
assert _value_from("$var", GraphQLBoolean, {}) is Undefined
154+
assert _value_from("$var", GraphQLBoolean, {"var": True}) is True
155+
assert _value_from("$var", GraphQLBoolean, {"var": None}) is None
159156

160157
def asserts_variables_are_provided_as_items_in_lists():
161-
_test_case_with_vars({}, list_of_bool, "[ $foo ]", [None])
162-
_test_case_with_vars({}, list_of_non_null_bool, "[ $foo ]", Undefined)
163-
_test_case_with_vars({"foo": True}, list_of_non_null_bool, "[ $foo ]", [True])
158+
assert _value_from("[ $foo ]", list_of_bool, {}) == [None]
159+
assert _value_from("[ $foo ]", list_of_non_null_bool, {}) is Undefined
160+
assert _value_from("[ $foo ]", list_of_non_null_bool, {"foo": True}) == [True]
164161
# Note: variables are expected to have already been coerced, so we
165162
# do not expect the singleton wrapping behavior for variables.
166-
_test_case_with_vars({"foo": True}, list_of_non_null_bool, "$foo", True)
167-
_test_case_with_vars({"foo": [True]}, list_of_non_null_bool, "$foo", [True])
163+
assert _value_from("$foo", list_of_non_null_bool, {"foo": True}) is True
164+
assert _value_from("$foo", list_of_non_null_bool, {"foo": [True]}) == [True]
168165

169166
def omits_input_object_fields_for_unprovided_variables():
170-
_test_case_with_vars(
171-
{},
172-
test_input_obj,
173-
"{ int: $foo, bool: $foo, requiredBool: true }",
174-
{"int": 42, "requiredBool": True},
175-
)
176-
_test_case_with_vars({}, test_input_obj, "{ requiredBool: $foo }", Undefined)
177-
_test_case_with_vars(
178-
{"foo": True},
179-
test_input_obj,
180-
"{ requiredBool: $foo }",
181-
{"int": 42, "requiredBool": True},
182-
)
167+
assert _value_from(
168+
"{ int: $foo, bool: $foo, requiredBool: true }", test_input_obj, {}
169+
) == {"int": 42, "requiredBool": True}
170+
assert _value_from("{ requiredBool: $foo }", test_input_obj, {}) is Undefined
171+
assert _value_from("{ requiredBool: $foo }", test_input_obj, {"foo": True}) == {
172+
"int": 42,
173+
"requiredBool": True,
174+
}
183175

184176
def transforms_names_using_out_name():
185177
# This is an extension of GraphQL.js.
@@ -192,9 +184,10 @@ def transforms_names_using_out_name():
192184
),
193185
},
194186
)
195-
_test_case(
196-
complex_input_obj, "{ realPart: 1 }", {"real_part": 1, "imag_part": 0}
197-
)
187+
assert _value_from("{ realPart: 1 }", complex_input_obj) == {
188+
"real_part": 1,
189+
"imag_part": 0,
190+
}
198191

199192
def transforms_values_with_out_type():
200193
# This is an extension of GraphQL.js.
@@ -206,4 +199,4 @@ def transforms_values_with_out_type():
206199
},
207200
out_type=lambda value: complex(value["real"], value["imag"]),
208201
)
209-
_test_case(complex_input_obj, "{ real: 1, imag: 2 }", 1 + 2j)
202+
assert _value_from("{ real: 1, imag: 2 }", complex_input_obj) == 1 + 2j

0 commit comments

Comments
 (0)