8000 Fix #196 - handle ChoiceType initialized with Enum by thejcannon · Pull Request #197 · graphql-python/graphene-sqlalchemy · GitHub
[go: up one dir, main page]

Skip to content

Fix #196 - handle ChoiceType initialized with Enum #197

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

Closed
wants to merge 1 commit into from
Closed
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 8000
Diff view
Diff view
7 changes: 6 additions & 1 deletion graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ def convert_enum_to_enum(type, column, registry=None):
@convert_sqlalchemy_type.register(ChoiceType)
def convert_column_to_enum(type, column, registry=None):
name = "{}_{}".format(column.table.name, column.name).upper()
return Enum(name, type.choices, description=get_column_doc(column))
enum_members = getattr(type.choices, "__members__", None)
if enum_members: # Check if an enum.Enum is used
items = [(item.name, item.value) for item in enum_members.values()]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also return Enum.from_enum(type.choices).
I don't know enough about what the expected name/description should be to know which way is more correct.

else: # Nope, just a list of items
items = type.choices
return Enum(name, items, description=get_column_doc(column))


@convert_sqlalchemy_type.register(ScalarListType)
Expand Down
16 changes: 15 additions & 1 deletion graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_should_label_convert_int():
assert isinstance(graphene_type, graphene.Int)


def test_should_choice_convert_enum():
def test_should_choice_convert_list():
TYPES = [(u"es", u"Spanish"), (u"en", u"English")]
column = Column(ChoiceType(TYPES), doc="Language", name="language")
Base = declarative_base()
Expand All @@ -156,6 +156,20 @@ def test_should_choice_convert_enum():
assert graphene_type._meta.enum.__members__["en"].value == "English"


def test_should_choice_convert_enum():
TYPES = enum.Enum("TYPES", [(u"es", u"Spanish"), (u"en", u"English")])
column = Column(ChoiceType(TYPES), doc="Language", name="language")
Base = declarative_base()

Table("translatedmodel", Base.metadata, column)
graphene_type = convert_sqlalchemy_column(column)
assert issubclass(graphene_type, graphene.Enum)
assert graphene_type._meta.name == "TRANSLATEDMODEL_LANGUAGE"
assert graphene_type._meta.description == "Language"
assert graphene_type._meta.enum.__members__["es"].value == "Spanish"
assert graphene_type._meta.enum.__members__["en"].value == "English"


def test_should_columproperty_convert():

Base = declarative_base()
Expand Down
0