def test_it_parses_variable_definition_with_default_and_directives(): assert_node_equal( parse("query ($foo: Int = 42 @bar @baz) { foo }", no_location=True), _ast.Document( definitions=[ _ast.OperationDefinition( "query", _ast.SelectionSet( selections=[_ast.Field(name=_ast.Name(value="foo"),)], ), variable_definitions=[ _ast.VariableDefinition( variable=_ast.Variable( name=_ast.Name(value="foo"), ), type=_ast.NamedType(name=_ast.Name(value="Int"),), default_value=_ast.IntValue(value="42"), directives=[ _ast.Directive(name=_ast.Name(value="bar")), _ast.Directive(name=_ast.Name(value="baz")), ], ) ], ) ], ), )
def enter_field(self, field): if field.name.value == "foo": return _ast.Field( name=_ast.Name("Foo"), arguments=[ _ast.Argument(_ast.Name("arg"), _ast.IntValue("42")) ], ) return field
def test_parse_value_it_parses_list_values(): assert_node_equal( parse_value('[123 "abc"]'), _ast.ListValue( loc=(0, 11), values=[ _ast.IntValue(loc=(1, 4), value="123"), _ast.StringValue(loc=(5, 10), value="abc"), ], ), )
@pytest.mark.parametrize( "value, input_type, expected", [ (True, Boolean, _ast.BooleanValue(value=True)), (False, Boolean, _ast.BooleanValue(value=False)), (None, Boolean, _ast.NullValue()), (1, Boolean, _ast.BooleanValue(value=True)), (0, Boolean, _ast.BooleanValue(value=False)), (0, NonNullType(Boolean), _ast.BooleanValue(value=False)), ( None, NonNullType(Boolean), ValueError('Value of type "Boolean!" cannot be null'), ), (-1, Int, _ast.IntValue(value="-1")), (123.0, Int, _ast.IntValue(value="123")), (1e4, Int, _ast.IntValue(value="10000")), ( 123.5, Int, ValueError("Int cannot represent non integer value: 123.5"), ), ( 1e40, Int, ValueError( "Int cannot represent non 32-bit signed integer: 1e+40"), ), (-1, Float, _ast.IntValue(value="-1")), (123.0, Float, _ast.IntValue(value="123")),
def test_provided_value(): arg = Argument("foo", Int) field = Field("test", Int, [arg]) node = _test_node(_ast.IntValue(value="42")) assert coerce_argument_values(field, node) == {"foo": 42}
def test_custom_python_name(): arg = Argument("foo", NonNullType(Int), python_name="other_name") field = Field("test", Int, [arg]) node = _test_node(_ast.IntValue(value="42")) assert coerce_argument_values(field, node) == {"other_name": 42}
def test_it_creates_ast(): assert_node_equal( parse( """{ node(id: 4) { id, name } } """ ), _ast.Document( loc=(0, 41), definitions=[ _ast.OperationDefinition( loc=(0, 40), operation="query", name=None, variable_definitions=[], directives=[], selection_set=_ast.SelectionSet( loc=(0, 40), selections=[ _ast.Field( loc=(4, 38), alias=None, name=_ast.Name(loc=(4, 8), value="node"), arguments=[ _ast.Argument( loc=(9, 14), name=_ast.Name(loc=(9, 11), value="id"), value=_ast.IntValue( loc=(13, 14), value="4" ), ) ], directives=[], selection_set=_ast.SelectionSet( loc=(16, 38), selections=[ _ast.Field( loc=(22, 24), alias=None, name=_ast.Name( loc=(22, 24), value="id" ), directives=[], arguments=[], selection_set=None, ), _ast.Field( loc=(30, 34), alias=None, name=_ast.Name( loc=(30, 34), value="name" ), directives=[], arguments=[], selection_set=None, ), ], ), ) ], ), ) ], ), )