2015-09-24 12:11:50 +03:00
|
|
|
from py.test import raises
|
|
|
|
from collections import namedtuple
|
|
|
|
from pytest import raises
|
|
|
|
|
|
|
|
from graphql.core.type import (
|
|
|
|
GraphQLField,
|
|
|
|
GraphQLNonNull,
|
|
|
|
GraphQLInt,
|
|
|
|
GraphQLString,
|
|
|
|
GraphQLBoolean,
|
|
|
|
GraphQLID,
|
|
|
|
)
|
|
|
|
|
2015-10-16 08:18:08 +03:00
|
|
|
from graphene.core.fields import (
|
|
|
|
Field,
|
|
|
|
StringField,
|
|
|
|
NonNullField
|
|
|
|
)
|
|
|
|
from graphene.core.options import Options
|
|
|
|
from graphene.core.schema import Schema
|
|
|
|
from graphene.core.types import ObjectType
|
|
|
|
|
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-10-26 05:48:22 +03:00
|
|
|
def resolve_customdoc(self, *args, **kwargs):
|
|
|
|
'''Resolver documentation'''
|
2015-09-24 12:11:50 +03:00
|
|
|
return None
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-11 00:53:46 +03:00
|
|
|
def __str__(self):
|
|
|
|
return "ObjectType"
|
|
|
|
|
2015-09-24 12:11:50 +03:00
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
|
2015-10-11 00:53:46 +03:00
|
|
|
def test_nonnullfield_type():
|
|
|
|
f = NonNullField(StringField())
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
assert isinstance(f.internal_type(schema), GraphQLNonNull)
|
|
|
|
|
|
|
|
|
2015-10-06 06:42:24 +03:00
|
|
|
def test_stringfield_type_required():
|
|
|
|
f = StringField(required=True)
|
2015-09-24 12:11:50 +03:00
|
|
|
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-06 06:42:24 +03:00
|
|
|
f = StringField(required=True, 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-26 07:39:54 +03:00
|
|
|
assert 'RESOLVED' == field_type.resolver(ot, None, None)
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_resolve_type_custom():
|
2015-10-16 08:18:08 +03:00
|
|
|
class MyCustomType(ObjectType):
|
2015-10-01 11:54:52 +03:00
|
|
|
pass
|
|
|
|
|
2015-10-16 08:18:08 +03:00
|
|
|
class OtherType(ObjectType):
|
|
|
|
pass
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
s = Schema()
|
|
|
|
|
|
|
|
f = Field('MyCustomType')
|
2015-10-16 08:18:08 +03:00
|
|
|
f.contribute_to_class(OtherType, 'field_name')
|
2015-10-01 11:54:52 +03:00
|
|
|
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
|
2015-10-11 00:53:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_orders():
|
|
|
|
f1 = Field(None)
|
|
|
|
f2 = Field(None)
|
|
|
|
assert f1 < f2
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_orders_wrong_type():
|
|
|
|
field = Field(None)
|
|
|
|
try:
|
|
|
|
assert not field < 1
|
|
|
|
except TypeError:
|
|
|
|
# Fix exception raising in Python3+
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_eq():
|
|
|
|
f1 = Field(None)
|
|
|
|
f2 = Field(None)
|
|
|
|
assert f1 != f2
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_eq_wrong_type():
|
|
|
|
field = Field(None)
|
|
|
|
assert field != 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_hash():
|
|
|
|
f1 = Field(None)
|
|
|
|
f2 = Field(None)
|
|
|
|
assert hash(f1) != hash(f2)
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_none_type_raises_error():
|
|
|
|
s = Schema()
|
|
|
|
f = Field(None)
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
f.internal_field(s)
|
|
|
|
assert str(excinfo.value) == "Internal type for field ObjectType.field_name is None"
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_str():
|
|
|
|
f = StringField()
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
assert str(f) == "ObjectType.field_name"
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_repr():
|
|
|
|
f = StringField()
|
|
|
|
assert repr(f) == "<graphene.core.fields.StringField>"
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_repr_contributed():
|
|
|
|
f = StringField()
|
|
|
|
f.contribute_to_class(ot, 'field_name')
|
|
|
|
assert repr(f) == "<graphene.core.fields.StringField: field_name>"
|
2015-10-26 05:48:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_resolve_objecttype_cos():
|
|
|
|
f = StringField()
|
|
|
|
f.contribute_to_class(ot, 'customdoc')
|
|
|
|
field = f.internal_field(schema)
|
|
|
|
assert field.description == 'Resolver documentation'
|