2016-09-18 02:29:00 +03:00
|
|
|
from django import forms
|
2022-11-14 13:56:18 +03:00
|
|
|
from pytest import raises
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
from graphene import (
|
2023-08-06 01:47:00 +03:00
|
|
|
ID,
|
|
|
|
UUID,
|
2018-07-20 02:51:33 +03:00
|
|
|
Boolean,
|
2023-08-06 01:47:00 +03:00
|
|
|
Date,
|
|
|
|
DateTime,
|
2022-01-18 17:03:08 +03:00
|
|
|
Decimal,
|
2018-07-20 02:51:33 +03:00
|
|
|
Float,
|
2023-08-06 01:47:00 +03:00
|
|
|
Int,
|
2018-07-20 02:51:33 +03:00
|
|
|
List,
|
|
|
|
NonNull,
|
2023-08-06 01:47:00 +03:00
|
|
|
String,
|
2018-07-20 02:51:33 +03:00
|
|
|
Time,
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-10-02 17:42:57 +03:00
|
|
|
from ..converter import convert_form_field
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def assert_conversion(django_field, graphene_field, *args):
|
2018-07-20 02:51:33 +03:00
|
|
|
field = django_field(*args, help_text="Custom Help Text")
|
2016-09-18 02:29:00 +03:00
|
|
|
graphene_type = convert_form_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"
|
2016-09-18 02:29:00 +03:00
|
|
|
return field
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_unknown_django_field_raise_exception():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
convert_form_field(None)
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "Don't know how to convert the Django form field" in str(excinfo.value)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-01-08 21:48:47 +03:00
|
|
|
def test_should_date_convert_date():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.DateField, Date)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-01-08 21:48:47 +03:00
|
|
|
def test_should_time_convert_time():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.TimeField, Time)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-01-08 21:48:47 +03:00
|
|
|
def test_should_date_time_convert_date_time():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.DateTimeField, DateTime)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_char_convert_string():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.CharField, String)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_email_convert_string():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.EmailField, String)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_slug_convert_string():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.SlugField, String)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_url_convert_string():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.URLField, String)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_choice_convert_string():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.ChoiceField, String)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_base_field_convert_string():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.Field, String)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_regex_convert_string():
|
2018-07-20 02:51:33 +03:00
|
|
|
assert_conversion(forms.RegexField, String, "[0-9]+")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_uuid_convert_string():
|
2018-07-20 02:51:33 +03:00
|
|
|
if hasattr(forms, "UUIDField"):
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.UUIDField, UUID)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_integer_convert_int():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.IntegerField, Int)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_boolean_convert_boolean():
|
2018-06-05 23:22:27 +03:00
|
|
|
field = assert_conversion(forms.BooleanField, Boolean)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert isinstance(field.type, NonNull)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_nullboolean_convert_boolean():
|
2018-06-05 23:22:27 +03:00
|
|
|
field = assert_conversion(forms.NullBooleanField, Boolean)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert not isinstance(field.type, NonNull)
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_float_convert_float():
|
2018-06-05 23:22:27 +03:00
|
|
|
assert_conversion(forms.FloatField, Float)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2022-01-18 17:03:08 +03:00
|
|
|
def test_should_decimal_convert_decimal():
|
|
|
|
assert_conversion(forms.DecimalField, Decimal)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2020-12-31 02:37:57 +03:00
|
|
|
def test_should_multiple_choice_convert_list():
|
|
|
|
field = forms.MultipleChoiceField()
|
|
|
|
graphene_type = convert_form_field(field)
|
|
|
|
assert isinstance(graphene_type, List)
|
|
|
|
assert graphene_type.of_type == String
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_model_multiple_choice_convert_connectionorlist():
|
2017-10-02 21:03:20 +03:00
|
|
|
field = forms.ModelMultipleChoiceField(queryset=None)
|
2016-09-18 02:29:00 +03:00
|
|
|
graphene_type = convert_form_field(field)
|
|
|
|
assert isinstance(graphene_type, List)
|
|
|
|
assert graphene_type.of_type == ID
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_manytoone_convert_connectionorlist():
|
2017-10-02 21:03:20 +03:00
|
|
|
field = forms.ModelChoiceField(queryset=None)
|
2016-09-18 02:29:00 +03:00
|
|
|
graphene_type = convert_form_field(field)
|
2018-06-05 23:22:27 +03:00
|
|
|
assert isinstance(graphene_type, ID)
|