From 53f107ecc5b12346ddc4de4cf14e0be1906690e6 Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Tue, 2 Apr 2019 10:08:04 -0500 Subject: [PATCH] Fix enum column conversion by "instantiating" Enum --- graphene_sqlalchemy/converter.py | 17 ++++-- graphene_sqlalchemy/tests/test_converter.py | 60 ++++++++++----------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/graphene_sqlalchemy/converter.py b/graphene_sqlalchemy/converter.py index 72089613..ec83bfb6 100644 --- a/graphene_sqlalchemy/converter.py +++ b/graphene_sqlalchemy/converter.py @@ -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) diff --git a/graphene_sqlalchemy/tests/test_converter.py b/graphene_sqlalchemy/tests/test_converter.py index c6c49bba..d6b9dee2 100644 --- a/graphene_sqlalchemy/tests/test_converter.py +++ b/graphene_sqlalchemy/tests/test_converter.py @@ -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(): @@ -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(): @@ -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():