Skip to content

Commit c378b69

Browse files
committed
Cleanup value_from_ast(_untyped) tests
Replicates graphql/graphql-js@5579fd2
1 parent 567ef58 commit c378b69

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

tests/utilities/test_value_from_ast.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from math import isnan, nan
2+
from typing import Any, Dict, Optional
23

34
from graphql.language import parse_value, ValueNode
45
from graphql.pyutils import Undefined
@@ -9,6 +10,7 @@
910
GraphQLID,
1011
GraphQLInputField,
1112
GraphQLInputObjectType,
13+
GraphQLInputType,
1214
GraphQLInt,
1315
GraphQLList,
1416
GraphQLNonNull,
@@ -19,7 +21,11 @@
1921

2022

2123
def describe_value_from_ast():
22-
def _value_from(value_text, type_, variables=None):
24+
def _value_from(
25+
value_text: str,
26+
type_: GraphQLInputType,
27+
variables: Optional[Dict[str, Any]] = None,
28+
):
2329
ast = parse_value(value_text)
2430
return value_from_ast(ast, type_, variables)
2531

tests/utilities/test_value_from_ast_untyped.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from math import nan
2+
from typing import Any, Dict, Optional
23

34
from graphql.language import parse_value, FloatValueNode, IntValueNode
45
from graphql.pyutils import Undefined
56
from graphql.utilities import value_from_ast_untyped
67

78

89
def describe_value_from_ast_untyped():
9-
def _compare_value(value, expected):
10+
def _compare_value(value: Any, expected: Any):
1011
if expected is None:
1112
assert value is None
1213
elif expected is Undefined:
@@ -16,46 +17,50 @@ def _compare_value(value, expected):
1617
else:
1718
assert value == expected
1819

19-
def _test_case(value_text, expected):
20-
value_node = parse_value(value_text)
21-
_compare_value(value_from_ast_untyped(value_node), expected)
20+
def _expect_value_from(value_text: str, expected: Any):
21+
ast = parse_value(value_text)
22+
value = value_from_ast_untyped(ast)
23+
_compare_value(value, expected)
2224

23-
def _test_case_with_vars(value_text, variables, expected):
24-
value_node = parse_value(value_text)
25-
_compare_value(value_from_ast_untyped(value_node, variables), expected)
25+
def _expect_value_from_vars(
26+
value_text: str, variables: Optional[Dict[str, Any]], expected: Any
27+
):
28+
ast = parse_value(value_text)
29+
value = value_from_ast_untyped(ast, variables)
30+
_compare_value(value, expected)
2631

2732
def parses_simple_values():
28-
_test_case("null", None)
29-
_test_case("true", True)
30-
_test_case("false", False)
31-
_test_case("123", 123)
32-
_test_case("123.456", 123.456)
33-
_test_case('"abc123"', "abc123")
33+
_expect_value_from("null", None)
34+
_expect_value_from("true", True)
35+
_expect_value_from("false", False)
36+
_expect_value_from("123", 123)
37+
_expect_value_from("123.456", 123.456)
38+
_expect_value_from('"abc123"', "abc123")
3439

3540
def parses_lists_of_values():
36-
_test_case("[true, false]", [True, False])
37-
_test_case("[true, 123.45]", [True, 123.45])
38-
_test_case("[true, null]", [True, None])
39-
_test_case('[true, ["foo", 1.2]]', [True, ["foo", 1.2]])
41+
_expect_value_from("[true, false]", [True, False])
42+
_expect_value_from("[true, 123.45]", [True, 123.45])
43+
_expect_value_from("[true, null]", [True, None])
44+
_expect_value_from('[true, ["foo", 1.2]]', [True, ["foo", 1.2]])
4045

4146
def parses_input_objects():
42-
_test_case("{ int: 123, bool: false }", {"int": 123, "bool": False})
43-
_test_case('{ foo: [ { bar: "baz"} ] }', {"foo": [{"bar": "baz"}]})
47+
_expect_value_from("{ int: 123, bool: false }", {"int": 123, "bool": False})
48+
_expect_value_from('{ foo: [ { bar: "baz"} ] }', {"foo": [{"bar": "baz"}]})
4449

4550
def parses_enum_values_as_plain_strings():
46-
_test_case("TEST_ENUM_VALUE", "TEST_ENUM_VALUE")
47-
_test_case("[TEST_ENUM_VALUE]", ["TEST_ENUM_VALUE"])
51+
_expect_value_from("TEST_ENUM_VALUE", "TEST_ENUM_VALUE")
52+
_expect_value_from("[TEST_ENUM_VALUE]", ["TEST_ENUM_VALUE"])
4853

4954
def parses_variables():
50-
_test_case_with_vars("$testVariable", {"testVariable": "foo"}, "foo")
51-
_test_case_with_vars("[$testVariable]", {"testVariable": "foo"}, ["foo"])
52-
_test_case_with_vars(
55+
_expect_value_from_vars("$testVariable", {"testVariable": "foo"}, "foo")
56+
_expect_value_from_vars("[$testVariable]", {"testVariable": "foo"}, ["foo"])
57+
_expect_value_from_vars(
5358
"{a:[$testVariable]}", {"testVariable": "foo"}, {"a": ["foo"]}
5459
)
55-
_test_case_with_vars("$testVariable", {"testVariable": None}, None)
56-
_test_case_with_vars("$testVariable", {"testVariable": nan}, nan)
57-
_test_case_with_vars("$testVariable", {}, Undefined)
58-
_test_case_with_vars("$testVariable", None, Undefined)
60+
_expect_value_from_vars("$testVariable", {"testVariable": None}, None)
61+
_expect_value_from_vars("$testVariable", {"testVariable": nan}, nan)
62+
_expect_value_from_vars("$testVariable", {}, Undefined)
63+
_expect_value_from_vars("$testVariable", None, Undefined)
5964

6065
def parse_invalid_int_as_nan():
6166
assert value_from_ast_untyped(IntValueNode(value="invalid")) is nan

0 commit comments

Comments
 (0)