2017-06-26 14:11:35 +03:00
|
|
|
import copy
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
import graphene
|
2018-02-13 13:48:43 +03:00
|
|
|
from django.db import models
|
|
|
|
from graphene import InputObjectType
|
2022-11-14 13:56:18 +03:00
|
|
|
from pytest import raises
|
2018-02-13 13:48:43 +03:00
|
|
|
from rest_framework import serializers
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
from ..serializer_converter import convert_serializer_field
|
2017-06-26 16:19:55 +03:00
|
|
|
from ..types import DictType
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
|
2020-02-07 13:17:56 +03:00
|
|
|
def _get_type(
|
|
|
|
rest_framework_field, is_input=True, convert_choices_to_enum=True, **kwargs
|
|
|
|
):
|
2017-06-26 14:11:35 +03:00
|
|
|
# prevents the following error:
|
|
|
|
# AssertionError: The `source` argument is not meaningful when applied to a `child=` field.
|
|
|
|
# Remove `source=` from the field declaration.
|
|
|
|
# since we are reusing the same child in when testing the required attribute
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
if "child" in kwargs:
|
|
|
|
kwargs["child"] = copy.deepcopy(kwargs["child"])
|
2017-06-26 14:11:35 +03:00
|
|
|
|
|
|
|
field = rest_framework_field(**kwargs)
|
|
|
|
|
2020-02-07 13:17:56 +03:00
|
|
|
return convert_serializer_field(
|
|
|
|
field, is_input=is_input, convert_choices_to_enum=convert_choices_to_enum
|
|
|
|
)
|
2017-06-26 14:11:35 +03:00
|
|
|
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
def assert_conversion(rest_framework_field, graphene_field, **kwargs):
|
2018-07-20 02:51:33 +03:00
|
|
|
graphene_type = _get_type(
|
|
|
|
rest_framework_field, help_text="Custom Help Text", **kwargs
|
|
|
|
)
|
2017-05-28 15:48:44 +03:00
|
|
|
assert isinstance(graphene_type, graphene_field)
|
|
|
|
|
2017-06-26 14:11:35 +03:00
|
|
|
graphene_type_required = _get_type(
|
2018-07-20 02:51:33 +03:00
|
|
|
rest_framework_field, help_text="Custom Help Text", required=True, **kwargs
|
2017-06-26 14:11:35 +03:00
|
|
|
)
|
|
|
|
assert isinstance(graphene_type_required, graphene_field)
|
2017-05-28 15:48:44 +03:00
|
|
|
|
2017-06-26 14:11:35 +03:00
|
|
|
return graphene_type
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_unknown_rest_framework_field_raise_exception():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
convert_serializer_field(None)
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "Don't know how to convert the serializer field" in str(excinfo.value)
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_char_convert_string():
|
|
|
|
assert_conversion(serializers.CharField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_email_convert_string():
|
|
|
|
assert_conversion(serializers.EmailField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_slug_convert_string():
|
|
|
|
assert_conversion(serializers.SlugField, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_url_convert_string():
|
|
|
|
assert_conversion(serializers.URLField, graphene.String)
|
|
|
|
|
|
|
|
|
2019-09-22 23:13:12 +03:00
|
|
|
def test_should_choice_convert_enum():
|
|
|
|
field = assert_conversion(
|
|
|
|
serializers.ChoiceField,
|
|
|
|
graphene.Enum,
|
|
|
|
choices=[("h", "Hello"), ("w", "World")],
|
|
|
|
source="word",
|
|
|
|
)
|
|
|
|
assert field._meta.enum.__members__["H"].value == "h"
|
|
|
|
assert field._meta.enum.__members__["H"].description == "Hello"
|
|
|
|
assert field._meta.enum.__members__["W"].value == "w"
|
|
|
|
assert field._meta.enum.__members__["W"].description == "World"
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
|
2020-02-07 13:17:56 +03:00
|
|
|
def test_should_choice_convert_string_if_enum_disabled():
|
|
|
|
assert_conversion(
|
|
|
|
serializers.ChoiceField,
|
|
|
|
graphene.String,
|
|
|
|
choices=[("h", "Hello"), ("w", "World")],
|
|
|
|
source="word",
|
|
|
|
convert_choices_to_enum=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-05-28 15:48:44 +03:00
|
|
|
def test_should_base_field_convert_string():
|
|
|
|
assert_conversion(serializers.Field, graphene.String)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_regex_convert_string():
|
2018-07-20 02:51:33 +03:00
|
|
|
assert_conversion(serializers.RegexField, graphene.String, regex="[0-9]+")
|
2017-05-28 15:48:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_uuid_convert_string():
|
2018-07-20 02:51:33 +03:00
|
|
|
if hasattr(serializers, "UUIDField"):
|
2017-05-28 15:48:44 +03:00
|
|
|
assert_conversion(serializers.UUIDField, graphene.String)
|
|
|
|
|
|
|
|
|
2017-07-18 16:46:36 +03:00
|
|
|
def test_should_model_convert_field():
|
|
|
|
class MyModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = None
|
2018-07-20 02:51:33 +03:00
|
|
|
fields = "__all__"
|
2017-07-18 16:46:36 +03:00
|
|
|
|
|
|
|
assert_conversion(MyModelSerializer, graphene.Field, is_input=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_date_time_convert_datetime():
|
|
|
|
assert_conversion(serializers.DateTimeField, graphene.types.datetime.DateTime)
|
|
|
|
|
|
|
|
|
2017-12-05 23:04:29 +03:00
|
|
|
def test_should_date_convert_date():
|
|
|
|
assert_conversion(serializers.DateField, graphene.types.datetime.Date)
|
2017-07-18 16:46:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_time_convert_time():
|
|
|
|
assert_conversion(serializers.TimeField, graphene.types.datetime.Time)
|
|
|
|
|
|
|
|
|
2017-05-28 15:48:44 +03:00
|
|
|
def test_should_integer_convert_int():
|
|
|
|
assert_conversion(serializers.IntegerField, graphene.Int)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_boolean_convert_boolean():
|
|
|
|
assert_conversion(serializers.BooleanField, graphene.Boolean)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_float_convert_float():
|
|
|
|
assert_conversion(serializers.FloatField, graphene.Float)
|
|
|
|
|
|
|
|
|
2022-01-18 17:03:08 +03:00
|
|
|
def test_should_decimal_convert_decimal():
|
2018-07-20 02:51:33 +03:00
|
|
|
assert_conversion(
|
2022-01-18 17:03:08 +03:00
|
|
|
serializers.DecimalField, graphene.Decimal, max_digits=4, decimal_places=2
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
2017-06-26 14:14:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_list_convert_to_list():
|
|
|
|
class StringListField(serializers.ListField):
|
|
|
|
child = serializers.CharField()
|
|
|
|
|
|
|
|
field_a = assert_conversion(
|
|
|
|
serializers.ListField,
|
|
|
|
graphene.List,
|
2018-07-20 02:51:33 +03:00
|
|
|
child=serializers.IntegerField(min_value=0, max_value=100),
|
2017-06-26 14:14:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
assert field_a.of_type == graphene.Int
|
|
|
|
|
|
|
|
field_b = assert_conversion(StringListField, graphene.List)
|
|
|
|
|
|
|
|
assert field_b.of_type == graphene.String
|
2017-06-26 16:19:55 +03:00
|
|
|
|
|
|
|
|
2018-02-13 13:48:43 +03:00
|
|
|
def test_should_list_serializer_convert_to_list():
|
|
|
|
class FooModel(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ChildSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = FooModel
|
2018-07-20 02:51:33 +03:00
|
|
|
fields = "__all__"
|
2018-02-13 13:48:43 +03:00
|
|
|
|
|
|
|
class ParentSerializer(serializers.ModelSerializer):
|
|
|
|
child = ChildSerializer(many=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = FooModel
|
2018-07-20 02:51:33 +03:00
|
|
|
fields = "__all__"
|
2018-02-13 13:48:43 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
converted_type = convert_serializer_field(
|
|
|
|
ParentSerializer().get_fields()["child"], is_input=True
|
|
|
|
)
|
2018-02-13 13:48:43 +03:00
|
|
|
assert isinstance(converted_type, graphene.List)
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
converted_type = convert_serializer_field(
|
|
|
|
ParentSerializer().get_fields()["child"], is_input=False
|
|
|
|
)
|
2018-02-13 13:48:43 +03:00
|
|
|
assert isinstance(converted_type, graphene.List)
|
|
|
|
assert converted_type.of_type is None
|
|
|
|
|
|
|
|
|
2017-06-26 16:19:55 +03:00
|
|
|
def test_should_dict_convert_dict():
|
|
|
|
assert_conversion(serializers.DictField, DictType)
|
2017-06-26 16:22:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_duration_convert_string():
|
|
|
|
assert_conversion(serializers.DurationField, graphene.String)
|
2017-06-26 16:23:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_file_convert_string():
|
|
|
|
assert_conversion(serializers.FileField, graphene.String)
|
2017-06-26 16:23:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_filepath_convert_string():
|
2019-09-22 23:13:12 +03:00
|
|
|
assert_conversion(serializers.FilePathField, graphene.Enum, path="/")
|
2017-06-26 16:24:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_ip_convert_string():
|
|
|
|
assert_conversion(serializers.IPAddressField, graphene.String)
|
2017-06-26 16:26:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_image_convert_string():
|
|
|
|
assert_conversion(serializers.ImageField, graphene.String)
|
2017-06-26 16:28:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_json_convert_jsonstring():
|
|
|
|
assert_conversion(serializers.JSONField, graphene.types.json.JSONString)
|
2017-06-26 16:32:57 +03:00
|
|
|
|
|
|
|
|
2019-09-22 23:13:12 +03:00
|
|
|
def test_should_multiplechoicefield_convert_to_list_of_enum():
|
2018-07-20 02:51:33 +03:00
|
|
|
field = assert_conversion(
|
|
|
|
serializers.MultipleChoiceField, graphene.List, choices=[1, 2, 3]
|
|
|
|
)
|
2017-06-26 16:32:57 +03:00
|
|
|
|
2019-09-22 23:13:12 +03:00
|
|
|
assert issubclass(field.of_type, graphene.Enum)
|