From 1e8746830e7ee05f86a6bdc059120997b4f35bfa Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 29 Sep 2015 23:50:23 -0700 Subject: [PATCH] Fixed tests with django starwars --- graphene/contrib/django/fields.py | 6 +- graphene/relay/fields.py | 3 +- tests/starwars_django/test_connections.py | 79 ++++--- .../test_objectidentification.py | 207 +++++++++--------- 4 files changed, 153 insertions(+), 142 deletions(-) diff --git a/graphene/contrib/django/fields.py b/graphene/contrib/django/fields.py index a6d54f62..9b81ca1e 100644 --- a/graphene/contrib/django/fields.py +++ b/graphene/contrib/django/fields.py @@ -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 diff --git a/graphene/relay/fields.py b/graphene/relay/fields.py index a888919d..963f9827 100644 --- a/graphene/relay/fields.py +++ b/graphene/relay/fields.py @@ -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 diff --git a/tests/starwars_django/test_connections.py b/tests/starwars_django/test_connections.py index 5c033216..42fd81b6 100644 --- a/tests/starwars_django/test_connections.py +++ b/tests/starwars_django/test_connections.py @@ -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 diff --git a/tests/starwars_django/test_objectidentification.py b/tests/starwars_django/test_objectidentification.py index 3040bd92..93bf1b71 100644 --- a/tests/starwars_django/test_objectidentification.py +++ b/tests/starwars_django/test_objectidentification.py @@ -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