Skip to content

Commit 6de3bbc

Browse files
committed
Small test refactor
1 parent d10895d commit 6de3bbc

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

graphene_django/rest_framework/tests/test_field_converter.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from rest_framework import serializers
23
from py.test import raises
34

@@ -6,23 +7,30 @@
67
from ..serializer_converter import convert_serializer_field
78

89

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+
1023

1124
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)
1426
assert isinstance(graphene_type, graphene_field)
1527

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)
2432

25-
return field
33+
return graphene_type
2634

2735

2836
def test_should_unknown_rest_framework_field_raise_exception():

0 commit comments

Comments
 (0)