mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +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):
|
class StartOptionGroup(object):
|
||||||
start_option_group = True
|
start_option_group = True
|
||||||
|
end_option_group = False
|
||||||
|
|
||||||
def __init__(self, label):
|
def __init__(self, label):
|
||||||
self.label = label
|
self.label = label
|
||||||
|
|
||||||
class EndOptionGroup(object):
|
class EndOptionGroup(object):
|
||||||
|
start_option_group = False
|
||||||
end_option_group = True
|
end_option_group = True
|
||||||
|
|
||||||
class Option(object):
|
class Option(object):
|
||||||
|
start_option_group = False
|
||||||
|
end_option_group = False
|
||||||
|
|
||||||
def __init__(self, value, display_text):
|
def __init__(self, value, display_text):
|
||||||
self.value = value
|
self.value = value
|
||||||
self.display_text = display_text
|
self.display_text = display_text
|
||||||
|
|
||||||
for key, value in self.grouped_choices.items():
|
for key, value in self.grouped_choices.items():
|
||||||
if isinstance(value, (list, tuple)):
|
if isinstance(value, dict):
|
||||||
yield StartOptionGroup(label=key)
|
yield StartOptionGroup(label=key)
|
||||||
for sub_key, sub_value in value.items():
|
for sub_key, sub_value in value.items():
|
||||||
yield Option(value=sub_key, display_text=sub_value)
|
yield Option(value=sub_key, display_text=sub_value)
|
||||||
|
|
|
@ -1107,6 +1107,31 @@ class TestChoiceField(FieldValues):
|
||||||
output = field.run_validation(None)
|
output = field.run_validation(None)
|
||||||
assert output is 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):
|
class TestChoiceFieldWithType(FieldValues):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user