def complex_field_nested_within_a_hyperlink_complex_field_is_wrapped_with_the_hyperlink(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, self._SEPARATE_COMPLEX_FIELD, self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [ xml_text(' AUTHOR "John Doe"') ]), self._SEPARATE_COMPLEX_FIELD, _run_element_with_text("John Doe"), self._END_COMPLEX_FIELD, self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run(children=is_sequence( is_text("John Doe"), )), self._is_empty_hyperlinked_run, is_empty_run, )))
def target_frame_is_read(self): element = xml_element("w:hyperlink", { "w:anchor": "start", "w:tgtFrame": "_blank", }) assert_that( _read_and_get_document_xml_element(element), is_hyperlink(target_frame="_blank"), )
def empty_target_frame_is_ignored(self): element = xml_element("w:hyperlink", { "w:anchor": "start", "w:tgtFrame": "", }) assert_that( _read_and_get_document_xml_element(element), is_hyperlink(target_frame=None), )
def test_can_parse_diffdoc_block_without_options_and_content_followed_by_text(): content = _load_elements(dedent(""" .. diff-doc:: start example Text """)) assert_that(content, is_sequence( is_diffdoc_block( arguments=is_sequence("start", "example"), options={}, content="", ), is_text("\n"), is_text("Text"), ))
def test_cbow(): input = list(range(1, 10)) y, X = cbow(input) assert_that(y, is_sequence(3, 4, 5, 6, 7)) assert_that( X, is_sequence( is_sequence(1, 2, 4, 5), is_sequence(2, 3, 5, 6), is_sequence(3, 4, 6, 7), is_sequence(4, 5, 7, 8), is_sequence(5, 6, 8, 9), ))
def test_diffdoc_replace(self): element = parser._read_rst_element( rst.DiffdocBlock( arguments=("replace", "example"), options={ "render": "True", }, content="CONTENT", )) assert_that( element, is_replace( name="example", render=True, content="CONTENT", ))
def test_extract_text_extracts_text_strings(): text = io.StringIO( dedent("""\ [05/06/2018, 18:54:06] John Smith: Flights booked. Maybe we can meet up and cook all together? [17/08/2018, 23:52:10] John Smith: Oh my! At least it went a hip!!! 😬 [13/01/2019, 22:20:03] Glenn Quagmire: Happy Anniversary to you Jim. """)) text_lines = extract_text(text) assert_that( text_lines, contains_exactly( "Flights booked. Maybe we can meet up and cook all together?", "Oh my! At least it went a hip!!! 😬", "Happy Anniversary to you Jim."))
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 tbl_header_marks_table_row_as_header(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:trPr", {}, [ xml_element("w:tblHeader") ]), ]), xml_element("w:tr"), ]) table = _read_and_get_document_xml_element(element) assert_that(table, is_table( children=is_sequence( is_row(is_header=True), is_row(is_header=False), ), ))
def test_query_is_validated(): Root = g.ObjectType("Root", fields=( g.field("value", g.String), )) graph_definition = g.define_graph(resolvers=()) graph = graph_definition.create_graph({}) query = """ { x } """ error = pytest.raises(GraphQLError, lambda: graphql.execute(graph=graph, document_text=query, query_type=Root)) assert_that(str(error.value), equal_to(('Cannot query field "x" on type "Root".')))
def test_when_there_are_pending_lines_then_replace_raises_error(self): element = parser.Replace( name="example", content="x = 2", render=False, ) state = { "example": _create_code( language="python", content="x = 1", pending_lines=("x = 1", ), ), } error = pytest.raises(ValueError, lambda: _execute(state, element, line_number=42)) assert_that(str(error.value), equal_to("cannot replace on line number 42, pending lines:\nx = 1"))
def test_given_field_arg_has_default_when_field_arg_is_not_set_then_default_is_used(): Root = schema.ObjectType( "Root", fields=( schema.field("one", type=schema.Int, params=[ schema.param("arg0", type=schema.Int, default=None), schema.param("arg1", type=schema.Int, default=42), ]), ), ) field_query = Root.fields.one() assert_that(field_query.args, has_attrs( arg0=None, arg1=42, ))
def test_fields_with_same_key_for_different_types_are_kept_separate(self): Item = schema.InterfaceType("Item", fields=()) Song = schema.ObjectType("Song", interfaces=(Item, ), fields=( schema.field("title", type=schema.String), )) Book = schema.ObjectType("Book", interfaces=(Item, ), fields=( schema.field("title", type=schema.String), )) query = ( Item(schema.key("title", Song.fields.title())) + Item(schema.key("title", Book.fields.title())) ) assert_that(query, is_query(Item( schema.key("title", Song.fields.title()), schema.key("title", Book.fields.title()), )))
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_converting_from_diff_to_replace_generates_replace_block(self): start = parser.Start( name="example", language="python", render=True, content=dedent(""" x = 1 print(x) """), ) diff = parser.Diff( name="example", render=False, content=dedent(""" --- old +++ new @@ -1,2 +1,2 @@ -x = 1 +x = 2 print(x) """), ) output = compiler.convert_block( source=( (1, start), (2, diff), ), line_number=2, block_type="replace", ) assert_that(output, is_sequence( is_start(), is_replace( name="example", render=False, content=dedent(""" x = 2 print(x) """) ), ))
def test_constant_object_resolver(): Root = g.ObjectType( "Root", fields=[ g.field("one", type=g.Int), g.field("two", type=g.Int), ], ) resolve_root = g.constant_object_resolver(Root, dict(one=1, two=2)) resolvers = [resolve_root] query = Root(g.key("value", Root.fields.one()), ) result = g.create_graph(resolvers).resolve(query) assert_that(result, has_attrs(value=1))
def test_replace_with_render_false_renders_nothing(self): element = parser.Replace( name="example", content="x = 2\nprint(x)\n", render=False, ) state = { "example": _create_code( language="python", content="x = 1\nprint(x)\n", pending_lines=(), ), } new_state, new_element = _execute(state, element) assert_that(new_element, is_empty_element) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence("x = 2")))
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_render_renders_content(self): element = parser.Render( name="example", content="print(x)", ) state = { "example": _create_code( language="python", content="x = 1\nprint(x)", ), } new_state, new_element = _execute(state, element) assert_that(new_element, is_code_block( language="python", content="print(x)", ))
def test_field_names_are_converted_to_snake_case(): Root = g.ObjectType( "Root", (g.field("one_value", type=g.Int), ), ) graphql_query = """ query { oneValue } """ object_query = _document_text_to_graph_query(graphql_query, query_type=Root) assert_that(object_query, is_query(Root(g.key("oneValue", Root.fields.one_value()), ), ))
def test_fields_can_have_alias(): Root = g.ObjectType( "Root", (g.field("one", type=g.Int), ), ) graphql_query = """ query { value: one } """ object_query = _document_text_to_graph_query(graphql_query, query_type=Root) assert_that(object_query, is_query(Root(g.key("value", Root.fields.one()), ), ))
def test_converting_query_for_object_to_interface_keeps_fields_for_object(self): Item = schema.InterfaceType("Item", fields=( schema.field("title", type=schema.String), )) Song = schema.ObjectType("Song", interfaces=(Item, ), fields=( schema.field("length", type=schema.Int), schema.field("title", type=schema.String), )) query = Song( schema.key("title", Song.fields.title()), schema.key("length", Song.fields.length()), ) assert_that(query.for_type(Item), is_query(Item( schema.key("title", Song.fields.title()), schema.key("length", Song.fields.length()), )))
def test_resolver_is_dispatched_using_type_of_query(): @g.resolver("one") def resolve_one(graph, query): return 1 @g.resolver("two") def resolve_two(graph, query): return 2 resolvers = [resolve_one, resolve_two] class Query(object): type = "one" result = g.create_graph(resolvers).resolve(Query) assert_that(result, equal_to(1))
def test_when_merging_fragments_then_scalar_fields_can_overlap(): Root = g.ObjectType( "Root", fields=lambda: (g.field("user", type=User), ), ) User = g.ObjectType( "User", fields=( g.field("name", type=g.String), g.field("address", type=g.String), g.field("role", type=g.String), ), ) graphql_query = """ query { ... on Root { user { name address } } ... on Root { user { name role } } } """ object_query = _document_text_to_graph_query(graphql_query, query_type=Root) assert_that( object_query, is_query( Root( g.key( "user", Root.fields.user( g.key("name", User.fields.name()), g.key("address", User.fields.address()), g.key("role", User.fields.role()), )), ), ))
def test_root_object_resolver_can_resolve_fields_with_dependencies(self): Root = g.ObjectType("Root", fields=(g.field("value", type=g.Int), )) resolve_root = g.root_object_resolver(Root) value_key = object() @resolve_root.field(Root.fields.value) @g.dependencies(value=value_key) def root_resolve_value(graph, query, args, *, value): return value graph_definition = g.define_graph(resolvers=(resolve_root, )) graph = graph_definition.create_graph({value_key: 42}) query = Root(g.key("value", Root.fields.value()), ) assert_that(graph.resolve(query), has_attrs(value=42))
def test_parsing_rst_splits_file_into_text_and_diffdoc_blocks(): content = rst.loads(dedent(""" Text one .. diff-doc:: start example :language: python :render: True Example 1 Example 2 Text two Text three .. diff-doc:: replace example Example 3 Example 4 Text four """)) assert_that(content, is_sequence( is_tuple(1, is_text("Text one\n")), is_tuple(2, is_text("\n")), is_tuple(3, is_diffdoc_block( arguments=is_sequence("start", "example"), options={"language": "python", "render": "True"}, content="Example 1\n\nExample 2\n", )), is_tuple(10, is_text("\n")), is_tuple(11, is_text("Text two\n")), is_tuple(12, is_text("\n")), is_tuple(13, is_text("Text three\n")), is_tuple(14, is_text("\n")), is_tuple(15, is_diffdoc_block( arguments=is_sequence("replace", "example"), options={}, content="Example 3\n\nExample 4\n", )), is_tuple(20, is_text("\n")), is_tuple(21, is_text("Text four")), ))
def test_field_can_be_from_subtype(self): Item = schema.InterfaceType("Item", fields=()) Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), ), interfaces=(Item, )) query = schema.key("title", Book.fields.title()) assert_that(query.to_string(Item), equal_to(dedent(""" FieldQuery( key="title", field=Book.fields.title, type_query=scalar_query, args=(), ) """)))
def test_nullable_type_for_type_calls_for_type_on_element_query(self): Item = schema.InterfaceType("Item", fields=( )) Song = schema.ObjectType("Song", interfaces=(Item, ), fields=( schema.field("length", type=schema.Int), )) Book = schema.ObjectType("Book", interfaces=(Item, ), fields=( schema.field("length", type=schema.Int), )) query = schema.NullableType(Item)( schema.key("length", Song.fields.length()), schema.key("length", Book.fields.length()), ).for_type(schema.NullableType(Song)) assert_that(query, is_query(schema.NullableType(Song)( schema.key("length", Song.fields.length()), )))
def test_simple_query_is_converted_to_object_query(): Root = g.ObjectType( "Root", (g.field("one", type=g.Int), ), ) graphql_query = """ query { one } """ object_query = _document_text_to_graph_query(graphql_query, query_type=Root) assert_that(object_query, is_query(Root(g.key("one", Root.fields.one()), ), ))
def test_fragments_can_be_on_more_specific_type(): Animal = g.InterfaceType( "Animal", fields=( g.field("name", type=g.String), ), ) Cat = g.ObjectType( "Cat", fields=( g.field("name", type=g.String), g.field("whisker_count", type=g.Int), ), interfaces=(Animal, ), ) Root = g.ObjectType( "Root", ( g.field("animal", type=Animal), ), ) graphql_query = """ query { animal { name ... on Cat { whiskerCount } } } """ object_query = _document_text_to_graph_query(graphql_query, query_type=Root, types=(Cat, )) assert_that(object_query, is_query( Root( g.key("animal", Root.fields.animal( g.key("name", Animal.fields.name()), g.key("whiskerCount", Cat.fields.whisker_count()), )), ), ))
def runs_after_a_complex_field_for_hyperlinks_are_not_read_as_hyperlinks(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, self._SEPARATE_COMPLEX_FIELD, self._END_COMPLEX_FIELD, _run_element_with_text("this will not be a hyperlink"), ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, is_empty_run, is_run(children=is_sequence( is_text("this will not be a hyperlink"), )), )))
def test_when_query_is_invalid_then_result_is_invalid(): Root = g.ObjectType("Root", fields=(g.field("value", g.String), )) graph_definition = g.define_graph(resolvers=()) graph = graph_definition.create_graph({}) query = """ query { bad } """ result = graphql.execute(graph=graph, document_text=query, query_type=Root) assert_that( result, is_invalid(errors=contains_exactly( has_attrs(message="Cannot query field 'bad' on type 'Root'."), )))
def test_when_resolve_is_called_then_resolver_is_passed_the_graph(): @g.resolver("root") def resolve_root(graph, query): return graph.resolve(Query("leaf")) @g.resolver("leaf") def resolve_leaf(graph, query): return 42 class Query(object): def __init__(self, type): self.type = type resolvers = [resolve_root, resolve_leaf] result = g.create_graph(resolvers).resolve(Query("root")) assert_that(result, equal_to(42))
def field_without_separate_fld_char_is_ignored(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, self._SEPARATE_COMPLEX_FIELD, self._BEGIN_COMPLEX_FIELD, self._END_COMPLEX_FIELD, _run_element_with_text("this is a hyperlink"), self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run(children=is_sequence( is_text("this is a hyperlink"), )), is_empty_run, )))
def can_handle_split_instr_text_elements(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [ xml_text(" HYPE") ]), xml_element("w:instrText", {}, [ xml_text('RLINK "{0}"'.format(self._URI)), ]), self._SEPARATE_COMPLEX_FIELD, _run_element_with_text("this is a hyperlink"), self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run(children=is_sequence( is_text("this is a hyperlink"), )), is_empty_run, )))
def assert_that_raises_assertion_error_if_match_fails(): try: assert_that(1, equal_to(2)) assert False, "Expected AssertionError" except AssertionError as error: assert_equal("\nExpected:\n 2\nbut:\n was 1", str(error))
def assert_that_does_nothing_if_matcher_matches(): assert_that(1, equal_to(1))