Skip to content

Fix enum column conversion by "instantiating" Enum #201

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
Diff view
Diff view
17 changes: 12 additions & 5 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,24 @@ def convert_enum_to_enum(type, column, registry=None):
else: # Nope, just a list of string options
items = zip(type.enums, type.enums)
graphene_type = Enum(type.name, items)
return Field(
graphene_type,
return graphene_type(
description=get_column_doc(column),
required=not (is_column_nullable(column)),
)


@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))
def convert_column_to_enum(type_, column, registry=None):
is_enum = isinstance(type_.choices, type)
if is_enum:
graphene_type = Enum.from_enum(type_.choices)
else:
name = "{}_{}".format(column.table.name, column.name).upper()
graphene_type = Enum(name, type_.choices)
return graphene_type(
description=get_column_doc(column),
required=not (is_column_nullable(column)),
)


@convert_sqlalchemy_type.register(ScalarListType)
Expand Down
60 changes: 28 additions & 32 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,11 @@ def test_should_unicodetext_convert_string():


def test_should_enum_convert_enum():
field = assert_column_conversion(
types.Enum(enum.Enum("one", "two")), graphene.Field
graphene_field = assert_column_conversion(
types.Enum("one", "two", name="two_numbers"), graphene.Enum
)
field_type = field.type()
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, "two")
field = assert_column_conversion(
types.Enum("one", "two", name="two_numbers"), graphene.Field
)
field_type = field.type()
assert field_type.__class__.__name__ == "two_numbers"
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, "two")
assert graphene_field.type.__name__ == "two_numbers"
assert hasattr(graphene_field.type, 'two')


def test_should_small_integer_convert_int():
Expand Down Expand Up @@ -142,18 +134,26 @@ def test_should_label_convert_int():
assert isinstance(graphene_type, graphene.Int)


def test_should_choice_convert_enum():
def test_should_list_choice_convert_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 isinstance(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"
assert graphene_type.Field().description == "Language"
assert graphene_type.es.value == "Spanish"
assert graphene_type.en.value == "English"


def test_should_enum_choice_convert_enum():
TYPES = enum.Enum("TYPES", [(u"es", u"Spanish"), (u"en", u"English")])
graphene_field = assert_column_conversion(ChoiceType(TYPES), graphene.Enum)
assert graphene_field.type._meta.name == "TYPES"
assert graphene_field.type.es.value == "Spanish"
assert graphene_field.type.en.value == "English"


def test_should_columproperty_convert():
Expand Down Expand Up @@ -269,24 +269,20 @@ def test_should_postgresql_uuid_convert():
assert_column_conversion(postgresql.UUID(), graphene.String)


def test_should_postgresql_enum_convert():
field = assert_column_conversion(
postgresql.ENUM("one", "two", name="two_numbers"), graphene.Field
def test_should_postgresql_enum_convert_enum():
graphene_field = assert_column_conversion(
postgresql.ENUM("one", "two", name="two_numbers"), graphene.Enum
)
field_type = field.type()
assert field_type.__class__.__name__ == "two_numbers"
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, "two")
assert graphene_field.type.__name__ == "two_numbers"
assert hasattr(graphene_field.type, "two")


def test_should_postgresql_py_enum_convert():
field = assert_column_conversion(
postgresql.ENUM(enum.Enum("TwoNumbers", "one two"), name="two_numbers"), graphene.Field
def test_should_postgresql_py_enum_convert_enum():
graphene_field = assert_column_conversion(
postgresql.ENUM(enum.Enum("TwoNumbers", "one two"), name="two_numbers"), graphene.Enum
)
field_type = field.type()
assert field_type.__class__.__name__ == "TwoNumbers"
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, "two")
assert graphene_field.type.__name__ == "TwoNumbers"
assert hasattr(graphene_field.type, "two")


def test_should_postgresql_array_convert():
Expand Down