|
| 1 | +import copy |
1 | 2 | from rest_framework import serializers
|
2 | 3 | from py.test import raises
|
3 | 4 |
|
|
6 | 7 | from ..serializer_converter import convert_serializer_field
|
7 | 8 |
|
8 | 9 |
|
9 |
| -# TODO: test required |
| 10 | +def _get_type(rest_framework_field, **kwargs): |
| 11 | + # prevents the following error: |
| 12 | + # AssertionError: The `source` argument is not meaningful when applied to a `child=` field. |
| 13 | + # Remove `source=` from the field declaration. |
| 14 | + # since we are reusing the same child in when testing the required attribute |
| 15 | + |
| 16 | + if 'child' in kwargs: |
| 17 | + kwargs['child'] = copy.deepcopy(kwargs['child']) |
| 18 | + |
| 19 | + field = rest_framework_field(**kwargs) |
| 20 | + |
| 21 | + return convert_serializer_field(field) |
| 22 | + |
10 | 23 |
|
11 | 24 | 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) |
| 25 | + graphene_type = _get_type(rest_framework_field, help_text='Custom Help Text', **kwargs) |
14 | 26 | assert isinstance(graphene_type, graphene_field)
|
15 | 27 |
|
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) |
| 28 | + graphene_type_required = _get_type( |
| 29 | + rest_framework_field, help_text='Custom Help Text', required=True, **kwargs |
| 30 | + ) |
| 31 | + assert isinstance(graphene_type_required, graphene_field) |
24 | 32 |
|
25 |
| - return field |
| 33 | + return graphene_type |
26 | 34 |
|
27 | 35 |
|
28 | 36 | def test_should_unknown_rest_framework_field_raise_exception():
|
|
0 commit comments