test and handle choices that start with an underscore, issue #141

This commit is contained in:
Jason Kraus 2019-06-05 09:52:50 -07:00
parent ae0331873e
commit 32d2808187
4 changed files with 22 additions and 1 deletions

View File

@ -31,6 +31,8 @@ singledispatch = import_single_dispatch()
def convert_choice_name(name):
name = force_text(name).encode('utf8').decode('ascii', 'ignore')
name = to_const(name)
if name.startswith('_'):
name = "A%s" % name
try:
assert_valid_name(name)
except AssertionError:

View File

@ -4,7 +4,7 @@ from __future__ import absolute_import
from django.db import models
from django.utils.translation import ugettext_lazy as _
CHOICES = ((1, u"1: this漢"), (2, _(u"2: that漢")))
CHOICES = ((1, u"1: this漢"), (2, _(u"2: that漢")), (3, "__amount__"))
class Pet(models.Model):

View File

@ -215,6 +215,24 @@ def test_field_with_choices_collision():
convert_django_field_with_choices(field)
def test_field_with_choices_underscore():
field = models.CharField(
choices=(
("__amount__", "Amount"),
("__percentage__", "Percentage"),
),
)
class UnderscoreChoicesModel(models.Model):
ourfield = field
class Meta:
app_label = "test"
graphene_type = convert_django_field_with_choices(field)
assert len(graphene_type._meta.enum.__members__) == 2
def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.Float)

View File

@ -174,6 +174,7 @@ type Reporter {
enum ReporterAChoice {
A_1_THIS
A_2_THAT
A__AMOUNT__
}
enum ReporterReporterType {