def test_when_resolution_raises_graph_error_then_result_is_invalid(): Root = g.ObjectType("Root", fields=( g.field("value", g.String), )) root_resolver = g.root_object_resolver(Root) @root_resolver.field(Root.fields.value) def root_resolve_value(graph, query, args): raise g.GraphError("BAD") graph_definition = g.define_graph(resolvers=(root_resolver, )) graph = graph_definition.create_graph({}) query = """ query { value } """ result = graphql.execute(graph=graph, document_text=query, query_type=Root) assert_that(result, is_invalid(errors=contains_exactly( all_of( is_instance(GraphQLError), has_str("BAD"), ), )))
def is_graphql_argument(type=None): if type is None: type = anything return all_of( is_instance(graphql.GraphQLArgument), has_attrs(type=type), )
def mismatches_when_unexpected_exception_is_raised(): def raise_key_error(): raise KeyError() matcher = raises(is_instance(ValueError)) result = matcher.match(raise_key_error) assert not result.is_match assert _normalise_newlines(result.explanation).startswith( "exception did not match: had type KeyError\n\nTraceback (most recent call last):\n", )
def test_build_id2word_reverses_word2id_mapping(): word2id = { "flights": 0, "booked": 1, "cook": 2, "all": 3, "together": 4, } id2word = build_id2word(word2id) assert_that( id2word, is_mapping({ 0: is_instance(str), 1: is_instance(str), 2: is_instance(str), 3: is_instance(str), 4: is_instance(str), }))
def is_graphql_interface_type(name, fields=None): if fields is None: fields = anything return all_of( is_instance(graphql.GraphQLInterfaceType), has_attrs( name=name, fields=fields, ), )
def is_graphql_field(type=None, args=None): if type is None: type = anything if args is None: args = anything return all_of( is_instance(graphql.GraphQLField), has_attrs(type=type, args=args), )
def is_graphql_input_object_type(name=None, fields=None): if name is None: name = anything if fields is None: fields = anything return all_of( is_instance(graphql.GraphQLInputObjectType), has_attrs( name=name, fields=fields, ), )
def is_query(query): if isinstance(query, schema.ScalarQuery): return is_instance(schema.ScalarQuery) elif isinstance(query, schema.EnumQuery): return is_instance(schema.EnumQuery) elif isinstance(query, schema.FieldQuery): return has_attrs( key=query.key, field=query.field, type_query=is_query(query.type_query), args=has_attrs(_values=is_mapping(query.args._values)), ) elif isinstance(query, schema.ListQuery): return has_attrs( type=query.type, element_query=is_query(query.element_query), ) elif isinstance(query, schema.NullableQuery): return has_attrs( type=query.type, element_query=is_query(query.element_query), ) elif isinstance(query, schema.ObjectQuery): return has_attrs( type=query.type, field_queries=is_sequence( * [is_query(field_query) for field_query in query.field_queries]), ) else: raise Exception("Unhandled query type: {}".format(type(query)))
def is_graphql_object_type(name=None, fields=None, interfaces=None): if name is None: name = anything if fields is None: fields = anything if interfaces is None: interfaces = anything return all_of( is_instance(graphql.GraphQLObjectType), has_attrs( name=name, fields=fields, interfaces=interfaces, ), )
def matches_when_value_is_instance_of_class(): assert_equal(matched(), is_instance(int).match(1))
def mismatches_when_no_exception_is_raised(): matcher = raises(is_instance(KeyError)) assert_equal(unmatched("did not raise exception"), matcher.match(lambda: None))
def explanation_of_mismatch_contains_actual_type(): assert_equal(unmatched("had type float"), is_instance(int).match(1.0))
def description_includes_expected_type(): assert_equal("is instance of int", is_instance(int).describe())
def is_graphql_enum_type(name, values): return all_of( is_instance(graphql.GraphQLEnumType), has_attrs(name=name, values=values), )
def mismatches_when_value_is_not_callable(): matcher = raises(is_instance(ValueError)) assert_equal(unmatched("was not callable"), matcher.match(42))
def is_output(**kwargs): return all_of( is_instance(parser.Output), has_attrs(**kwargs), )
def is_diff(**kwargs): return all_of( is_instance(parser.Diff), has_attrs(**kwargs), )
def is_replace(**kwargs): return all_of( is_instance(parser.Replace), has_attrs(**kwargs), )
def is_start(**kwargs): return all_of( is_instance(parser.Start), has_attrs(**kwargs), )
def description_includes_description_of_exception(): matcher = raises(is_instance(ValueError)) assert_equal("a callable raising: is instance of ValueError", matcher.describe())
def is_graphql_input_field(type): return all_of( is_instance(graphql.GraphQLInputField), has_attrs(type=type), )
def is_graphql_enum_value(value): return all_of( is_instance(graphql.GraphQLEnumValue), has_attrs(value=value), )
def _is_in_range(max_value): return all_of(is_instance(int), less_than_or_equal_to(max_value))
def is_diffdoc_block(arguments, options, content): return all_of( is_instance(rst.DiffdocBlock), has_attrs(arguments=arguments, options=options, content=content), )
def is_literal_block(**kwargs): return all_of( is_instance(rst.LiteralBlock), has_attrs(**kwargs), )
def matches_when_expected_exception_is_raised(): def raise_key_error(): raise KeyError() matcher = raises(is_instance(KeyError)) assert_equal(matched(), matcher.match(raise_key_error))
def is_render(**kwargs): return all_of( is_instance(parser.Render), has_attrs(**kwargs), )
def is_code_block(**kwargs): return all_of( is_instance(rst.CodeBlock), has_attrs(**kwargs), )
def is_graphql_list(element_matcher): return all_of( is_instance(graphql.GraphQLList), has_attrs(of_type=element_matcher), )
def is_text(text): return all_of( is_instance(rst.Text), has_attrs(text=text), )
def is_graphql_non_null(element_matcher): return all_of( is_instance(graphql.GraphQLNonNull), has_attrs(of_type=element_matcher), )