support blank fields with choices

This commit is contained in:
melvanlonden@canvasmedical.com 2019-03-20 21:20:10 -06:00
parent ea2cd9894f
commit 98de592303
4 changed files with 25 additions and 1 deletions

4
.gitignore vendored
View File

@ -78,3 +78,7 @@ Session.vim
*~ *~
# auto-generated tag files # auto-generated tag files
tags tags
# Pipenv
Pipfile
Pipfile.lock

View File

@ -62,6 +62,8 @@ def convert_django_field_with_choices(field, registry=None):
meta = field.model._meta meta = field.model._meta
name = to_camel_case("{}_{}".format(meta.object_name, field.name)) name = to_camel_case("{}_{}".format(meta.object_name, field.name))
choices = list(get_choices(choices)) choices = list(get_choices(choices))
if field.blank:
choices.append(('EMPTY', '', ''))
named_choices = [(c[0], c[1]) for c in choices] named_choices = [(c[0], c[1]) for c in choices]
named_choices_descriptions = {c[0]: c[2] for c in choices} named_choices_descriptions = {c[0]: c[2] for c in choices}

View File

@ -196,6 +196,22 @@ def test_field_with_choices_collision():
convert_django_field_with_choices(field) convert_django_field_with_choices(field)
def test_field_with_blank():
field = models.CharField(
help_text="Language", choices=(("es", "Spanish"), ("en", "English")), blank=True
)
class TranslatedModel(models.Model):
language = field
class Meta:
app_label = "test"
graphene_type = convert_django_field_with_choices(field)
assert graphene_type._meta.enum.__members__["EMPTY"].value == ""
assert graphene_type._meta.enum.__members__["EMPTY"].description == ""
def test_should_float_convert_float(): def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.Float) assert_conversion(models.FloatField, graphene.Float)
@ -241,7 +257,7 @@ def test_should_manytoone_convert_connectionorlist():
class Meta: class Meta:
model = Article model = Article
graphene_field = convert_django_field(Reporter.articles.rel, graphene_field = convert_django_field(Reporter.articles.rel,
A._meta.registry) A._meta.registry)
assert isinstance(graphene_field, graphene.Dynamic) assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type() dynamic_field = graphene_field.get_type()

View File

@ -138,6 +138,7 @@ type ArticleEdge {
enum ArticleImportance { enum ArticleImportance {
A_1 A_1
A_2 A_2
EMPTY
} }
enum ArticleLang { enum ArticleLang {
@ -179,6 +180,7 @@ enum ReporterAChoice {
enum ReporterReporterType { enum ReporterReporterType {
A_1 A_1
A_2 A_2
EMPTY
} }
type RootQuery { type RootQuery {