def test_reject_non_output_type_as_object_fields(type_): schema = _single_type_schema(type_) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Expected output type for field "f" on "Query" but got "%s"' % type_) in str(exc_info.value)
def test_accept_extra_positional_with_default(self, schema: Schema) -> None: @schema.resolver("Object.field") def resolver(root, ctx, info, d=None, *, b, c, a="s"): pass validate_schema(schema)
def test_accept_partial_variable_keyword_args(self, schema: Schema) -> None: @schema.resolver("Object.field") def resolver(root, ctx, info, b, c, **kwargs): pass validate_schema(schema)
def test_accept_generic_type_default_resolver(self, schema: Schema) -> None: @schema.resolver("Object.*") def default_resolver(root, info, ctx, **args): pass validate_schema(schema)
def test_reject_enum_type_with_no_values(): schema = _single_type_schema(EnumType("EmptyEnum", [])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'EnumType "EmptyEnum" must at least define one value' in str( exc_info.value)
def test_reject_object_without_fields(): schema = _single_type_schema(IncompleteObject) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Type "IncompleteObject" must define at least one field' in str( exc_info.value)
def test_reject_bad_type_default_resolver(self, schema: Schema) -> None: @schema.resolver("Object.*") def default_resolver(root, info, ctx): pass with pytest.raises(SchemaError): validate_schema(schema)
def test_collects_multiple_errors(): iface = InterfaceType("IFace", [Field("f", ListType(String))]) bad_name_union = UnionType("#BadNameUnion", lambda: [object_type]) empty_union = UnionType("EmptyUnion", []) object_type = ObjectType( "SomeObject", [ Field("f", String), Field("__f", lambda: empty_union), Field("g", lambda: bad_name_union), ], interfaces=[iface], ) # type: ObjectType schema = _single_type_schema(object_type) with pytest.raises(SchemaValidationError) as exc_info: validate_schema(schema) assert set([str(e) for e in exc_info.value.errors]) == set([ 'Invalid name "__f".', 'Interface field "IFace.f" expects type "[String]" but "SomeObject.f" ' 'is type "String"', 'UnionType "EmptyUnion" must at least define one member', 'Invalid type name "#BadNameUnion"', ])
def test_reject_non_object_query_type(): schema = Schema(String) # type: ignore with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Query must be ObjectType but got "String"' in str(exc_info.value)
def test_reject_incorrectly_named_type(): schema = _single_type_schema( ObjectType("bad-name-with-dashes", [Field("field", String)])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Invalid type name "bad-name-with-dashes"' in str(exc_info.value)
def test_reject_bad_global_default_resolver(self, schema: Schema) -> None: def default_resolver(root, info, ctx): pass schema.default_resolver = default_resolver with pytest.raises(SchemaError): validate_schema(schema)
def test_accept_generic_global_default_resolver(self, schema: Schema) -> None: def default_resolver(root, info, ctx, **args): pass schema.default_resolver = default_resolver validate_schema(schema)
def test_reject_interface_with_no_field(): iface = InterfaceType("BadInterface", []) schema = _single_type_schema(iface) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Type "BadInterface" must define at least one field' in str( exc_info.value)
def test_reject_interface_fields_with_non_output_type(type_): iface = InterfaceType("BadInterface", [Field("f", type_)]) schema = _single_type_schema(iface) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Expected output type for field "f" on "BadInterface" ' 'but got "%s"' % type_) in str(exc_info.value)
def test_accept_callable_object(self, schema: Schema) -> None: class Resolver: def __call__(self, root, ctx, info, b, c, a="s"): pass schema.register_resolver("Object", "field", Resolver()) validate_schema(schema)
def test_reject_object_with_incorrectly_named_fields(): schema = Schema( ObjectType("Query", [Field("bad-name-with-dashes", String)])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Invalid name "bad-name-with-dashes"' in str(exc_info.value)
def test_reject_object_with_null_interface_non_null_field(): iface = InterfaceType("IFace", [Field("f", NonNullType(String))]) schema = _single_type_schema( ObjectType("SomeObject", [Field("f", String)], interfaces=[iface])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Interface field "IFace.f" expects type "String!" but ' '"SomeObject.f" is type "String"' in str(exc_info.value))
def test_reject_union_type_with_no_member(): EmptyUnion = UnionType("EmptyUnion", []) schema = _single_type_schema(EmptyUnion) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'UnionType "EmptyUnion" must at least define one member' in str( exc_info.value)
def test_reject_union_type_with_duplicate_members(): TypeA = ObjectType("TypeA", [Field("f", String)]) BadUnion = UnionType("BadUnion", [TypeA, TypeA]) schema = _single_type_schema(BadUnion) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'UnionType "BadUnion" can only include type "TypeA" once' in str( exc_info.value)
def test_reject_object_with_incorrectly_typed_interface_field(): schema = _single_type_schema( ObjectType("SomeObject", [Field("f", Int)], interfaces=[SomeInterface])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Interface field "SomeInterface.f" expects type "String" ' 'but "SomeObject.f" is type "Int"' in str(exc_info.value))
def test_reject_object_missing_interface_field(): schema = _single_type_schema( ObjectType("SomeObject", [Field("_f", String)], interfaces=[SomeInterface])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Interface field "SomeInterface.f" is not implemented ' 'by type "SomeObject"' in str(exc_info.value))
def test_reject_union_type_with_non_object_members(): TypeA = ObjectType("TypeA", [Field("f", String)]) BadUnion = UnionType("BadUnion", [TypeA, SomeScalar]) # type: ignore schema = _single_type_schema(BadUnion) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('UnionType "BadUnion" expects object types but got "SomeScalar"' in str(exc_info.value))
def test_reject_argument_with_non_input_type(type_): schema = _single_type_schema( ObjectType("BadObject", [Field("f", String, [Argument("badArg", type_)])])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Expected input type for argument "badArg" on "BadObject.f" ' 'but got "%s"' % type_) in str(exc_info.value)
def test_reject_input_object_with_no_field(): arg = Argument("badArg", InputObjectType("BadInput", [])) schema = _single_type_schema( ObjectType("BadObject", [Field("f", String, [arg])])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Type "BadInput" must define at least one field' in str( exc_info.value)
def test_reject_object_fields_with_missing_interface_argument(): iface = InterfaceType("IFace", [Field("f", String, [Argument("arg", String)])]) obj = ObjectType("Obj", [Field("f", String)], interfaces=[iface]) schema = _single_type_schema(obj) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Interface field argument "IFace.f.arg" is not provided by "Obj.f"' in str(exc_info.value))
def test_reject_non_object_subscription_type(): schema = Schema( ObjectType("Query", [Field("test", String)]), subscription_type=String, # type: ignore ) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Subscription must be ObjectType but got "String"' in str( exc_info.value)
def test_reject_field_args_with_incorrect_names(): schema = _single_type_schema( ObjectType( "SomeObject", [Field("field", String, [Argument("bad-arg", String)])], )) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Invalid name "bad-arg"' in str(exc_info.value)
def test_reject_input_type_with_no_fields(): EmptyInput = InputObjectType("EmptyInput", []) schema = _single_type_schema( ObjectType("Object", [Field("field", String, [Argument("arg", EmptyInput)])])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert 'Type "EmptyInput" must define at least one field' in str( exc_info.value)
def test_reject_input_type_with_incorectly_typed_fields(): BadInput = InputObjectType("BadInput", [InputField("f", SomeObject)]) schema = _single_type_schema( ObjectType("Object", [Field("field", String, [Argument("arg", BadInput)])])) with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert ('Expected input type for field "f" on "BadInput" ' 'but got "SomeObject"' in str(exc_info.value))
def test_reject_extra_positional(self, schema: Schema) -> None: @schema.resolver("Object.field") def resolver(root, ctx, info, bar, *, b, c, a="s", d=None): pass with pytest.raises(SchemaError) as exc_info: validate_schema(schema) assert set([str(e) for e in exc_info.value.errors]) == set([ 'Required resolver parameter "bar" on "Object.field" does not ' "match any known argument or expected positional parameter", ])