Completed Python3 Compatibility!

This commit is contained in:
Syrus Akbary 2015-10-05 22:59:23 -07:00
parent b58269ce72
commit fe510dc686
10 changed files with 38 additions and 15 deletions

View File

@ -2,6 +2,10 @@ language: python
sudo: false sudo: false
python: python:
- 2.7 - 2.7
- 3.3
- 3.4
- 3.5
- pypy
install: install:
- pip install pytest pytest-cov coveralls flake8 six blinker - pip install pytest pytest-cov coveralls flake8 six blinker
# - pip install -e .[django] # TODO: Commented until graphqllib is in pypi # - pip install -e .[django] # TODO: Commented until graphqllib is in pypi

View File

@ -20,7 +20,7 @@ class GraphQLView(View):
def format_result(result): def format_result(result):
data = {'data': result.data} data = {'data': result.data}
if result.errors: if result.errors:
data['errors'] = map(form_error, result.errors) data['errors'] = list(map(form_error, result.errors))
return data return data
@ -42,7 +42,6 @@ class GraphQLView(View):
if settings.DEBUG: if settings.DEBUG:
raise e raise e
return self.response_errors(e) return self.response_errors(e)
return JsonResponse(data) return JsonResponse(data)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -58,7 +57,7 @@ class GraphQLView(View):
content_type = self.get_content_type(request) content_type = self.get_content_type(request)
if content_type == 'application/json': if content_type == 'application/json':
try: try:
received_json_data = json.loads(request.body) received_json_data = json.loads(request.body.decode())
query = received_json_data.get('query') query = received_json_data.get('query')
except ValueError: except ValueError:
return self.response_errors(ValueError("Malformed json body in the post data")) return self.response_errors(ValueError("Malformed json body in the post data"))

View File

@ -117,9 +117,6 @@ class BaseObjectType(object):
def get_field(self, field): def get_field(self, field):
return getattr(self.instance, field, None) return getattr(self.instance, field, None)
def __eq__(self, other):
return self.instance.__eq__(other)
def resolve(self, field_name, args, info): def resolve(self, field_name, args, info):
custom_resolve_fn = 'resolve_%s' % field_name custom_resolve_fn = 'resolve_%s' % field_name
if hasattr(self, custom_resolve_fn): if hasattr(self, custom_resolve_fn):

View File

@ -40,6 +40,12 @@ setup(
'Intended Audience :: Developers', 'Intended Audience :: Developers',
'Topic :: Software Development :: Libraries', 'Topic :: Software Development :: Libraries',
'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: Implementation :: PyPy',
], ],
keywords='api graphql protocol rest relay graphene', keywords='api graphql protocol rest relay graphene',

View File

@ -9,6 +9,8 @@ from graphene.contrib.django import (
) )
from .models import Reporter, Article from .models import Reporter, Article
from tests.utils import assert_equal_lists
def test_should_raise_if_no_model(): def test_should_raise_if_no_model():
with raises(Exception) as excinfo: with raises(Exception) as excinfo:
@ -49,8 +51,10 @@ def test_should_map_fields_correctly():
class Meta: class Meta:
model = Reporter model = Reporter
assert ReporterType2._meta.fields_map.keys( assert_equal_lists(
) == ['articles', 'first_name', 'last_name', 'email', 'pets', 'id'] ReporterType2._meta.fields_map.keys(),
['articles', 'first_name', 'last_name', 'email', 'pets', 'id']
)
def test_should_map_fields(): def test_should_map_fields():
@ -93,7 +97,10 @@ def test_should_map_only_few_fields():
class Meta: class Meta:
model = Reporter model = Reporter
only_fields = ('id', 'email') only_fields = ('id', 'email')
assert Reporter2._meta.fields_map.keys() == ['id', 'email'] assert_equal_lists(
Reporter2._meta.fields_map.keys(),
['id', 'email']
)
def test_should_node(): def test_should_node():

View File

@ -18,6 +18,8 @@ from graphene.contrib.django.types import (
from .models import Reporter, Article from .models import Reporter, Article
from tests.utils import assert_equal_lists
class Character(DjangoInterface): class Character(DjangoInterface):
@ -48,8 +50,10 @@ def test_pseudo_interface():
assert Character._meta.interface is True assert Character._meta.interface is True
assert isinstance(object_type, GraphQLInterfaceType) assert isinstance(object_type, GraphQLInterfaceType)
assert Character._meta.model == Reporter assert Character._meta.model == Reporter
assert object_type.get_fields().keys() == [ assert_equal_lists(
'articles', 'firstName', 'lastName', 'email', 'pets', 'id'] object_type.get_fields().keys(),
['articles', 'firstName', 'lastName', 'email', 'pets', 'id']
)
def test_interface_resolve_type(): def test_interface_resolve_type():

View File

@ -20,7 +20,7 @@ from graphene.contrib.django.types import (
def format_response(response): def format_response(response):
return json.loads(response.content) return json.loads(response.content.decode())
def test_client_get_no_query(settings, client): def test_client_get_no_query(settings, client):

View File

@ -19,6 +19,7 @@ from graphene import (
Schema Schema
) )
from tests.utils import assert_equal_lists
schema = Schema(name='My own schema') schema = Schema(name='My own schema')
@ -104,5 +105,7 @@ def test_query_schema_execute():
def test_schema_get_type_map(): def test_schema_get_type_map():
assert schema.schema.get_type_map().keys() == [ assert_equal_lists(
'__Field', 'String', 'Pet', 'Character', '__InputValue', '__Directive', '__TypeKind', '__Schema', '__Type', 'Human', '__EnumValue', 'Boolean'] schema.schema.get_type_map().keys(),
['__Field', 'String', 'Pet', 'Character', '__InputValue', '__Directive', '__TypeKind', '__Schema', '__Type', 'Human', '__EnumValue', 'Boolean']
)

3
tests/utils.py Normal file
View File

@ -0,0 +1,3 @@
def assert_equal_lists(l1, l2):
assert sorted(l1) == sorted(l2)

View File

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py27 envlist = py27,py33,py34,py35,pypy
[testenv] [testenv]
deps= deps=