#467 Added unit test

This commit is contained in:
Michal Dvorak 2012-12-03 19:07:07 +01:00
parent 9ae0ca1cae
commit ad01fa0eae
3 changed files with 31 additions and 7 deletions

View File

@ -428,11 +428,11 @@ class ModelSerializer(Serializer):
if max_length:
kwargs['max_length'] = max_length
if model_field.verbose_name:
kwargs['label'] = model_field.verbose_name
if model_field.verbose_name is not None:
kwargs['label'] = smart_unicode(model_field.verbose_name)
if model_field.help_text:
kwargs['help_text'] = model_field.help_text
if model_field.help_text is not None:
kwargs['help_text'] = smart_unicode(model_field.help_text)
field_mapping = {
models.FloatField: FloatField,

View File

@ -1,6 +1,7 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
from django.utils.translation import ugettext_lazy as _
# from django.contrib.auth.models import Group
@ -56,7 +57,7 @@ class Anchor(RESTFrameworkModel):
class BasicModel(RESTFrameworkModel):
text = models.CharField(max_length=100)
text = models.CharField(max_length=100, verbose_name=_("Text"), help_text=_("Text description."))
class SlugBasedModel(RESTFrameworkModel):

View File

@ -1,6 +1,6 @@
import datetime
from django.test import TestCase
from rest_framework import serializers
from rest_framework import serializers, fields
from rest_framework.tests.models import (ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
ManyToManyModel, Person, ReadOnlyManyToManyModel)
@ -48,7 +48,7 @@ class BookSerializer(serializers.ModelSerializer):
class ActionItemSerializer(serializers.ModelSerializer):
class Meta:
model = ActionItem
@ -641,3 +641,26 @@ class BlankFieldTests(TestCase):
"""
serializer = self.not_blank_model_serializer_class(data=self.data)
self.assertEquals(serializer.is_valid(), False)
# Test for issue #467
class FieldLabelTest(TestCase):
def setUp(self):
class LabelModelSerializer(serializers.ModelSerializer):
# This is check that ctor supports both fields
additional = fields.CharField(label='Label', help_text='Help')
class Meta:
model = BasicModel
self.serializer_class = LabelModelSerializer
def test_label_from_model(self):
"""
Validates that label and help_text are correctly copied from the model class.
"""
serializer = self.serializer_class()
text_field = serializer.fields['text']
self.assertEquals('Text', text_field.label)
self.assertEquals('Text description.', text_field.help_text)