|
| 1 | +from rest_framework import serializers |
| 2 | +from py.test import raises |
| 3 | + |
| 4 | +import graphene |
| 5 | + |
| 6 | +from ..serializer_converter import convert_serializer_field |
| 7 | + |
| 8 | + |
| 9 | +# TODO: test required |
| 10 | + |
| 11 | +def assert_conversion(rest_framework_field, graphene_field, **kwargs): |
| 12 | + field = rest_framework_field(help_text='Custom Help Text', **kwargs) |
| 13 | + graphene_type = convert_serializer_field(field) |
| 14 | + assert isinstance(graphene_type, graphene_field) |
| 15 | + |
| 16 | + field = graphene_type.Field() |
| 17 | + assert field.description == 'Custom Help Text' |
| 18 | + assert not isinstance(field, graphene.NonNull) |
| 19 | + |
| 20 | + field = rest_framework_field(help_text='Custom Help Text', required=True, **kwargs) |
| 21 | + graphene_type = convert_serializer_field(field) |
| 22 | + field = graphene_type.Field() |
| 23 | + assert isinstance(field.type, graphene.NonNull) |
| 24 | + |
| 25 | + return field |
| 26 | + |
| 27 | + |
| 28 | +def test_should_unknown_rest_framework_field_raise_exception(): |
| 29 | + with raises(Exception) as excinfo: |
| 30 | + convert_serializer_field(None) |
| 31 | + assert 'Don\'t know how to convert the serializer field' in str(excinfo.value) |
| 32 | + |
| 33 | + |
| 34 | +def test_should_date_convert_string(): |
| 35 | + assert_conversion(serializers.DateField, graphene.String) |
| 36 | + |
| 37 | + |
| 38 | +def test_should_time_convert_string(): |
| 39 | + assert_conversion(serializers.TimeField, graphene.String) |
| 40 | + |
| 41 | + |
| 42 | +def test_should_date_time_convert_string(): |
| 43 | + assert_conversion(serializers.DateTimeField, graphene.String) |
| 44 | + |
| 45 | + |
| 46 | +def test_should_char_convert_string(): |
| 47 | + assert_conversion(serializers.CharField, graphene.String) |
| 48 | + |
| 49 | + |
| 50 | +def test_should_email_convert_string(): |
| 51 | + assert_conversion(serializers.EmailField, graphene.String) |
| 52 | + |
| 53 | + |
| 54 | +def test_should_slug_convert_string(): |
| 55 | + assert_conversion(serializers.SlugField, graphene.String) |
| 56 | + |
| 57 | + |
| 58 | +def test_should_url_convert_string(): |
| 59 | + assert_conversion(serializers.URLField, graphene.String) |
| 60 | + |
| 61 | + |
| 62 | +def test_should_choice_convert_string(): |
| 63 | + assert_conversion(serializers.ChoiceField, graphene.String, choices=[]) |
| 64 | + |
| 65 | + |
| 66 | +def test_should_base_field_convert_string(): |
| 67 | + assert_conversion(serializers.Field, graphene.String) |
| 68 | + |
| 69 | + |
| 70 | +def test_should_regex_convert_string(): |
| 71 | + assert_conversion(serializers.RegexField, graphene.String, regex='[0-9]+') |
| 72 | + |
| 73 | + |
| 74 | +def test_should_uuid_convert_string(): |
| 75 | + if hasattr(serializers, 'UUIDField'): |
| 76 | + assert_conversion(serializers.UUIDField, graphene.String) |
| 77 | + |
| 78 | + |
| 79 | +def test_should_integer_convert_int(): |
| 80 | + assert_conversion(serializers.IntegerField, graphene.Int) |
| 81 | + |
| 82 | + |
| 83 | +def test_should_boolean_convert_boolean(): |
| 84 | + assert_conversion(serializers.BooleanField, graphene.Boolean) |
| 85 | + |
| 86 | + |
| 87 | +def test_should_float_convert_float(): |
| 88 | + assert_conversion(serializers.FloatField, graphene.Float) |
| 89 | + |
| 90 | + |
| 91 | +def test_should_decimal_convert_float(): |
| 92 | + assert_conversion(serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2) |
0 commit comments