def test_cannot_convert_query_for_object_to_unrelated_object(self): Item = schema.ObjectType("Item", fields=( schema.field("title", type=schema.String), )) Song = schema.ObjectType("Song", fields=( schema.field("title", type=schema.String), )) query = Song( schema.key("title", Song.fields.title()), ) error = pytest.raises(TypeError, lambda: query.for_type(Item)) assert_that(str(error.value), equal_to("cannot coerce query for Song to query for Item"))
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_value_is_none_then_nullable_value_is_converted_to_none(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), )) NullableBook = schema.NullableType(Book) query = NullableBook(schema.key("book_title", Book.fields.title())) assert_that(query.to_json_value(None), equal_to(None))
def test_object_query_string_includes_type_and_field_queries(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), schema.field("publication_year", schema.Int), )) query = Book( schema.key("title", Book.fields.title()), schema.key("year", Book.fields.publication_year()), ) assert_that(str(query), equal_to(dedent(""" ObjectQuery( type=Book, fields=( FieldQuery( key="title", field=Book.fields.title, type_query=scalar_query, args=(), ), FieldQuery( key="year", field=Book.fields.publication_year, type_query=scalar_query, args=(), ), ), ) """)))
def test_adding_enum_query_to_other_query_raises_type_error(self): class Direction(enum.Enum): up = "up" down = "down" DirectionGraphType = schema.EnumType(Direction) pytest.raises(TypeError, lambda: DirectionGraphType() + schema.ObjectType("Obj", fields=()))
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_object_type_has_field_param_types_in_child_types(self): User = schema.ObjectType("User", fields=lambda: ( schema.field("name", type=schema.String, params=( schema.param("long", type=schema.Boolean), )), )) collected_types = schema.collect_types((User, )) assert_that(collected_types, includes(schema.Boolean))
def test_when_field_does_not_exist_on_object_type_then_error_is_raised(): book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), )) error = pytest.raises(ValueError, lambda: book.fields.author) assert_that(str(error.value), equal_to("Book has no field author"))
def test_object_type_has_interface_types_in_child_types(self): Person = schema.InterfaceType("Person", fields=lambda: ( schema.field("name", type=schema.String), )) User = schema.ObjectType("User", fields=lambda: ( schema.field("name", type=schema.String), ), interfaces=(Person, )) collected_types = schema.collect_types((User, )) assert_that(collected_types, includes(Person))
def test_objects_are_converted_to_dicts(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), )) query = Book(schema.key("book_title", Book.fields.title())) value = Object(dict(book_title="Orbiting the Giant Hairball")) assert_that(query.to_json_value(value), equal_to({ "book_title": "Orbiting the Giant Hairball", }))
def test_creating_field_query_specialises_type_of_type_query(self): Root = schema.ObjectType("Root", fields=lambda: ( schema.field("song", type=Song), )) Item = schema.InterfaceType("Item", fields=( schema.field("title", type=schema.String), )) Song = schema.ObjectType("Song", interfaces=(Item, ), fields=( schema.field("title", type=schema.String), )) field_query = Root.fields.song.query(key="song", args=(), type_query=Item( schema.key("title", Item.fields.title()), )) assert_that(field_query, is_query( Root.fields.song.query(key="song", args=(), type_query=Song( schema.key("title", Song.fields.title()), )), ))
def test_when_value_is_not_none_then_nullable_value_is_converted_using_element_query(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), )) NullableBook = schema.NullableType(Book) query = NullableBook(schema.key("book_title", Book.fields.title())) value = Object(dict(book_title="Orbiting the Giant Hairball")) assert_that(query.to_json_value(value), equal_to({ "book_title": "Orbiting the Giant Hairball", }))
def test_merged_object_query_has_fields_from_operand_queries(self): Song = schema.ObjectType("Song", fields=( schema.field("title", type=schema.String), schema.field("length", type=schema.Int), )) query = Song(schema.key("title", Song.fields.title())) + Song(schema.key("length", Song.fields.length())) assert_that(query, is_query(Song( schema.key("title", Song.fields.title()), schema.key("length", Song.fields.length()), )))
def test_arguments_are_coerced(): Root = schema.ObjectType( "Root", fields=( schema.field("one", type=schema.Int, params=[ schema.param("arg", type=schema.Int), ]), ), ) error = pytest.raises(GraphError, lambda: Root.fields.one(Root.fields.one.params.arg(None))) assert_that(str(error.value), equal_to("cannot coerce None to Int"))
def test_given_field_arg_has_no_default_when_field_arg_is_not_set_then_error_is_raised(): Root = schema.ObjectType( "Root", fields=( schema.field("one", type=schema.Int, params=[ schema.param("arg0", type=schema.Int), ]), ), ) error = pytest.raises(ValueError, lambda: Root.fields.one()) assert_that(str(error.value), equal_to("missing value for arg0"))
def test_lists_convert_elements_to_json_values(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), )) BookList = schema.ListType(Book) query = BookList(schema.key("book_title", Book.fields.title())) value = Object(dict(book_title="Orbiting the Giant Hairball")) assert_that(query.to_json_value([value]), equal_to([ { "book_title": "Orbiting the Giant Hairball", }, ]))
def test_object_type_for_type_filters_fields_to_those_for_type(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), )) Book = schema.ObjectType("Book", interfaces=(Item, ), fields=( schema.field("length", type=schema.Int), schema.field("title", type=schema.String), )) query = Item( schema.key("title", Item.fields.title()), schema.key("length", Song.fields.length()), schema.key("length", Book.fields.length()), ) assert_that(query.for_type(Song), is_query(Song( schema.key("title", Song.fields.title()), schema.key("length", Song.fields.length()), )))
def test_objects_convert_fields_to_json_values(self): Author = schema.ObjectType("Author", fields=( schema.field("name", schema.String), )) Book = schema.ObjectType("Book", fields=( schema.field("author", Author), )) query = Book( schema.key("author", Book.fields.author( schema.key("name", Author.fields.name()), )), ) value = Object(dict( author=Object(dict( name="Gordon A. Mackenzie", )), )) assert_that(query.to_json_value(value), equal_to({ "author": { "name": "Gordon A. Mackenzie", }, }))
def test_cannot_convert_query_for_interface_to_unrelated_object(self): Item = schema.InterfaceType("Item", fields=( schema.field("title", type=schema.String), )) Book = schema.ObjectType("Book", fields=( schema.field("title", type=schema.String), )) query = Item( schema.key("title", Item.fields.title()), ) error = pytest.raises(TypeError, lambda: query.for_type(Book)) assert_that(str(error.value), equal_to("cannot coerce query for Item to query for Book"))
def test_fields_are_recursively_merged(self): User = schema.ObjectType( "User", fields=lambda: ( schema.field("address", type=Address), ), ) Address = schema.ObjectType( "Address", fields=lambda: ( schema.field("first_line", type=schema.String), schema.field("city", type=schema.String), schema.field("postcode", type=schema.String), ), ) left_query = User( schema.key("address", User.fields.address( schema.key("first_line", Address.fields.first_line()), schema.key("city", Address.fields.city()), )), ) right_query = User( schema.key("address", User.fields.address( schema.key("city", Address.fields.city()), schema.key("postcode", Address.fields.postcode()), )), ) assert_that(left_query + right_query, is_query( User( schema.key("address", User.fields.address( schema.key("first_line", Address.fields.first_line()), schema.key("city", Address.fields.city()), schema.key("postcode", Address.fields.postcode()), )), ), ))
def test_list_query_merges_element_queries(self): Song = schema.ObjectType("Song", fields=( schema.field("title", type=schema.String), schema.field("length", type=schema.Int), )) query = ( schema.ListType(Song)(schema.key("title", Song.fields.title())) + schema.ListType(Song)(schema.key("length", Song.fields.length())) ) assert_that(query, is_query(schema.ListType(Song)( schema.key("title", Song.fields.title()), schema.key("length", Song.fields.length()), )))
def test_converting_query_for_interface_to_same_interface_keeps_fields_for_subtypes(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), )) Book = schema.ObjectType("Book", interfaces=(Item, ), fields=( schema.field("length", type=schema.Int), schema.field("title", type=schema.String), )) query = Item( schema.key("title", Item.fields.title()), schema.key("length", Song.fields.length()), schema.key("length", Book.fields.length()), ) assert_that(query.for_type(Item), is_query(Item( schema.key("title", Item.fields.title()), schema.key("length", Song.fields.length()), schema.key("length", Book.fields.length()), )))
def test_field_query_string_includes_key_and_field_and_type_query(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String), )) query = schema.key("title", Book.fields.title()) assert_that(query.to_string(Book), equal_to(dedent(""" FieldQuery( key="title", field=Book.fields.title, type_query=scalar_query, args=(), ) """)))
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_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_field_query_string_includes_args(self): Book = schema.ObjectType("Book", fields=( schema.field("title", schema.String, params=( schema.param("truncate", schema.Int), )), )) query = schema.key("title", Book.fields.title(Book.fields.title.params.truncate(42))) assert_that(query.to_string(Book), equal_to(dedent(""" FieldQuery( key="title", field=Book.fields.title, type_query=scalar_query, args=( Book.fields.title.params.truncate(42), ), ) """)))
def test_adding_object_query_to_other_query_raises_type_error(self): pytest.raises(TypeError, lambda: schema.ObjectType("Obj", fields=()) + schema.Boolean())
def test_interface_can_be_set_as_function_returning_iterable(self): Interface = schema.InterfaceType("Interface", fields=()) Obj = schema.ObjectType("Obj", fields=(), interfaces=lambda: (Interface, )) assert_that(Obj.interfaces, contains_exactly(Interface))
def test_object_type_has_field_types_in_child_types(self): User = schema.ObjectType("User", fields=lambda: ( schema.field("name", type=schema.String), )) collected_types = schema.collect_types((User, )) assert_that(collected_types, includes(schema.String))