def test_when_there_are_fewer_molecules_than_requested_then_all_molecules_are_fetched( builder, graphql): with builder: builder.add_molecule(chembl_id="CHEMBL42") query = """ query { moleculesConnection(first: 2) { edges { node { chemblId } } pageInfo { hasNextPage } } } """ response = graphql(query) assert_that( response["moleculesConnection"], is_mapping({ "edges": contains_exactly(is_mapping({"node": { "chemblId": "CHEMBL42" }}), ), "pageInfo": is_mapping({"hasNextPage": False}), }))
def test_when_there_are_no_molecules_then_connection_is_empty(graphql): query = """ query { moleculesConnection(first: 2) { edges { node { chemblId } } pageInfo { endCursor hasNextPage } } } """ response = graphql(query) assert_that( response["moleculesConnection"], is_mapping({ "edges": contains_exactly(), "pageInfo": is_mapping({ "endCursor": None, "hasNextPage": False }), }))
def test_edge_cursor_can_be_used_as_after_argument_to_get_next_page( builder, graphql): with builder: builder.add_molecule(chembl_id="CHEMBL42") builder.add_molecule(chembl_id="CHEMBL43") builder.add_molecule(chembl_id="CHEMBL44") query = """ query ($after: String) { moleculesConnection(first: 2, after: $after) { edges { cursor node { chemblId } } } } """ response = graphql(query, variables={"after": None}) assert_that( response["moleculesConnection"], is_mapping({ "edges": contains_exactly( is_mapping({ "cursor": anything, "node": { "chemblId": "CHEMBL42" } }), is_mapping({ "cursor": anything, "node": { "chemblId": "CHEMBL43" } }), ), })) response = graphql( query, variables={ "after": response["moleculesConnection"]["edges"][-1]["cursor"] }) assert_that( response["moleculesConnection"], is_mapping({ "edges": contains_exactly( is_mapping({ "cursor": anything, "node": { "chemblId": "CHEMBL44" } }), ), }))
def test_can_get_synonyms_of_molecule(builder, graphql): with builder: molecule_1 = builder.add_molecule(chembl_id="CHEMBL1") molecule_2 = builder.add_molecule(chembl_id="CHEMBL2") builder.add_molecule(chembl_id="CHEMBL3") builder.add_molecule_synonym( molecule=molecule_1, name="<synonym 1>", ) builder.add_molecule_synonym( molecule=molecule_2, name="<synonym 2a>", ) builder.add_molecule_synonym( molecule=molecule_2, name="<synonym 2b>", ) query = """ query { molecules { chemblId synonyms { name } } } """ response = graphql(query) assert_that( response["molecules"], contains_exactly( is_mapping({ "chemblId": "CHEMBL1", "synonyms": contains_exactly({"name": "<synonym 1>"}, ), }), is_mapping({ "chemblId": "CHEMBL2", "synonyms": contains_exactly( {"name": "<synonym 2a>"}, {"name": "<synonym 2b>"}, ), }), is_mapping({ "chemblId": "CHEMBL3", "synonyms": contains_exactly(), }), ))
def test_when_param_has_default_then_param_is_converted_to_nullable_graphql_arg(): graph_type = g.ObjectType("Obj", fields=( g.field("value", type=g.String, params=( g.param("arg", g.Int, default=42), )), )) assert_that(to_graphql_type(graph_type), is_graphql_non_null( is_graphql_object_type( fields=is_mapping({ "value": is_graphql_field(args=is_mapping({ "arg": is_graphql_argument(type=is_graphql_int), })), }), ), ))
def test_param_names_are_converted_from_snake_case_to_camel_case(): graph_type = g.ObjectType("Obj", fields=( g.field("value", type=g.String, params=( g.param("arg_zero", g.Int), )), )) assert_that(to_graphql_type(graph_type), is_graphql_non_null( is_graphql_object_type( fields=is_mapping({ "value": is_graphql_field(args=is_mapping({ "argZero": is_graphql_argument(), })), }), ), ))
def test_diff_updates_code_using_content_as_patch(self): element = parser.Diff( name="example", content=dedent(""" --- old +++ new @@ -1,2 +1,2 @@ -x = 1 +x = 2 print(x) """), render=False, ) state = { "example": _create_code( language="python", content="x = 1\nprint(x)\n", ), } new_state, new_element = _execute(state, element) assert_that(new_state, is_mapping({ "example": is_code( language="python", content="x = 2\nprint(x)\n", ), }))
def does_not_match_when_value_does_not_match(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal(unmatched("value for key 'b' mismatched:\n * was 3"), matcher.match({ "a": 1, "b": 3 }))
def does_not_match_when_there_are_extra_keys(): matcher = is_mapping({"a": equal_to(1)}) assert_equal(unmatched("had extra keys:\n * 'b'\n * 'c'"), matcher.match({ "a": 1, "b": 1, "c": 1 }))
def test_object_type_field_names_are_converted_from_snake_case_to_camel_case(): graph_type = g.ObjectType("Obj", fields=( g.field("field_name", type=g.String), )) assert_that(to_graphql_type(graph_type), is_graphql_non_null( is_graphql_object_type( fields=is_mapping({ "fieldName": anything, }), ), ))
def test_object_type_is_converted_to_non_null_graphql_object_type(): graph_type = g.ObjectType("Obj", fields=( g.field("value", type=g.String), )) assert_that(to_graphql_type(graph_type), is_graphql_non_null( is_graphql_object_type( name="Obj", fields=is_mapping({ "value": is_graphql_field(type=is_graphql_non_null(is_graphql_string)), }), ), ))
def test_when_input_field_has_default_then_input_field_type_is_nullable(): graph_type = g.InputObjectType("Obj", fields=( g.input_field("value", type=g.String, default=""), )) assert_that(to_graphql_input_type(graph_type), is_graphql_non_null( is_graphql_input_object_type( name="Obj", fields=is_mapping({ "value": is_graphql_input_field(type=is_graphql_string), }), ), ))
def test_recursive_object_type_is_converted_to_non_null_graphql_object_type(): graph_type = g.ObjectType("Obj", fields=lambda: ( g.field("self", type=graph_type), )) assert_that(to_graphql_type(graph_type), is_graphql_non_null( is_graphql_object_type( name="Obj", fields=is_mapping({ "self": is_graphql_field(type=is_graphql_non_null(is_graphql_object_type(name="Obj"))), }), ), ))
def test_start_initialises_state_for_name(self): # TODO: error if name already taken element = parser.Start( name="example", language="python", content="x = 1", render=False, ) state = {} new_state, new_element = _execute(state, element) assert_that(new_state, is_mapping({ "example": is_code( language="python", content="x = 1", ), }))
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 test_replace_replaces_content_for_code(self): element = parser.Replace( name="example", content="x = 2", render=False, ) state = { "example": _create_code( language="python", content="x = 1", ), } new_state, new_element = _execute(state, element) assert_that(new_state, is_mapping({ "example": is_code( language="python", content="x = 2", ), }))
def test_enum_is_converted_to_non_null_enum_type(): class Season(enum.Enum): winter = "WINTER" spring = "SPRING" summer = "SUMMER" autumn = "AUTUMN" SeasonGraphType = g.EnumType(Season) graphql_type = to_graphql_type(SeasonGraphType) assert_that(graphql_type, is_graphql_non_null(is_graphql_enum_type( name="Season", values=is_mapping({ "WINTER": is_graphql_enum_value(value="WINTER"), "SPRING": is_graphql_enum_value(value="SPRING"), "SUMMER": is_graphql_enum_value(value="SUMMER"), "AUTUMN": is_graphql_enum_value(value="AUTUMN"), }), )))
def test_build_word2id_creates_word_id_mapping(): words = [ "flights", "booked", "booked", "cook", "all", "together", ] word2id = build_word2id(words) assert_that( word2id, is_mapping({ "flights": _is_in_range(4), "booked": _is_in_range(4), "cook": _is_in_range(4), "all": _is_in_range(4), "together": _is_in_range(4), }))
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 matches_when_keys_and_values_match(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal(matched(), matcher.match({"a": 1, "b": 2}))
def description_describes_keys_and_value_matchers(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal("mapping with items:\n * 'a': 1\n * 'b': 2", matcher.describe())
def does_not_match_when_keys_are_missing(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal(unmatched("was missing key: 'b'"), matcher.match({"a": 1}))
def values_are_coerced_to_matchers(): matcher = is_mapping({"a": 1, "b": 2}) assert_equal(matched(), matcher.match({"a": 1, "b": 2}))
def matches_when_keys_and_values_match(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal(matched(), matcher.match({"a": 1, "b": 2}))
def description_describes_keys_and_value_matchers(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal("mapping with items:\n * 'a': 1\n * 'b': 2", matcher.describe())
def does_not_match_when_there_are_extra_keys(): matcher = is_mapping({"a": equal_to(1)}) assert_equal(unmatched("had extra keys:\n * 'b'\n * 'c'"), matcher.match({"a": 1, "b": 1, "c": 1}))
def does_not_match_when_keys_are_missing(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal(unmatched("was missing key: 'b'"), matcher.match({"a": 1}))
def does_not_match_when_value_does_not_match(): matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)}) assert_equal(unmatched("value for key 'b' mismatched:\n * was 3"), matcher.match({"a": 1, "b": 3}))
def values_are_coerced_to_matchers(): matcher = is_mapping({"a": 1, "b": 2}) assert_equal(matched(), matcher.match({"a": 1, "b": 2}))