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 django.db.models.query import QuerySet
from django.db.models.manager import Manager
def get_type_for_model(schema, model):
schema = schema or get_global_schema()
@ -20,9 +22,9 @@ def get_type_for_model(schema, model):
class DjangoConnectionField(relay.ConnectionField):
def wrap_resolved(self, value, instance, args, info):
if isinstance(value, QuerySet):
if isinstance(value, (QuerySet, Manager)):
cls = instance.__class__
value = [cls(s) for s in value]
value = [cls(s) for s in value.all()]
return value

View File

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

View File

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

View File

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