def test_nullable_query_merges_element_queries(self): Song = schema.ObjectType("Song", fields=( schema.field("title", type=schema.String), schema.field("length", type=schema.Int), )) query = ( schema.NullableType(Song)(schema.key("title", Song.fields.title())) + schema.NullableType(Song)(schema.key("length", Song.fields.length())) ) assert_that(query, is_query(schema.NullableType(Song)( schema.key("title", Song.fields.title()), schema.key("length", Song.fields.length()), )))
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_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_nullable_query_string_includes_element_query(self): query = schema.NullableType(schema.Int)() assert_that(str(query), equal_to(dedent(""" NullableQuery( type=Nullable(Int), element_query=ScalarQuery(type=Int), ) """)))
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_nullable_type_has_element_type_as_child_type(self): collected_types = schema.collect_types((schema.NullableType(schema.String), )) assert_that(collected_types, contains_exactly(schema.NullableType(schema.String), schema.String))
def test_cannot_change_type_of_nullable_query_to_non_nullable_query(self): query = schema.NullableType(schema.Boolean)() error = pytest.raises(TypeError, lambda: query.for_type(schema.Boolean)) assert_that(str(error.value), equal_to("cannot coerce query for Nullable(Boolean) to query for Boolean"))
def test_adding_nullable_query_to_nullable_query_of_different_element_type_raises_type_error(self): error = pytest.raises(TypeError, lambda: schema.NullableType(schema.Boolean)() + schema.NullableType(schema.Int)()) assert_that(str(error.value), equal_to("cannot add queries for nullables with different element types: Boolean and Int"))
def test_adding_nullable_query_to_non_null_query_raises_type_error(self): pytest.raises(TypeError, lambda: schema.NullableType(schema.Boolean)() + schema.Boolean())
def test_nullable_type_coerces_null_to_null(self): self._assert_coercion(schema.NullableType(schema.Float), None, None)
def test_nullable_type_coerces_non_null_value_to_element_type(self): self._assert_coercion(schema.NullableType(schema.Float), 1, 1.0)
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)) @pytest.mark.parametrize("graph_type, element_type", ( (schema.ListType(schema.Boolean), schema.Boolean), (schema.NullableType(schema.Boolean), schema.Boolean), (schema.Boolean, schema.Boolean), (schema.ListType(schema.ListType(schema.Boolean)), schema.Boolean), (schema.NullableType(schema.NullableType(schema.Boolean)), schema.Boolean), (schema.ListType(schema.NullableType(schema.Boolean)), schema.Boolean), )) def test_to_element_type(graph_type, element_type): assert_that(schema.to_element_type(graph_type), equal_to(element_type)) @pytest.mark.parametrize("graph_type, element_type, expected", ( (schema.ListType(schema.Boolean), schema.String, schema.ListType(schema.String)), (schema.NullableType(schema.Boolean), schema.String, schema.NullableType(schema.String)), (schema.Boolean, schema.String, schema.String), ( schema.ListType(schema.ListType(schema.Boolean)),