1
1
from math import nan
2
+ from typing import Any , Dict , Optional
2
3
3
4
from graphql .language import parse_value , FloatValueNode , IntValueNode
4
5
from graphql .pyutils import Undefined
5
6
from graphql .utilities import value_from_ast_untyped
6
7
7
8
8
9
def describe_value_from_ast_untyped ():
9
- def _compare_value (value , expected ):
10
+ def _compare_value (value : Any , expected : Any ):
10
11
if expected is None :
11
12
assert value is None
12
13
elif expected is Undefined :
@@ -16,46 +17,50 @@ def _compare_value(value, expected):
16
17
else :
17
18
assert value == expected
18
19
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 )
22
24
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 )
26
31
27
32
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" )
34
39
35
40
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 ]])
40
45
41
46
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" }]})
44
49
45
50
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" ])
48
53
49
54
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 (
53
58
"{a:[$testVariable]}" , {"testVariable" : "foo" }, {"a" : ["foo" ]}
54
59
)
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 )
59
64
60
65
def parse_invalid_int_as_nan ():
61
66
assert value_from_ast_untyped (IntValueNode (value = "invalid" )) is nan
0 commit comments