From b5454dd02290130a7fb0a0e375f3efecc58edc6d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 22 Sep 2014 16:50:04 +0100 Subject: [PATCH] Tests and tweaks for choice fields --- rest_framework/fields.py | 4 ++-- tests/test_fields.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 12975ae49..500018f3d 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -750,7 +750,7 @@ class ChoiceField(Field): self.fail('invalid_choice', input=data) def to_representation(self, value): - return value + return self.choice_strings_to_values[str(value)] class MultipleChoiceField(ChoiceField): @@ -769,7 +769,7 @@ class MultipleChoiceField(ChoiceField): ]) def to_representation(self, value): - return value + return [self.choice_strings_to_values[str(item)] for item in value] # File types... diff --git a/tests/test_fields.py b/tests/test_fields.py index 8c50aaba6..3343123f4 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -505,9 +505,11 @@ class TestChoiceField(FieldValues): 'good': 'good', } invalid_inputs = { - 'awful': ['`awful` is not a valid choice.'] + 'amazing': ['`amazing` is not a valid choice.'] + } + outputs = { + 'good': 'good' } - outputs = {} field = fields.ChoiceField( choices=[ ('poor', 'Poor quality'), @@ -530,7 +532,10 @@ class TestChoiceFieldWithType(FieldValues): 5: ['`5` is not a valid choice.'], 'abc': ['`abc` is not a valid choice.'] } - outputs = {} + outputs = { + '1': 1, + 1: 1 + } field = fields.ChoiceField( choices=[ (1, 'Poor quality'), @@ -553,7 +558,9 @@ class TestChoiceFieldWithListChoices(FieldValues): invalid_inputs = { 'awful': ['`awful` is not a valid choice.'] } - outputs = {} + outputs = { + 'good': 'good' + } field = fields.ChoiceField(choices=('poor', 'medium', 'good'))