Skip to content

Avoids collisions in choices names conversion. #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ class Meta:
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)

Expand Down