Add specific tests for name generation

This commit is contained in:
Jonathan Kim 2020-01-11 17:17:51 +00:00
parent edbb14e656
commit b4b64fd6f4
2 changed files with 35 additions and 8 deletions

View File

@ -69,16 +69,15 @@ def convert_choices_to_named_enum_with_descriptions(name, choices):
return Enum(name, list(named_choices), type=EnumWithDescriptionsType)
def generate_enum_name(django_model, field):
meta = django_model._meta
def generate_enum_name(django_model_meta, field):
if graphene_settings.CHOICES_TO_ENUM_UNIQUE_TYPE_NAME is True:
name = "DjangoModel{app_label}{object_name}{field_name}Choices".format(
app_label=to_camel_case(meta.app_label).capitalize(),
object_name=meta.object_name,
field_name=to_camel_case(field.name).capitalize(),
app_label=to_camel_case(django_model_meta.app_label.title()),
object_name=django_model_meta.object_name,
field_name=to_camel_case(field.name.title()),
)
else:
name = to_camel_case("{}_{}".format(meta.object_name, field.name))
name = to_camel_case("{}_{}".format(django_model_meta.object_name, field.name))
return name
@ -91,7 +90,7 @@ def convert_django_field_with_choices(
return converted
choices = getattr(field, "choices", None)
if choices and convert_choices_to_enum:
name = generate_enum_name(field.model, field)
name = generate_enum_name(field.model._meta, field)
enum = convert_choices_to_named_enum_with_descriptions(name, choices)
required = not (field.blank or field.null)
converted = enum(description=field.help_text, required=required)

View File

@ -1,4 +1,5 @@
import pytest
from collections import namedtuple
from django.db import models
from django.utils.translation import ugettext_lazy as _
from graphene import NonNull
@ -10,9 +11,14 @@ from graphene.types.datetime import DateTime, Date, Time
from graphene.types.json import JSONString
from ..compat import JSONField, ArrayField, HStoreField, RangeField, MissingType
from ..converter import convert_django_field, convert_django_field_with_choices
from ..converter import (
convert_django_field,
convert_django_field_with_choices,
generate_enum_name,
)
from ..registry import Registry
from ..types import DjangoObjectType
from ..settings import graphene_settings
from .models import Article, Film, FilmDetails, Reporter
@ -325,3 +331,25 @@ def test_should_postgres_range_convert_list():
assert isinstance(field.type, graphene.NonNull)
assert isinstance(field.type.of_type, graphene.List)
assert field.type.of_type.of_type == graphene.Int
def test_generate_enum_name():
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
graphene_settings.CHOICES_TO_ENUM_UNIQUE_TYPE_NAME = True
# Simple case
field = graphene.Field(graphene.String, name="type")
model_meta = MockDjangoModelMeta(app_label="users", object_name="User")
assert generate_enum_name(model_meta, field) == "DjangoModelUsersUserTypeChoices"
# More complicated multiple work case
field = graphene.Field(graphene.String, name="fizz_buzz")
model_meta = MockDjangoModelMeta(
app_label="some_long_app_name", object_name="SomeObject"
)
assert (
generate_enum_name(model_meta, field)
== "DjangoModelSomeLongAppNameSomeObjectFizzBuzzChoices"
)
graphene_settings.CHOICES_TO_ENUM_UNIQUE_TYPE_NAME = False