Ejemplo n.º 1
0
def test_when_field_value_in_input_object_in_input_object_is_not_set_then_default_is_used():
    Input = schema.InputObjectType(
        "Input",
        fields=(
            schema.input_field("field0", type=schema.Int, default=42),
        ),
    )
    OuterInput = schema.InputObjectType(
        "OuterInput",
        fields=(
            schema.input_field("value", type=Input),
        ),
    )

    Root = g.ObjectType(
        "Root",
        fields=(
            g.field("one", type=g.Int, params=[
                g.param("arg", type=OuterInput),
            ]),
        ),
    )

    graphql_query = """
        query {
            one(arg: {value: {}})
        }
    """

    object_query = _document_text_to_graph_query(graphql_query, query_type=Root)
    assert_that(object_query.field_queries[0].args.arg, has_attrs(
        value=has_attrs(
            field0=42,
        ),
    ))
Ejemplo n.º 2
0
    def test_cannot_coerce_instances_of_input_object_type_to_other_input_object_type(self):
        PersonInput = schema.InputObjectType("PersonInput", fields=lambda: (
            schema.input_field("name", type=schema.String),
        ))

        UserInput = schema.InputObjectType("UserInput", fields=lambda: (
            schema.input_field("name", type=schema.String),
        ))

        self._assert_coercion_failure(UserInput, PersonInput(name="Bob"), "cannot coerce PersonInput(name='Bob') to UserInput")
Ejemplo n.º 3
0
def test_when_field_value_in_input_object_in_list_is_not_set_then_default_is_used(
):
    Input = schema.InputObjectType(
        "Input",
        fields=(schema.input_field("field0", type=schema.Int, default=42), ),
    )

    Root = g.ObjectType(
        "Root",
        fields=(g.field("one",
                        type=g.Int,
                        params=[
                            g.param("arg", type=g.ListType(Input)),
                        ]), ),
    )

    graphql_query = """
        query ($value: Int!) {
            one(arg: [{}])
        }
    """

    object_query = _document_text_to_graph_query(graphql_query,
                                                 query_type=Root)
    assert_that(object_query.fields[0].args.arg,
                is_sequence(has_attrs(field0=42, ), ))
Ejemplo n.º 4
0
def test_given_input_field_has_no_default_when_input_field_is_not_set_then_error_is_raised():
    Input = schema.InputObjectType(
        "Input",
        fields=(
            schema.input_field("field0", type=schema.Int),
        ),
    )
    
    error = pytest.raises(ValueError, lambda: Input())
    assert_that(str(error.value), equal_to("missing value for field0"))
Ejemplo n.º 5
0
def test_when_passing_unknown_field_values_into_input_object_then_error_is_raised():
    Input = schema.InputObjectType(
        "Input",
        fields=(
            schema.input_field("field0", type=schema.Int),
        ),
    )

    error = pytest.raises(GraphError, lambda: Input(field0=0, field1=1))
    assert_that(str(error.value), equal_to("Input has no field field1"))
Ejemplo n.º 6
0
def test_given_input_field_has_default_when_input_field_is_not_set_then_default_is_used():
    Input = schema.InputObjectType(
        "Input",
        fields=(
            schema.input_field("field0", type=schema.Int, default=None),
            schema.input_field("field1", type=schema.Int, default=42),
        ),
    )
    
    input_value = Input()
    assert_that(input_value, has_attrs(
        field0=None,
        field1=42,
    ))
Ejemplo n.º 7
0
 def test_input_object_type_has_field_types_in_child_types(self):
     UserInput = schema.InputObjectType("UserInput", fields=lambda: (
         schema.field("name", type=schema.String),
     ))
     collected_types = schema.collect_types((UserInput, ))
     assert_that(collected_types, includes(schema.String))
Ejemplo n.º 8
0
    def test_cannot_coerce_none_to_input_object_type(self):
        UserInput = schema.InputObjectType("UserInput", fields=lambda: (
            schema.input_field("name", type=schema.String),
        ))

        self._assert_coercion_failure(UserInput, None, "cannot coerce None to UserInput")
Ejemplo n.º 9
0
    def test_input_object_type_coerces_instances_to_same_value(self):
        UserInput = schema.InputObjectType("UserInput", fields=lambda: (
            schema.input_field("name", type=schema.String),
        ))

        self._assert_coercion(UserInput, UserInput(name="Bob"), UserInput(name="Bob"))