mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Tests and tweaks for choice fields
This commit is contained in:
parent
e5f0a97595
commit
b5454dd022
|
@ -750,7 +750,7 @@ class ChoiceField(Field):
|
||||||
self.fail('invalid_choice', input=data)
|
self.fail('invalid_choice', input=data)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
return value
|
return self.choice_strings_to_values[str(value)]
|
||||||
|
|
||||||
|
|
||||||
class MultipleChoiceField(ChoiceField):
|
class MultipleChoiceField(ChoiceField):
|
||||||
|
@ -769,7 +769,7 @@ class MultipleChoiceField(ChoiceField):
|
||||||
])
|
])
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
return value
|
return [self.choice_strings_to_values[str(item)] for item in value]
|
||||||
|
|
||||||
|
|
||||||
# File types...
|
# File types...
|
||||||
|
|
|
@ -505,9 +505,11 @@ class TestChoiceField(FieldValues):
|
||||||
'good': 'good',
|
'good': 'good',
|
||||||
}
|
}
|
||||||
invalid_inputs = {
|
invalid_inputs = {
|
||||||
'awful': ['`awful` is not a valid choice.']
|
'amazing': ['`amazing` is not a valid choice.']
|
||||||
|
}
|
||||||
|
outputs = {
|
||||||
|
'good': 'good'
|
||||||
}
|
}
|
||||||
outputs = {}
|
|
||||||
field = fields.ChoiceField(
|
field = fields.ChoiceField(
|
||||||
choices=[
|
choices=[
|
||||||
('poor', 'Poor quality'),
|
('poor', 'Poor quality'),
|
||||||
|
@ -530,7 +532,10 @@ class TestChoiceFieldWithType(FieldValues):
|
||||||
5: ['`5` is not a valid choice.'],
|
5: ['`5` is not a valid choice.'],
|
||||||
'abc': ['`abc` is not a valid choice.']
|
'abc': ['`abc` is not a valid choice.']
|
||||||
}
|
}
|
||||||
outputs = {}
|
outputs = {
|
||||||
|
'1': 1,
|
||||||
|
1: 1
|
||||||
|
}
|
||||||
field = fields.ChoiceField(
|
field = fields.ChoiceField(
|
||||||
choices=[
|
choices=[
|
||||||
(1, 'Poor quality'),
|
(1, 'Poor quality'),
|
||||||
|
@ -553,7 +558,9 @@ class TestChoiceFieldWithListChoices(FieldValues):
|
||||||
invalid_inputs = {
|
invalid_inputs = {
|
||||||
'awful': ['`awful` is not a valid choice.']
|
'awful': ['`awful` is not a valid choice.']
|
||||||
}
|
}
|
||||||
outputs = {}
|
outputs = {
|
||||||
|
'good': 'good'
|
||||||
|
}
|
||||||
field = fields.ChoiceField(choices=('poor', 'medium', 'good'))
|
field = fields.ChoiceField(choices=('poor', 'medium', 'good'))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user