def test_can_handle_recursive_types(self): obj_1 = GraphQLObjectType("Object", fields=lambda: {"self": GraphQLField(type=obj_1)}) obj_2 = GraphQLObjectType("Object", fields=lambda: {"self": GraphQLField(type=obj_2)}) assert is_subtype(obj_1, obj_2) obj_1 = GraphQLObjectType("Object", fields=lambda: {"self": GraphQLField(type=obj_1)}) obj_2 = GraphQLObjectType("Object", fields=lambda: {"self": GraphQLField(type=GraphQLNonNull(obj_2))}) assert not is_subtype(obj_1, obj_2)
def _assert_merge(self, types, expected_type): merged = _common_supertype(*types) assert_that(merged, expected_type) assert_that(_common_supertype(*list(reversed(types))), expected_type) for type_ in types: assert is_subtype(type_, merged)
def _assert_merge(self, types, expected_type): merged = greatest_common_subtype(types) assert_that(merged, expected_type) assert_that(greatest_common_subtype(list(reversed(types))), expected_type) for type_ in types: assert is_subtype(merged, type_)
def test_when_query_is_not_subtype_then_schema_is_not_subtype(self): assert not is_subtype( GraphQLSchema( query=GraphQLObjectType("Object", fields={ "id": GraphQLField(type=GraphQLInt), }), ), GraphQLSchema( query=GraphQLObjectType("Object", fields={ "id": GraphQLField(type=GraphQLInt), "name": GraphQLField(type=GraphQLString), }), ), )
def test_when_arg_is_missing_then_is_not_subtype(self): assert not is_subtype( GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={}, ) }), GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={"id": GraphQLArgument(type=GraphQLInt)}, ) }), )
def test_when_field_has_extra_non_null_args_then_is_not_subtype(self): assert not is_subtype( GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={"id": GraphQLArgument(type=GraphQLNonNull(GraphQLInt))}, ) }), GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={}, ) }), )
def test_when_field_has_arg_of_supertype_then_is_subtype(self): assert is_subtype( GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={"id": GraphQLArgument(type=GraphQLInt)}, ) }), GraphQLObjectType("Object", { "value": GraphQLField( type=GraphQLNonNull(GraphQLInt), args={"id": GraphQLArgument(type=GraphQLNonNull(GraphQLInt))}, ) }), )
def test_when_mutation_is_missing_then_is_not_subtype_of_schema_with_mutation(self): assert not is_subtype( GraphQLSchema( query=GraphQLObjectType("Object", fields={ "id": GraphQLField(type=GraphQLInt), }), ), GraphQLSchema( query=GraphQLObjectType("Object", fields={ "id": GraphQLField(type=GraphQLInt), }), mutation=GraphQLObjectType("Mutation", fields={ "id": GraphQLField(type=GraphQLInt), }), ), )
def test_list_of_type_is_not_subtype_of_list_of_subtype(self): assert not is_subtype(GraphQLList(GraphQLInt), GraphQLList(GraphQLNonNull(GraphQLInt)))
def test_list_of_type_is_subtype_of_list_of_same_type(self): assert is_subtype(GraphQLList(GraphQLInt), GraphQLList(GraphQLInt))
def test_non_null_type_is_not_subtype_of_different_null_type(self): assert not is_subtype(GraphQLNonNull(GraphQLInt), GraphQLString)
def test_non_null_type_is_subtype_of_same_null_type(self): assert is_subtype(GraphQLNonNull(GraphQLInt), GraphQLInt)
def test_scalar_types_are_not_subtypes_of_other_types(self, subtype, supertype): assert not is_subtype(subtype, supertype)
def test_when_object_type_has_same_name_and_has_different_field_name_then_it_is_not_a_subtype(self): assert not is_subtype( GraphQLObjectType("Object", fields={"id": GraphQLField(type=GraphQLString)}), GraphQLObjectType("Object", fields={"name": GraphQLField(type=GraphQLString)}), )
def test_when_object_type_field_is_of_different_type_then_it_is_not_a_subtype(self, object_type): assert not is_subtype( object_type("Object", {"id": GraphQLString}), object_type("Object", {"id": GraphQLInt}), )
def test_when_object_types_have_different_names_then_they_are_not_subtypes(self, object_type): assert not is_subtype( object_type("First", {"id": GraphQLInt}), object_type("Second", {"id": GraphQLInt}), )
def test_when_input_object_is_missing_non_null_field_then_it_is_not_a_subtype(self): assert not is_subtype( GraphQLInputObjectType("Object", {"id": GraphQLInputObjectField(type=GraphQLInt)}), GraphQLInputObjectType("Object", {"id": GraphQLInputObjectField(type=GraphQLInt), "name": GraphQLInputObjectField(type=GraphQLNonNull(GraphQLString))}), )
def test_when_input_object_type_has_same_name_and_superset_of_fields_then_it_is_a_subtype(self): assert is_subtype( GraphQLInputObjectType("Object", {"id": GraphQLInputObjectField(type=GraphQLInt), "name": GraphQLInputObjectField(type=GraphQLString)}), GraphQLInputObjectType("Object", {"id": GraphQLInputObjectField(type=GraphQLInt)}), )
def test_when_object_types_have_same_name_and_fields_then_they_are_subtypes(self, object_type): assert is_subtype( object_type("Object", {"id": GraphQLInt}), object_type("Object", {"id": GraphQLInt}), )
def test_scalar_types_are_subtypes_of_themselves(self, graphql_type): assert is_subtype(graphql_type, graphql_type)
def test_object_type_field_can_be_subtype(self, object_type): assert is_subtype( object_type("Object", {"id": GraphQLNonNull(GraphQLInt)}), object_type("Object", {"id": GraphQLInt}), )
def test_when_object_type_has_same_name_and_subset_of_fields_then_it_is_not_a_subtype(self): assert not is_subtype( GraphQLObjectType("Object", fields={"id": GraphQLField(type=GraphQLInt)}), GraphQLObjectType("Object", fields={"id": GraphQLField(type=GraphQLInt), "name": GraphQLField(type=GraphQLString)}), )