Merge pull request #86 from gamingexperience/fix/field-choices-collision

Avoids collisions in choices names conversion.
This commit is contained in:
Syrus Akbary 2017-01-14 18:30:07 -08:00 committed by GitHub
commit 03f3fbef22
2 changed files with 20 additions and 0 deletions

View File

@ -27,12 +27,16 @@ def convert_choice_name(name):
def get_choices(choices):
converted_names = []
for value, help_text in choices:
if isinstance(help_text, (tuple, list)):
for choice in get_choices(help_text):
yield choice
else:
name = convert_choice_name(value)
while name in converted_names:
name += '_' + str(len(converted_names))
converted_names.append(name)
description = help_text
yield name, value, description

View File

@ -176,6 +176,22 @@ def test_field_with_choices_gettext():
convert_django_field_with_choices(field)
def test_field_with_choices_collision():
field = models.CharField(help_text='Timezone', choices=(
('Etc/GMT+1+2', 'Fake choice to produce double collision'),
('Etc/GMT+1', 'Greenwich Mean Time +1'),
('Etc/GMT-1', 'Greenwich Mean Time -1'),
))
class CollisionChoicesModel(models.Model):
timezone = field
class Meta:
app_label = 'test'
convert_django_field_with_choices(field)
def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.Float)