2020-03-13 13:04:25 +03:00
|
|
|
from collections import namedtuple
|
2020-04-20 15:23:20 +03:00
|
|
|
|
|
|
|
import pytest
|
2016-09-18 02:29:00 +03:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from py.test import raises
|
|
|
|
|
|
|
|
import graphene
|
2020-04-20 15:23:20 +03:00
|
|
|
from graphene import NonNull
|
2016-09-18 03:09:56 +03:00
|
|
|
from graphene.relay import ConnectionField, Node
|
2020-04-20 15:23:20 +03:00
|
|
|
from graphene.types.datetime import Date, DateTime, Time
|
2016-09-18 02:29:00 +03:00
|
|
|
from graphene.types.json import JSONString
|
|
|
|
|
2020-08-07 12:22:15 +03:00
|
|
|
from ..compat import (
|
|
|
|
ArrayField,
|
|
|
|
HStoreField,
|
|
|
|
JSONField,
|
|
|
|
PGJSONField,
|
|
|
|
MissingType,
|
|
|
|
RangeField,
|
|
|
|
)
|
2020-03-13 13:04:25 +03:00
|
|
|
from ..converter import (
|
|
|
|
convert_django_field,
|
|
|
|
convert_django_field_with_choices,
|
|
|
|
generate_enum_name,
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
from ..registry import Registry
|
|
|
|
from ..types import DjangoObjectType
|
2016-09-18 03:09:56 +03:00
|
|
|
from .models import Article, Film, FilmDetails, Reporter
|
|
|
|
|
2016-11-23 20:24:53 +03:00
|
|
|
# from graphene.core.types.custom_scalars import DateTime, Time, JSONString
|
2016-09-18 03:09:56 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
def assert_conversion(django_field, graphene_field, *args, **kwargs):
|
2020-05-09 14:09:17 +03:00
|
|
|
_kwargs = kwargs.copy()
|
|
|
|
if "null" not in kwargs:
|
|
|
|
_kwargs["null"] = True
|
|
|
|
field = django_field(help_text="Custom Help Text", *args, **_kwargs)
|
2016-09-18 02:29:00 +03:00
|
|
|
graphene_type = convert_django_field(field)
|
|
|
|
assert isinstance(graphene_type, graphene_field)
|
|
|
|
field = graphene_type.Field()
|
2018-07-20 02:51:33 +03:00
|
|
|
assert field.description == "Custom Help Text"
|
2020-05-09 14:09:17 +03:00
|
|
|
|
|
|
|
_kwargs = kwargs.copy()
|
|
|
|
if "null" not in kwargs:
|
|
|
|
_kwargs["null"] = False
|
|
|
|
nonnull_field = django_field(*args, **_kwargs)
|
2016-09-23 06:25:00 +03:00
|
|
|
if not nonnull_field.null:
|
|
|
|
nonnull_graphene_type = convert_django_field(nonnull_field)
|
|
|
|
nonnull_field = nonnull_graphene_type.Field()
|
|
|
|
assert isinstance(nonnull_field.type, graphene.NonNull)
|
|
|
|
return nonnull_field
|
2016-09-18 02:29:00 +03:00
|
|
|
return field
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_unknown_django_field_raise_exception():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
convert_django_field(None)
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "Don't know how to convert the Django field" in str(excinfo.value)
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-12-05 23:04:29 +03:00
|
|
|
def test_should_date_time_convert_string():
|
|
|
|
assert_conversion(models.DateTimeField, DateTime)
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
def test_should_date_convert_string():
|
2017-12-05 23:04:29 +03:00
|
|
|
assert_conversion(models.DateField, Date)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2016-11-23 20:07:40 +03:00
|
|
|
def test_should_time_convert_string():
|
|
|
|
assert_conversion(models.TimeField, Time)
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_char_convert_string():
|
|
|
|
assert_conversion(models.CharField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_text_convert_string():
|
|
|
|
assert_conversion(models.TextField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_email_convert_string():
|
|
|
|
assert_conversion(models.EmailField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_slug_convert_string():
|
|
|
|
assert_conversion(models.SlugField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_url_convert_string():
|
|
|
|
assert_conversion(models.URLField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_ipaddress_convert_string():
|
|
|
|
assert_conversion(models.GenericIPAddressField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_file_convert_string():
|
|
|
|
assert_conversion(models.FileField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_image_convert_string():
|
|
|
|
assert_conversion(models.ImageField, graphene.String)
|
|
|
|
|
|
|
|
|
2019-03-27 17:21:15 +03:00
|
|
|
def test_should_file_path_field_convert_string():
|
2018-06-06 00:39:51 +03:00
|
|
|
assert_conversion(models.FilePathField, graphene.String)
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_auto_convert_id():
|
|
|
|
assert_conversion(models.AutoField, graphene.ID, primary_key=True)
|
|
|
|
|
|
|
|
|
2019-03-27 17:21:15 +03:00
|
|
|
def test_should_uuid_convert_id():
|
2017-07-25 09:42:40 +03:00
|
|
|
assert_conversion(models.UUIDField, graphene.UUID)
|
2016-10-16 02:43:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_auto_convert_duration():
|
2017-06-23 14:28:29 +03:00
|
|
|
assert_conversion(models.DurationField, graphene.Float)
|
2016-10-16 02:43:27 +03:00
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_positive_integer_convert_int():
|
|
|
|
assert_conversion(models.PositiveIntegerField, graphene.Int)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_positive_small_convert_int():
|
|
|
|
assert_conversion(models.PositiveSmallIntegerField, graphene.Int)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_small_integer_convert_int():
|
|
|
|
assert_conversion(models.SmallIntegerField, graphene.Int)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_big_integer_convert_int():
|
|
|
|
assert_conversion(models.BigIntegerField, graphene.Int)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_integer_convert_int():
|
|
|
|
assert_conversion(models.IntegerField, graphene.Int)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_boolean_convert_boolean():
|
2020-05-09 14:09:17 +03:00
|
|
|
assert_conversion(models.BooleanField, graphene.Boolean, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_boolean_convert_non_null_boolean():
|
|
|
|
field = assert_conversion(models.BooleanField, graphene.Boolean, null=False)
|
|
|
|
assert isinstance(field.type, graphene.NonNull)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert field.type.of_type == graphene.Boolean
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_nullboolean_convert_boolean():
|
|
|
|
assert_conversion(models.NullBooleanField, graphene.Boolean)
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_with_choices_convert_enum():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = models.CharField(
|
|
|
|
help_text="Language", choices=(("es", "Spanish"), ("en", "English"))
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class TranslatedModel(models.Model):
|
|
|
|
language = field
|
|
|
|
|
|
|
|
class Meta:
|
2018-07-20 02:51:33 +03:00
|
|
|
app_label = "test"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
graphene_type = convert_django_field_with_choices(field)
|
|
|
|
assert isinstance(graphene_type, graphene.Enum)
|
2018-07-20 02:51:33 +03:00
|
|
|
assert graphene_type._meta.name == "TranslatedModelLanguage"
|
|
|
|
assert graphene_type._meta.enum.__members__["ES"].value == "es"
|
|
|
|
assert graphene_type._meta.enum.__members__["ES"].description == "Spanish"
|
|
|
|
assert graphene_type._meta.enum.__members__["EN"].value == "en"
|
|
|
|
assert graphene_type._meta.enum.__members__["EN"].description == "English"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_field_with_grouped_choices():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = models.CharField(
|
|
|
|
help_text="Language",
|
|
|
|
choices=(("Europe", (("es", "Spanish"), ("en", "English"))),),
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class GroupedChoicesModel(models.Model):
|
|
|
|
language = field
|
|
|
|
|
|
|
|
class Meta:
|
2018-07-20 02:51:33 +03:00
|
|
|
app_label = "test"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
convert_django_field_with_choices(field)
|
|
|
|
|
|
|
|
|
|
|
|
def test_field_with_choices_gettext():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = models.CharField(
|
|
|
|
help_text="Language", choices=(("es", _("Spanish")), ("en", _("English")))
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class TranslatedChoicesModel(models.Model):
|
|
|
|
language = field
|
|
|
|
|
|
|
|
class Meta:
|
2018-07-20 02:51:33 +03:00
|
|
|
app_label = "test"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
convert_django_field_with_choices(field)
|
|
|
|
|
|
|
|
|
2017-01-04 19:23:17 +03:00
|
|
|
def test_field_with_choices_collision():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = models.CharField(
|
|
|
|
help_text="Timezone",
|
|
|
|
choices=(
|
|
|
|
("Etc/GMT+1+2", "Fake choice to produce double collision"),
|
|
|
|
("Etc/GMT+1", "Greenwich Mean Time +1"),
|
|
|
|
("Etc/GMT-1", "Greenwich Mean Time -1"),
|
|
|
|
),
|
|
|
|
)
|
2017-01-04 19:23:17 +03:00
|
|
|
|
|
|
|
class CollisionChoicesModel(models.Model):
|
|
|
|
timezone = field
|
|
|
|
|
|
|
|
class Meta:
|
2018-07-20 02:51:33 +03:00
|
|
|
app_label = "test"
|
2017-01-04 19:23:17 +03:00
|
|
|
|
|
|
|
convert_django_field_with_choices(field)
|
|
|
|
|
|
|
|
|
2019-06-17 20:48:29 +03:00
|
|
|
def test_field_with_choices_convert_enum_false():
|
|
|
|
field = models.CharField(
|
|
|
|
help_text="Language", choices=(("es", "Spanish"), ("en", "English"))
|
|
|
|
)
|
|
|
|
|
|
|
|
class TranslatedModel(models.Model):
|
|
|
|
language = field
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = "test"
|
|
|
|
|
|
|
|
graphene_type = convert_django_field_with_choices(
|
|
|
|
field, convert_choices_to_enum=False
|
|
|
|
)
|
|
|
|
assert isinstance(graphene_type, graphene.String)
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_float_convert_float():
|
|
|
|
assert_conversion(models.FloatField, graphene.Float)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_manytomany_convert_connectionorlist():
|
|
|
|
registry = Registry()
|
|
|
|
dynamic_field = convert_django_field(Reporter._meta.local_many_to_many[0], registry)
|
|
|
|
assert not dynamic_field.get_type()
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_manytomany_convert_connectionorlist_list():
|
|
|
|
class A(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
graphene_field = convert_django_field(
|
|
|
|
Reporter._meta.local_many_to_many[0], A._meta.registry
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert isinstance(graphene_field, graphene.Dynamic)
|
|
|
|
dynamic_field = graphene_field.get_type()
|
|
|
|
assert isinstance(dynamic_field, graphene.Field)
|
2019-06-25 18:30:30 +03:00
|
|
|
# A NonNull List of NonNull A ([A!]!)
|
|
|
|
# https://github.com/graphql-python/graphene-django/issues/448
|
|
|
|
assert isinstance(dynamic_field.type, NonNull)
|
|
|
|
assert isinstance(dynamic_field.type.of_type, graphene.List)
|
|
|
|
assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
|
|
|
|
assert dynamic_field.type.of_type.of_type.of_type == A
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_manytomany_convert_connectionorlist_connection():
|
|
|
|
class A(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
graphene_field = convert_django_field(
|
|
|
|
Reporter._meta.local_many_to_many[0], A._meta.registry
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert isinstance(graphene_field, graphene.Dynamic)
|
|
|
|
dynamic_field = graphene_field.get_type()
|
|
|
|
assert isinstance(dynamic_field, ConnectionField)
|
2019-08-01 19:31:18 +03:00
|
|
|
assert dynamic_field.type.of_type == A._meta.connection
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_manytoone_convert_connectionorlist():
|
|
|
|
class A(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
|
|
|
|
2019-04-30 12:02:23 +03:00
|
|
|
graphene_field = convert_django_field(Reporter.articles.rel, A._meta.registry)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert isinstance(graphene_field, graphene.Dynamic)
|
|
|
|
dynamic_field = graphene_field.get_type()
|
|
|
|
assert isinstance(dynamic_field, graphene.Field)
|
2019-06-25 18:30:30 +03:00
|
|
|
# a NonNull List of NonNull A ([A!]!)
|
|
|
|
assert isinstance(dynamic_field.type, NonNull)
|
|
|
|
assert isinstance(dynamic_field.type.of_type, graphene.List)
|
|
|
|
assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
|
|
|
|
assert dynamic_field.type.of_type.of_type.of_type == A
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_onetoone_reverse_convert_model():
|
|
|
|
class A(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = FilmDetails
|
|
|
|
|
2019-04-30 12:02:23 +03:00
|
|
|
graphene_field = convert_django_field(Film.details.related, A._meta.registry)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert isinstance(graphene_field, graphene.Dynamic)
|
|
|
|
dynamic_field = graphene_field.get_type()
|
|
|
|
assert isinstance(dynamic_field, graphene.Field)
|
2016-09-23 06:25:00 +03:00
|
|
|
assert dynamic_field.type == A
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist")
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_postgres_array_convert_list():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = assert_conversion(
|
|
|
|
ArrayField, graphene.List, models.CharField(max_length=100)
|
|
|
|
)
|
2016-09-23 05:57:28 +03:00
|
|
|
assert isinstance(field.type, graphene.NonNull)
|
|
|
|
assert isinstance(field.type.of_type, graphene.List)
|
2020-06-10 19:52:45 +03:00
|
|
|
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
|
|
|
|
assert field.type.of_type.of_type.of_type == graphene.String
|
|
|
|
|
|
|
|
field = assert_conversion(
|
|
|
|
ArrayField, graphene.List, models.CharField(max_length=100, null=True)
|
|
|
|
)
|
|
|
|
assert isinstance(field.type, graphene.NonNull)
|
|
|
|
assert isinstance(field.type.of_type, graphene.List)
|
2016-09-23 05:57:28 +03:00
|
|
|
assert field.type.of_type.of_type == graphene.String
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist")
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_postgres_array_multiple_convert_list():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = assert_conversion(
|
|
|
|
ArrayField, graphene.List, ArrayField(models.CharField(max_length=100))
|
|
|
|
)
|
2016-09-23 05:57:28 +03:00
|
|
|
assert isinstance(field.type, graphene.NonNull)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert isinstance(field.type.of_type, graphene.List)
|
2016-09-23 05:57:28 +03:00
|
|
|
assert isinstance(field.type.of_type.of_type, graphene.List)
|
2020-06-10 19:52:45 +03:00
|
|
|
assert isinstance(field.type.of_type.of_type.of_type, graphene.NonNull)
|
|
|
|
assert field.type.of_type.of_type.of_type.of_type == graphene.String
|
|
|
|
|
|
|
|
field = assert_conversion(
|
|
|
|
ArrayField,
|
|
|
|
graphene.List,
|
|
|
|
ArrayField(models.CharField(max_length=100, null=True)),
|
|
|
|
)
|
|
|
|
assert isinstance(field.type, graphene.NonNull)
|
|
|
|
assert isinstance(field.type.of_type, graphene.List)
|
|
|
|
assert isinstance(field.type.of_type.of_type, graphene.List)
|
2016-09-23 05:57:28 +03:00
|
|
|
assert field.type.of_type.of_type.of_type == graphene.String
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(HStoreField is MissingType, reason="HStoreField should exist")
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_postgres_hstore_convert_string():
|
|
|
|
assert_conversion(HStoreField, JSONString)
|
|
|
|
|
|
|
|
|
2020-08-07 12:22:15 +03:00
|
|
|
@pytest.mark.skipif(PGJSONField is MissingType, reason="PGJSONField should exist")
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_postgres_json_convert_string():
|
2020-08-07 12:22:15 +03:00
|
|
|
assert_conversion(PGJSONField, JSONString)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
|
|
|
|
def test_should_json_convert_string():
|
2016-09-18 02:29:00 +03:00
|
|
|
assert_conversion(JSONField, JSONString)
|
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
@pytest.mark.skipif(RangeField is MissingType, reason="RangeField should exist")
|
2016-09-18 02:29:00 +03:00
|
|
|
def test_should_postgres_range_convert_list():
|
|
|
|
from django.contrib.postgres.fields import IntegerRangeField
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
field = assert_conversion(IntegerRangeField, graphene.List)
|
2016-09-23 05:57:28 +03:00
|
|
|
assert isinstance(field.type, graphene.NonNull)
|
|
|
|
assert isinstance(field.type.of_type, graphene.List)
|
2020-06-10 19:52:45 +03:00
|
|
|
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
|
|
|
|
assert field.type.of_type.of_type.of_type == graphene.Int
|
2020-03-13 13:04:25 +03:00
|
|
|
|
|
|
|
|
2020-04-20 15:23:20 +03:00
|
|
|
def test_generate_enum_name(graphene_settings):
|
2020-03-13 13:04:25 +03:00
|
|
|
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
|
|
|
|
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
|
|
|
|
|
|
|
|
# Simple case
|
|
|
|
field = graphene.Field(graphene.String, name="type")
|
|
|
|
model_meta = MockDjangoModelMeta(app_label="users", object_name="User")
|
|
|
|
assert generate_enum_name(model_meta, field) == "UsersUserTypeChoices"
|
|
|
|
|
|
|
|
# More complicated multiple work case
|
|
|
|
field = graphene.Field(graphene.String, name="fizz_buzz")
|
|
|
|
model_meta = MockDjangoModelMeta(
|
|
|
|
app_label="some_long_app_name", object_name="SomeObject"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
generate_enum_name(model_meta, field)
|
|
|
|
== "SomeLongAppNameSomeObjectFizzBuzzChoices"
|
|
|
|
)
|