2015-10-01 11:54:52 +03:00
|
|
|
from py.test import raises
|
|
|
|
from collections import namedtuple
|
|
|
|
from pytest import raises
|
|
|
|
from graphql.core import graphql
|
|
|
|
from graphene.core.fields import (
|
|
|
|
Field,
|
|
|
|
StringField,
|
|
|
|
ListField,
|
|
|
|
)
|
|
|
|
from graphql.core.type import (
|
|
|
|
GraphQLObjectType,
|
|
|
|
GraphQLSchema,
|
|
|
|
GraphQLInterfaceType
|
|
|
|
)
|
|
|
|
|
|
|
|
from graphene import (
|
|
|
|
Interface,
|
|
|
|
ObjectType,
|
|
|
|
Schema
|
|
|
|
)
|
|
|
|
|
2015-10-06 08:59:23 +03:00
|
|
|
from tests.utils import assert_equal_lists
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
schema = Schema(name='My own schema')
|
|
|
|
|
|
|
|
|
|
|
|
class Character(Interface):
|
|
|
|
name = StringField()
|
|
|
|
|
|
|
|
|
|
|
|
class Pet(ObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
type = StringField(resolve=lambda *_: 'Dog')
|
2015-10-01 11:54:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Human(Character):
|
|
|
|
friends = ListField(Character)
|
|
|
|
pet = Field(Pet)
|
|
|
|
|
|
|
|
def resolve_name(self, *args):
|
|
|
|
return 'Peter'
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def resolve_friend(self, *args):
|
|
|
|
return Human(object())
|
|
|
|
|
|
|
|
def resolve_pet(self, *args):
|
|
|
|
return Pet(object())
|
|
|
|
|
|
|
|
schema.query = Human
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def test_get_registered_type():
|
|
|
|
assert schema.get_type('Character') == Character
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def test_get_unregistered_type():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
schema.get_type('NON_EXISTENT_MODEL')
|
|
|
|
assert 'not found' in str(excinfo.value)
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def test_schema_query():
|
|
|
|
assert schema.query == Human
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-10-01 11:54:52 +03:00
|
|
|
def test_query_schema_graphql():
|
|
|
|
a = object()
|
|
|
|
query = '''
|
|
|
|
{
|
|
|
|
name
|
|
|
|
pet {
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
expected = {
|
|
|
|
'name': 'Peter',
|
|
|
|
'pet': {
|
2015-10-03 08:17:51 +03:00
|
|
|
'type': 'Dog'
|
2015-10-01 11:54:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result = graphql(schema.schema, query, root=Human(object()))
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_query_schema_execute():
|
|
|
|
a = object()
|
|
|
|
query = '''
|
|
|
|
{
|
|
|
|
name
|
|
|
|
pet {
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
expected = {
|
|
|
|
'name': 'Peter',
|
|
|
|
'pet': {
|
2015-10-03 08:17:51 +03:00
|
|
|
'type': 'Dog'
|
2015-10-01 11:54:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result = schema.execute(query, root=object())
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_get_type_map():
|
2015-10-06 08:59:23 +03:00
|
|
|
assert_equal_lists(
|
|
|
|
schema.schema.get_type_map().keys(),
|
|
|
|
['__Field', 'String', 'Pet', 'Character', '__InputValue', '__Directive', '__TypeKind', '__Schema', '__Type', 'Human', '__EnumValue', 'Boolean']
|
|
|
|
)
|
2015-10-11 00:53:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_schema_no_query():
|
|
|
|
schema = Schema(name='My own schema')
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
schema.schema
|
|
|
|
assert 'define a base query type' in str(excinfo)
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_register():
|
|
|
|
schema = Schema(name='My own schema')
|
|
|
|
|
|
|
|
@schema.register
|
|
|
|
class MyType(ObjectType):
|
|
|
|
type = StringField(resolve=lambda *_: 'Dog')
|
|
|
|
|
|
|
|
assert schema.get_type('MyType') == MyType
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_introspect():
|
|
|
|
schema = Schema(name='My own schema')
|
|
|
|
|
|
|
|
class MyType(ObjectType):
|
|
|
|
type = StringField(resolve=lambda *_: 'Dog')
|
|
|
|
|
|
|
|
schema.query = MyType
|
|
|
|
|
|
|
|
introspection = schema.introspect()
|
|
|
|
assert '__schema' in introspection
|
|
|
|
|