Fix compatibility with pypy

This commit is contained in:
Patrick Arminio 2017-06-23 12:48:32 +01:00
parent 245ebe3d91
commit 7fd6125199
3 changed files with 16 additions and 10 deletions

View File

@ -3,10 +3,11 @@ class MissingType(object):
try: try:
from django.db.models.related import RelatedObject # Postgres fields are only available in Django with psycopg2 installed
except: # and we cannot have psycopg2 on PyPy
# Improved compatibility for Django 1.6 from django.contrib.postgres.fields import ArrayField, HStoreField, RangeField
RelatedObject = MissingType except ImportError:
ArrayField, HStoreField, JSONField, RangeField = (MissingType, ) * 4
try: try:

View File

@ -1,6 +1,5 @@
from django.db import models from django.db import models
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.contrib.postgres.fields import ArrayField, HStoreField, RangeField
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List, from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
NonNull, String) NonNull, String)
@ -10,8 +9,8 @@ from graphene.types.json import JSONString
from graphene.utils.str_converters import to_camel_case, to_const from graphene.utils.str_converters import to_camel_case, to_const
from graphql import assert_valid_name from graphql import assert_valid_name
from .compat import (ArrayField, HStoreField, JSONField, RangeField,
from .compat import JSONField, RelatedObject RelatedObject)
from .fields import get_connection_field, DjangoListField from .fields import get_connection_field, DjangoListField
from .utils import get_related_model, import_single_dispatch from .utils import get_related_model, import_single_dispatch

View File

@ -1,8 +1,6 @@
import pytest import pytest
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.contrib.postgres.fields import ArrayField, HStoreField
from py.test import raises from py.test import raises
import graphene import graphene
@ -10,7 +8,7 @@ from graphene.relay import ConnectionField, Node
from graphene.types.datetime import DateTime, Time from graphene.types.datetime import DateTime, Time
from graphene.types.json import JSONString from graphene.types.json import JSONString
from ..compat import JSONField, MissingType from ..compat import JSONField, RelatedObject
from ..converter import convert_django_field, convert_django_field_with_choices from ..converter import convert_django_field, convert_django_field_with_choices
from ..registry import Registry from ..registry import Registry
from ..types import DjangoObjectType from ..types import DjangoObjectType
@ -264,6 +262,8 @@ def test_should_onetoone_reverse_convert_model():
assert dynamic_field.type == A assert dynamic_field.type == A
@pytest.mark.skipif(ArrayField is MissingType,
reason="ArrayField should exist")
def test_should_postgres_array_convert_list(): def test_should_postgres_array_convert_list():
field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100)) field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100))
assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type, graphene.NonNull)
@ -271,6 +271,8 @@ def test_should_postgres_array_convert_list():
assert field.type.of_type.of_type == graphene.String assert field.type.of_type.of_type == graphene.String
@pytest.mark.skipif(ArrayField is MissingType,
reason="ArrayField should exist")
def test_should_postgres_array_multiple_convert_list(): def test_should_postgres_array_multiple_convert_list():
field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100))) field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100)))
assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type, graphene.NonNull)
@ -279,6 +281,8 @@ def test_should_postgres_array_multiple_convert_list():
assert field.type.of_type.of_type.of_type == graphene.String assert field.type.of_type.of_type.of_type == graphene.String
@pytest.mark.skipif(HStoreField is MissingType,
reason="HStoreField should exist")
def test_should_postgres_hstore_convert_string(): def test_should_postgres_hstore_convert_string():
assert_conversion(HStoreField, JSONString) assert_conversion(HStoreField, JSONString)
@ -289,6 +293,8 @@ def test_should_postgres_json_convert_string():
assert_conversion(JSONField, JSONString) assert_conversion(JSONField, JSONString)
@pytest.mark.skipif(RangeField is MissingType,
reason="RangeField should exist")
def test_should_postgres_range_convert_list(): def test_should_postgres_range_convert_list():
from django.contrib.postgres.fields import IntegerRangeField from django.contrib.postgres.fields import IntegerRangeField
field = assert_conversion(IntegerRangeField, graphene.List) field = assert_conversion(IntegerRangeField, graphene.List)