mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Added test_iter_options
This commit is contained in:
parent
4d69286efa
commit
24dec32e7a
|
@ -1192,20 +1192,25 @@ class ChoiceField(Field):
|
|||
"""
|
||||
class StartOptionGroup(object):
|
||||
start_option_group = True
|
||||
end_option_group = False
|
||||
|
||||
def __init__(self, label):
|
||||
self.label = label
|
||||
|
||||
class EndOptionGroup(object):
|
||||
start_option_group = False
|
||||
end_option_group = True
|
||||
|
||||
class Option(object):
|
||||
start_option_group = False
|
||||
end_option_group = False
|
||||
|
||||
def __init__(self, value, display_text):
|
||||
self.value = value
|
||||
self.display_text = display_text
|
||||
|
||||
for key, value in self.grouped_choices.items():
|
||||
if isinstance(value, (list, tuple)):
|
||||
if isinstance(value, dict):
|
||||
yield StartOptionGroup(label=key)
|
||||
for sub_key, sub_value in value.items():
|
||||
yield Option(value=sub_key, display_text=sub_value)
|
||||
|
|
|
@ -1107,6 +1107,31 @@ class TestChoiceField(FieldValues):
|
|||
output = field.run_validation(None)
|
||||
assert output is None
|
||||
|
||||
def test_iter_options(self):
|
||||
"""
|
||||
iter_options() should return a list of options and option groups.
|
||||
"""
|
||||
field = serializers.ChoiceField(
|
||||
choices=[
|
||||
('Numbers', ['integer', 'float']),
|
||||
('Strings', ['text', 'email', 'url'])
|
||||
]
|
||||
)
|
||||
items = list(field.iter_options())
|
||||
|
||||
assert items[0].start_option_group
|
||||
assert items[0].label == 'Numbers'
|
||||
assert items[1].value == 'integer'
|
||||
assert items[2].value == 'float'
|
||||
assert items[3].end_option_group
|
||||
|
||||
assert items[4].start_option_group
|
||||
assert items[4].label == 'Strings'
|
||||
assert items[5].value == 'text'
|
||||
assert items[6].value == 'email'
|
||||
assert items[7].value == 'url'
|
||||
assert items[8].end_option_group
|
||||
|
||||
|
||||
class TestChoiceFieldWithType(FieldValues):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user