def test_cannot_convert_query_for_interface_to_unrelated_interface(self): Item = schema.InterfaceType("Item", fields=( schema.field("title", type=schema.String), )) Song = schema.InterfaceType("Song", fields=( schema.field("title", type=schema.String), )) query = Item( schema.key("title", Item.fields.title()), ) error = pytest.raises(TypeError, lambda: query.for_type(Song)) assert_that(str(error.value), equal_to("cannot coerce query for Item to query for Song"))
def test_interface_type_has_field_param_types_in_child_types(self): User = schema.InterfaceType("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_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_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_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_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_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_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_interface_type_has_field_types_in_child_types(self): User = schema.InterfaceType("User", fields=lambda: ( schema.field("name", type=schema.String), )) collected_types = schema.collect_types((User, )) assert_that(collected_types, includes(schema.String))
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))