|
| 1 | +from django import forms |
| 2 | +from py.test import raises |
| 3 | + |
| 4 | +import graphene |
| 5 | +from graphene.contrib.django.form_converter import convert_form_field |
| 6 | +from graphene.contrib.django.fields import (ConnectionOrListField, |
| 7 | + DjangoModelField) |
| 8 | + |
| 9 | +from .models import Reporter |
| 10 | + |
| 11 | + |
| 12 | +def assert_conversion(django_field, graphene_field, *args): |
| 13 | + field = django_field(*args, help_text='Custom Help Text') |
| 14 | + graphene_type = convert_form_field(field) |
| 15 | + assert isinstance(graphene_type, graphene_field) |
| 16 | + field = graphene_type.as_field() |
| 17 | + assert field.description == 'Custom Help Text' |
| 18 | + return field |
| 19 | + |
| 20 | + |
| 21 | +def test_should_unknown_django_field_raise_exception(): |
| 22 | + with raises(Exception) as excinfo: |
| 23 | + convert_form_field(None) |
| 24 | + assert 'Don\'t know how to convert the Django form field' in str(excinfo.value) |
| 25 | + |
| 26 | + |
| 27 | +def test_should_date_convert_string(): |
| 28 | + assert_conversion(forms.DateField, graphene.String) |
| 29 | + |
| 30 | + |
| 31 | +def test_should_time_convert_string(): |
| 32 | + assert_conversion(forms.TimeField, graphene.String) |
| 33 | + |
| 34 | + |
| 35 | +def test_should_date_time_convert_string(): |
| 36 | + assert_conversion(forms.DateTimeField, graphene.String) |
| 37 | + |
| 38 | + |
| 39 | +def test_should_char_convert_string(): |
| 40 | + assert_conversion(forms.CharField, graphene.String) |
| 41 | + |
| 42 | + |
| 43 | +def test_should_email_convert_string(): |
| 44 | + assert_conversion(forms.EmailField, graphene.String) |
| 45 | + |
| 46 | + |
| 47 | +def test_should_slug_convert_string(): |
| 48 | + assert_conversion(forms.SlugField, graphene.String) |
| 49 | + |
| 50 | + |
| 51 | +def test_should_url_convert_string(): |
| 52 | + assert_conversion(forms.URLField, graphene.String) |
| 53 | + |
| 54 | + |
| 55 | +def test_should_choice_convert_string(): |
| 56 | + assert_conversion(forms.ChoiceField, graphene.String) |
| 57 | + |
| 58 | + |
| 59 | +def test_should_base_field_convert_string(): |
| 60 | + assert_conversion(forms.Field, graphene.String) |
| 61 | + |
| 62 | + |
| 63 | +def test_should_regex_convert_string(): |
| 64 | + assert_conversion(forms.RegexField, graphene.String, '[0-9]+') |
| 65 | + |
| 66 | + |
| 67 | +def test_should_uuid_convert_string(): |
| 68 | + if hasattr(forms, 'UUIDField'): |
| 69 | + assert_conversion(forms.UUIDField, graphene.String) |
| 70 | + |
| 71 | + |
| 72 | +def test_should_integer_convert_int(): |
| 73 | + assert_conversion(forms.IntegerField, graphene.Int) |
| 74 | + |
| 75 | + |
| 76 | +def test_should_boolean_convert_boolean(): |
| 77 | + field = assert_conversion(forms.BooleanField, graphene.Boolean) |
| 78 | + assert field.required is True |
| 79 | + |
| 80 | + |
| 81 | +def test_should_nullboolean_convert_boolean(): |
| 82 | + field = assert_conversion(forms.NullBooleanField, graphene.Boolean) |
| 83 | + assert field.required is False |
| 84 | + |
| 85 | + |
| 86 | +def test_should_float_convert_float(): |
| 87 | + assert_conversion(forms.FloatField, graphene.Float) |
| 88 | + |
| 89 | + |
| 90 | +def test_should_decimal_convert_float(): |
| 91 | + assert_conversion(forms.DecimalField, graphene.Float) |
| 92 | + |
| 93 | + |
| 94 | +def test_should_multiple_choice_convert_connectionorlist(): |
| 95 | + field = forms.ModelMultipleChoiceField(Reporter.objects.all()) |
| 96 | + graphene_type = convert_form_field(field) |
| 97 | + assert isinstance(graphene_type, ConnectionOrListField) |
| 98 | + assert isinstance(graphene_type.type, DjangoModelField) |
| 99 | + assert graphene_type.type.model == Reporter |
| 100 | + |
| 101 | + |
| 102 | +def test_should_manytoone_convert_connectionorlist(): |
| 103 | + field = forms.ModelChoiceField(Reporter.objects.all()) |
| 104 | + graphene_type = convert_form_field(field) |
| 105 | + assert isinstance(graphene_type, graphene.ID) |
| 106 | + |
0 commit comments