Skip to content

Commit fecadac

Browse files
committed
added tests for form
1 parent 4dffcb5 commit fecadac

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

rest_framework/fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ def humanize_field(field):
135135
136136
"""
137137
humanized = {
138-
'type': (field.type_name if field.type_name
139-
else humanize_field_type(field.form_field_class)),
138+
'type': humanize_field_type(field.__class__),
140139
'required': getattr(field, 'required', False),
141140
'label': field.label,
142141
}
@@ -154,7 +153,8 @@ def humanize_form_fields(form):
154153
:return: A dictionary of {field_label: humanized description}
155154
156155
"""
157-
fields = SortedDict([(f.name, humanize_field(f)) for f in form.fields])
156+
fields = SortedDict([(name, humanize_field(field))
157+
for name, field in form.fields.iteritems()])
158158
return fields
159159

160160

rest_framework/tests/fields.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from __future__ import unicode_literals
55
from django.utils.datastructures import SortedDict
66
import datetime
7-
from rest_framework.fields import humanize_field, humanize_field_type
7+
from rest_framework.fields import (humanize_field, humanize_field_type,
8+
humanize_form_fields)
89
from django import forms
910
from decimal import Decimal
1011
from django.db import models
@@ -742,3 +743,23 @@ def test_optional(self):
742743
def test_label(self):
743744
for field in (self.required_field, self.optional_field):
744745
self.assertEqual(humanize_field(field)['label'], field.label)
746+
747+
748+
class Form(forms.Form):
749+
field1 = forms.CharField(max_length=3, label='field one')
750+
field2 = forms.CharField(label='field two')
751+
752+
753+
class HumanizedSerializer(TestCase):
754+
def setUp(self):
755+
self.serializer = TimestampedModelSerializer()
756+
757+
def test_humanized(self):
758+
humanized = humanize_form_fields(Form())
759+
self.assertEqual(humanized, {
760+
'field1': {
761+
u'help_text': u'', u'required': True,
762+
u'type': u'Single Character', u'label': 'field one'},
763+
'field2': {
764+
u'help_text': u'', u'required': True,
765+
u'type': u'Single Character', u'label': 'field two'}})

0 commit comments

Comments
 (0)