Added tests for dynamic field and make more consistent.

This commit is contained in:
Markus Padourek 2016-10-13 14:47:43 +01:00
parent 88ccaec8fa
commit 822b030938
3 changed files with 44 additions and 1 deletions

View File

@ -0,0 +1,27 @@
from ..structures import List, NonNull
from ..scalars import String
from ..dynamic import Dynamic
def test_dynamic():
dynamic = Dynamic(lambda: String)
assert dynamic.get_type() == String
assert str(dynamic.get_type()) == 'String'
def test_nonnull():
dynamic = Dynamic(lambda: NonNull(String))
assert dynamic.get_type().of_type == String
assert str(dynamic.get_type()) == 'String!'
def test_list():
dynamic = Dynamic(lambda: List(String))
assert dynamic.get_type().of_type == String
assert str(dynamic.get_type()) == '[String]'
def test_list_non_null():
dynamic = Dynamic(lambda: List(NonNull(String)))
assert dynamic.get_type().of_type.of_type == String
assert str(dynamic.get_type()) == '[String!]'

View File

@ -10,6 +10,7 @@ from ..objecttype import ObjectType
from ..scalars import Int, String
from ..schema import Schema
from ..structures import List
from ..dynamic import Dynamic
def test_query():
@ -23,6 +24,19 @@ def test_query():
assert executed.data == {'hello': 'World'}
def test_query_dynamic():
class Query(ObjectType):
hello = Dynamic(lambda: String(resolver=lambda *_: 'World'))
hellos = Dynamic(lambda: List(String, resolver=lambda *_: ['Worlds']))
hello_field = Dynamic(lambda: Field(String, resolver=lambda *_: 'Field World'))
hello_schema = Schema(Query)
executed = hello_schema.execute('{ hello hellos helloField }')
assert not executed.errors
assert executed.data == {'hello': 'World', 'hellos': ['Worlds'], 'helloField': 'Field World'}
def test_query_default_value():
class MyType(ObjectType):
field = String()

View File

@ -13,12 +13,14 @@ from ..utils.str_converters import to_camel_case
from ..utils.get_unbound_function import get_unbound_function
from .dynamic import Dynamic
from .enum import Enum
from .field import Field
from .inputobjecttype import InputObjectType
from .interface import Interface
from .objecttype import ObjectType
from .scalars import ID, Boolean, Float, Int, Scalar, String
from .structures import List, NonNull
from .union import Union
from .utils import get_field_as
def is_graphene_type(_type):
@ -202,7 +204,7 @@ class TypeMap(GraphQLTypeMap):
fields = OrderedDict()
for name, field in type._meta.fields.items():
if isinstance(field, Dynamic):
field = field.get_type()
field = get_field_as(field.get_type(), _as=Field)
if not field:
continue
map = self.reducer(map, field.type)