2015-09-24 12:11:50 +03:00
|
|
|
from py.test import raises
|
|
|
|
from collections import namedtuple
|
|
|
|
from pytest import raises
|
|
|
|
from graphene.core.fields import (
|
|
|
|
Field,
|
|
|
|
StringField,
|
|
|
|
)
|
|
|
|
|
|
|
|
from graphene.core.options import Options
|
|
|
|
|
|
|
|
from graphql.core.type import (
|
|
|
|
GraphQLField,
|
|
|
|
GraphQLNonNull,
|
|
|
|
GraphQLInt,
|
|
|
|
GraphQLString,
|
|
|
|
GraphQLBoolean,
|
|
|
|
GraphQLID,
|
|
|
|
)
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-24 12:11:50 +03:00
|
|
|
class ObjectType(object):
|
|
|
|
_meta = Options()
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-24 12:11:50 +03:00
|
|
|
def resolve(self, *args, **kwargs):
|
|
|
|
return None
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-24 12:11:50 +03:00
|
|
|
def can_resolve(self, *args):
|
|
|
|
return True
|
|
|
|
|
|
|
|
ot = ObjectType()
|
|
|
|
|
|
|
|
ObjectType._meta.contribute_to_class(ObjectType, '_meta')
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
class Schema(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
schema = Schema()
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-24 12:11:50 +03:00
|
|
|
def test_field_no_contributed_raises_error():
|
|
|
|
f = Field(GraphQLString)
|
|
|
|
with raises(Exception) as excinfo:
|
2015-10-01 11:54:52 +03:00
|
|
|
f.internal_field(schema)
|
2015-09-24 12:11:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_type():
|
|
|
|
f = Field(GraphQLString)
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
2015-10-01 11:54:52 +03:00
|
|
|
assert isinstance(f.internal_field(schema), GraphQLField)
|
|
|
|
assert f.internal_type(schema) == GraphQLString
|
2015-09-24 12:11:50 +03:00
|
|
|
|
|
|
|
|
2015-10-03 08:56:37 +03:00
|
|
|
def test_field_name_automatic_camelcase():
|
|
|
|
f = Field(GraphQLString)
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
assert f.name == 'fieldName'
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_name_use_name_if_exists():
|
|
|
|
f = Field(GraphQLString, name='my_custom_name')
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
assert f.name == 'my_custom_name'
|
|
|
|
|
|
|
|
|
2015-09-24 12:11:50 +03:00
|
|
|
def test_stringfield_type():
|
|
|
|
f = StringField()
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
2015-10-01 11:54:52 +03:00
|
|
|
assert f.internal_type(schema) == GraphQLString
|
2015-09-24 12:11:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_stringfield_type_null():
|
|
|
|
f = StringField(null=False)
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
2015-10-01 11:54:52 +03:00
|
|
|
assert isinstance(f.internal_field(schema), GraphQLField)
|
|
|
|
assert isinstance(f.internal_type(schema), GraphQLNonNull)
|
2015-09-24 12:11:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_resolve():
|
2015-10-03 08:17:51 +03:00
|
|
|
f = StringField(null=False, resolve=lambda *args: 'RESOLVED')
|
2015-10-01 11:54:52 +03:00
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
field_type = f.internal_field(schema)
|
2015-10-03 08:17:51 +03:00
|
|
|
assert 'RESOLVED' == field_type.resolver(ot, 2, 3)
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_resolve_type_custom():
|
|
|
|
class MyCustomType(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Schema(object):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def get_type(self, name):
|
|
|
|
if name == 'MyCustomType':
|
|
|
|
return MyCustomType
|
|
|
|
|
|
|
|
s = Schema()
|
|
|
|
|
|
|
|
f = Field('MyCustomType')
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
field_type = f.get_object_type(s)
|
|
|
|
assert field_type == MyCustomType
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_resolve_type_custom():
|
|
|
|
s = Schema()
|
|
|
|
|
|
|
|
f = Field('self')
|
2015-09-24 12:11:50 +03:00
|
|
|
f.contribute_to_class(ot, 'field_name')
|
2015-10-01 11:54:52 +03:00
|
|
|
field_type = f.get_object_type(s)
|
|
|
|
assert field_type == ot
|