Fixed tests with django starwars

This commit is contained in:
Syrus Akbary 2015-09-29 23:50:23 -07:00
parent 35ec787501
commit 1e8746830e
4 changed files with 153 additions and 142 deletions

View File

@ -8,6 +8,8 @@ from graphene.utils import cached_property
from graphene.env import get_global_schema from graphene.env import get_global_schema
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.db.models.manager import Manager
def get_type_for_model(schema, model): def get_type_for_model(schema, model):
schema = schema or get_global_schema() schema = schema or get_global_schema()
@ -20,9 +22,9 @@ def get_type_for_model(schema, model):
class DjangoConnectionField(relay.ConnectionField): class DjangoConnectionField(relay.ConnectionField):
def wrap_resolved(self, value, instance, args, info): def wrap_resolved(self, value, instance, args, info):
if isinstance(value, QuerySet): if isinstance(value, (QuerySet, Manager)):
cls = instance.__class__ cls = instance.__class__
value = [cls(s) for s in value] value = [cls(s) for s in value.all()]
return value return value

View File

@ -21,8 +21,9 @@ class ConnectionField(Field):
def resolve(self, instance, args, info): def resolve(self, instance, args, info):
resolved = super(ConnectionField, self).resolve(instance, args, info) resolved = super(ConnectionField, self).resolve(instance, args, info)
if resolved: if resolved:
assert isinstance(resolved, collections.Iterable), 'Resolved value from the connection field have to be iterable'
resolved = self.wrap_resolved(resolved, instance, args, info) resolved = self.wrap_resolved(resolved, instance, args, info)
print resolved
assert isinstance(resolved, collections.Iterable), 'Resolved value from the connection field have to be iterable'
return connectionFromArray(resolved, args) return connectionFromArray(resolved, args)
@cached_property @cached_property

View File

@ -1,43 +1,42 @@
# import pytest import pytest
# from graphql.core import graphql from graphql.core import graphql
# from .models import * from .models import *
# from .schema import schema from .schema import schema
# from .data import initialize, getFaction from .data import initialize
# pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
# def test_correct_fetch_first_ship_rebels(): def test_correct_fetch_first_ship_rebels():
# initialize() initialize()
# print schema.Faction._meta.fields_map query = '''
# query = ''' query RebelsShipsQuery {
# query RebelsShipsQuery { rebels {
# rebels { name,
# name, ships(first: 1) {
# ships(first: 1) { edges {
# edges { node {
# node { name
# name }
# } }
# } }
# } }
# } }
# } '''
# ''' expected = {
# expected = { 'rebels': {
# 'rebels': { 'name': 'Alliance to Restore the Republic',
# 'name': 'Alliance to Restore the Republic', 'ships': {
# 'ships': { 'edges': [
# 'edges': [ {
# { 'node': {
# 'node': { 'name': 'X-Wing'
# 'name': 'X-Wing' }
# } }
# } ]
# ] }
# } }
# } }
# } result = schema.execute(query)
# result = schema.execute(query) assert not result.errors
# assert not result.errors assert result.data == expected
# assert result.data == expected

View File

@ -1,105 +1,114 @@
# from pytest import raises import pytest
# from graphql.core import graphql from pytest import raises
from graphql.core import graphql
from .data import initialize
# from .schema import schema from .schema import schema
# def test_correctly_fetches_id_name_rebels(): pytestmark = pytest.mark.django_db
# query = '''
# query RebelsQuery {
# rebels {
# id
# name
# }
# }
# '''
# expected = {
# 'rebels': {
# 'id': 'RmFjdGlvbjox',
# 'name': 'Alliance to Restore the Republic'
# }
# }
# result = schema.execute(query)
# assert not result.errors
# assert result.data == expected
# def test_correctly_refetches_rebels(): def test_correctly_fetches_id_name_rebels():
# query = ''' initialize()
# query RebelsRefetchQuery { query = '''
# node(id: "RmFjdGlvbjox") { query RebelsQuery {
# id rebels {
# ... on Faction { id
# name name
# } }
# } }
# } '''
# ''' expected = {
# expected = { 'rebels': {
# 'node': { 'id': 'RmFjdGlvbjox',
# 'id': 'RmFjdGlvbjox', 'name': 'Alliance to Restore the Republic'
# 'name': 'Alliance to Restore the Republic' }
# } }
# } result = schema.execute(query)
# result = schema.execute(query) assert not result.errors
# assert not result.errors assert result.data == expected
# assert result.data == expected
# def test_correctly_fetches_id_name_empire(): def test_correctly_refetches_rebels():
# query = ''' initialize()
# query EmpireQuery { query = '''
# empire { query RebelsRefetchQuery {
# id node(id: "RmFjdGlvbjox") {
# name id
# } ... on Faction {
# } name
# ''' }
# expected = { }
# 'empire': { }
# 'id': 'RmFjdGlvbjoy', '''
# 'name': 'Galactic Empire' expected = {
# } 'node': {
# } 'id': 'RmFjdGlvbjox',
# result = schema.execute(query) 'name': 'Alliance to Restore the Republic'
# assert not result.errors }
# assert result.data == expected }
result = schema.execute(query)
assert not result.errors
assert result.data == expected
# def test_correctly_refetches_empire(): def test_correctly_fetches_id_name_empire():
# query = ''' initialize()
# query EmpireRefetchQuery { query = '''
# node(id: "RmFjdGlvbjoy") { query EmpireQuery {
# id empire {
# ... on Faction { id
# name name
# } }
# } }
# } '''
# ''' expected = {
# expected = { 'empire': {
# 'node': { 'id': 'RmFjdGlvbjoy',
# 'id': 'RmFjdGlvbjoy', 'name': 'Galactic Empire'
# 'name': 'Galactic Empire' }
# } }
# } result = schema.execute(query)
# result = schema.execute(query) assert not result.errors
# assert not result.errors assert result.data == expected
# assert result.data == expected
# def test_correctly_refetches_xwing(): def test_correctly_refetches_empire():
# query = ''' initialize()
# query XWingRefetchQuery { query = '''
# node(id: "U2hpcDox") { query EmpireRefetchQuery {
# id node(id: "RmFjdGlvbjoy") {
# ... on Ship { id
# name ... on Faction {
# } name
# } }
# } }
# ''' }
# expected = { '''
# 'node': { expected = {
# 'id': 'U2hpcDox', 'node': {
# 'name': 'X-Wing' 'id': 'RmFjdGlvbjoy',
# } 'name': 'Galactic Empire'
# } }
# result = schema.execute(query) }
# assert not result.errors result = schema.execute(query)
# assert result.data == expected assert not result.errors
assert result.data == expected
def test_correctly_refetches_xwing():
initialize()
query = '''
query XWingRefetchQuery {
node(id: "U2hpcDox") {
id
... on Ship {
name
}
}
}
'''
expected = {
'node': {
'id': 'U2hpcDox',
'name': 'X-Wing'
}
}
result = schema.execute(query)
assert not result.errors
assert result.data == expected