2015-09-28 10:34:25 +03:00
|
|
|
from py.test import raises
|
|
|
|
from collections import namedtuple
|
|
|
|
from pytest import raises
|
|
|
|
import graphene
|
|
|
|
from graphene import relay
|
|
|
|
from graphene.contrib.django import (
|
|
|
|
DjangoObjectType,
|
|
|
|
DjangoNode
|
|
|
|
)
|
|
|
|
from .models import Reporter, Article
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_raise_if_no_model():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
class Character1(DjangoObjectType):
|
|
|
|
pass
|
|
|
|
assert 'model in the Meta' in str(excinfo.value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_raise_if_model_is_invalid():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
class Character2(DjangoObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-28 10:34:25 +03:00
|
|
|
class Meta:
|
|
|
|
model = 1
|
|
|
|
assert 'not a Django model' in str(excinfo.value)
|
|
|
|
|
|
|
|
|
2015-09-29 09:29:10 +03:00
|
|
|
def test_should_raise_if_model_is_invalid():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
class ReporterTypeError(DjangoObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-29 09:29:10 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
only_fields = ('articles', )
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=ReporterTypeError)
|
|
|
|
query = '''
|
|
|
|
query ReporterQuery {
|
|
|
|
articles
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
assert 'articles (Article) model not mapped in current schema' in str(
|
|
|
|
excinfo.value)
|
2015-09-29 09:29:10 +03:00
|
|
|
|
2015-09-30 09:40:40 +03:00
|
|
|
|
|
|
|
def test_should_map_fields_correctly():
|
|
|
|
class ReporterType2(DjangoObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-30 09:40:40 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2015-10-03 08:17:51 +03:00
|
|
|
assert ReporterType2._meta.fields_map.keys(
|
|
|
|
) == ['articles', 'first_name', 'last_name', 'id', 'email']
|
|
|
|
|
2015-09-30 09:40:40 +03:00
|
|
|
|
2015-09-28 10:34:25 +03:00
|
|
|
def test_should_map_fields():
|
|
|
|
class ReporterType(DjangoObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-28 10:34:25 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
|
2015-09-29 09:29:10 +03:00
|
|
|
class Query2(graphene.ObjectType):
|
2015-09-28 10:34:25 +03:00
|
|
|
reporter = graphene.Field(ReporterType)
|
|
|
|
|
|
|
|
def resolve_reporter(self, *args, **kwargs):
|
|
|
|
return ReporterType(Reporter(first_name='ABA', last_name='X'))
|
|
|
|
|
|
|
|
query = '''
|
|
|
|
query ReporterQuery {
|
|
|
|
reporter {
|
|
|
|
first_name,
|
|
|
|
last_name,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
expected = {
|
|
|
|
'reporter': {
|
|
|
|
'first_name': 'ABA',
|
|
|
|
'last_name': 'X',
|
|
|
|
'email': ''
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 09:29:10 +03:00
|
|
|
Schema = graphene.Schema(query=Query2)
|
2015-09-28 10:34:25 +03:00
|
|
|
result = Schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_map_only_few_fields():
|
|
|
|
class Reporter2(DjangoObjectType):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-28 10:34:25 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
only_fields = ('id', 'email')
|
|
|
|
assert Reporter2._meta.fields_map.keys() == ['id', 'email']
|
|
|
|
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-28 10:34:25 +03:00
|
|
|
def test_should_node():
|
|
|
|
class ReporterNodeType(DjangoNode):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-28 10:34:25 +03:00
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_node(cls, id):
|
|
|
|
return ReporterNodeType(Reporter(id=2, first_name='Cookie Monster'))
|
|
|
|
|
2015-09-29 09:29:10 +03:00
|
|
|
def resolve_articles(self, *args, **kwargs):
|
|
|
|
return [ArticleNodeType(Article(headline='Hi!'))]
|
|
|
|
|
2015-09-28 11:51:51 +03:00
|
|
|
class ArticleNodeType(DjangoNode):
|
2015-10-03 08:17:51 +03:00
|
|
|
|
2015-09-28 11:51:51 +03:00
|
|
|
class Meta:
|
|
|
|
model = Article
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_node(cls, id):
|
2015-09-29 09:29:10 +03:00
|
|
|
return ArticleNodeType(Article(id=1, headline='Article node'))
|
2015-09-28 11:51:51 +03:00
|
|
|
|
2015-09-29 09:29:10 +03:00
|
|
|
class Query1(graphene.ObjectType):
|
2015-09-28 10:34:25 +03:00
|
|
|
node = relay.NodeField()
|
|
|
|
reporter = graphene.Field(ReporterNodeType)
|
2015-10-01 11:54:52 +03:00
|
|
|
article = graphene.Field(ArticleNodeType)
|
2015-09-28 10:34:25 +03:00
|
|
|
|
|
|
|
def resolve_reporter(self, *args, **kwargs):
|
|
|
|
return ReporterNodeType(Reporter(id=1, first_name='ABA', last_name='X'))
|
|
|
|
|
|
|
|
query = '''
|
|
|
|
query ReporterQuery {
|
|
|
|
reporter {
|
|
|
|
id,
|
|
|
|
first_name,
|
2015-09-29 09:29:10 +03:00
|
|
|
articles {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
headline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 10:34:25 +03:00
|
|
|
last_name,
|
|
|
|
email
|
|
|
|
}
|
2015-09-29 09:29:10 +03:00
|
|
|
my_article: node(id:"QXJ0aWNsZU5vZGVUeXBlOjE=") {
|
2015-09-28 10:34:25 +03:00
|
|
|
id
|
|
|
|
... on ReporterNodeType {
|
|
|
|
first_name
|
|
|
|
}
|
2015-09-29 09:29:10 +03:00
|
|
|
... on ArticleNodeType {
|
|
|
|
headline
|
|
|
|
}
|
2015-09-28 10:34:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
expected = {
|
|
|
|
'reporter': {
|
|
|
|
'id': 'UmVwb3J0ZXJOb2RlVHlwZTox',
|
|
|
|
'first_name': 'ABA',
|
|
|
|
'last_name': 'X',
|
2015-09-29 09:29:10 +03:00
|
|
|
'email': '',
|
|
|
|
'articles': {
|
2015-10-03 08:17:51 +03:00
|
|
|
'edges': [{
|
|
|
|
'node': {
|
|
|
|
'headline': 'Hi!'
|
|
|
|
}
|
|
|
|
}]
|
2015-09-29 09:29:10 +03:00
|
|
|
},
|
2015-09-28 10:34:25 +03:00
|
|
|
},
|
2015-09-29 09:29:10 +03:00
|
|
|
'my_article': {
|
|
|
|
'id': 'QXJ0aWNsZU5vZGVUeXBlOjE=',
|
|
|
|
'headline': 'Article node'
|
2015-09-28 10:34:25 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 09:29:10 +03:00
|
|
|
Schema = graphene.Schema(query=Query1)
|
2015-09-28 10:34:25 +03:00
|
|
|
result = Schema.execute(query)
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|