Skip to content

Commit 1d897cb

Browse files
author
Joshua Cannon
committed
Fix enum column conversion by "instantiating" Enum
1 parent 1033de5 commit 1d897cb

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

Diff for: graphene_sqlalchemy/converter.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,24 @@ def convert_enum_to_enum(type, column, registry=None):
152152
else: # Nope, just a list of string options
153153
items = zip(type.enums, type.enums)
154154
graphene_type = Enum(type.name, items)
155-
return Field(
156-
graphene_type,
155+
return graphene_type(
157156
description=get_column_doc(column),
158157
required=not (is_column_nullable(column)),
159158
)
160159

161160

162161
@convert_sqlalchemy_type.register(ChoiceType)
163-
def convert_column_to_enum(type, column, registry=None):
164-
name = "{}_{}".format(column.table.name, column.name).upper()
165-
return Enum(name, type.choices, description=get_column_doc(column))
162+
def convert_column_to_enum(type_, column, registry=None):
163+
is_enum = isinstance(type_.choices, type)
164+
if is_enum:
165+
graphene_type = Enum.from_enum(type_.choices)
166+
else:
167+
name = "{}_{}".format(column.table.name, column.name).upper()
168+
graphene_type = Enum(name, type_.choices)
169+
return graphene_type(
170+
description=get_column_doc(column),
171+
required=not (is_column_nullable(column)),
172+
)
166173

167174

168175
@convert_sqlalchemy_type.register(ScalarListType)

Diff for: graphene_sqlalchemy/tests/test_converter.py

+26-30
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,11 @@ def test_should_unicodetext_convert_string():
8787

8888

8989
def test_should_enum_convert_enum():
90-
field = assert_column_conversion(
91-
types.Enum(enum.Enum("one", "two")), graphene.Field
90+
graphene_field = assert_column_conversion(
91+
types.Enum("one", "two", name="two_numbers"), graphene.Enum
9292
)
93-
field_type = field.type()
94-
assert isinstance(field_type, graphene.Enum)
95-
assert hasattr(field_type, "two")
96-
field = assert_column_conversion(
97-
types.Enum("one", "two", name="two_numbers"), graphene.Field
98-
)
99-
field_type = field.type()
100-
assert field_type.__class__.__name__ == "two_numbers"
101-
assert isinstance(field_type, graphene.Enum)
102-
assert hasattr(field_type, "two")
93+
assert graphene_field.type.__name__ == "two_numbers"
94+
assert hasattr(graphene_field.type, 'two')
10395

10496

10597
def test_should_small_integer_convert_int():
@@ -142,18 +134,26 @@ def test_should_label_convert_int():
142134
assert isinstance(graphene_type, graphene.Int)
143135

144136

145-
def test_should_choice_convert_enum():
137+
def test_should_choice_convert_list():
146138
TYPES = [(u"es", u"Spanish"), (u"en", u"English")]
147139
column = Column(ChoiceType(TYPES), doc="Language", name="language")
148140
Base = declarative_base()
149-
150141
Table("translatedmodel", Base.metadata, column)
151142
graphene_type = convert_sqlalchemy_column(column)
152-
assert issubclass(graphene_type, graphene.Enum)
143+
144+
assert isinstance(graphene_type, graphene.Enum)
153145
assert graphene_type._meta.name == "TRANSLATEDMODEL_LANGUAGE"
154-
assert graphene_type._meta.description == "Language"
155-
assert graphene_type._meta.enum.__members__["es"].value == "Spanish"
156-
assert graphene_type._meta.enum.__members__["en"].value == "English"
146+
assert graphene_type.Field().description == "Language"
147+
assert graphene_type.es.value == "Spanish"
148+
assert graphene_type.en.value == "English"
149+
150+
151+
def test_should_choice_convert_enum():
152+
TYPES = enum.Enum("TYPES", [(u"es", u"Spanish"), (u"en", u"English")])
153+
graphene_field = assert_column_conversion(ChoiceType(TYPES), graphene.Enum)
154+
assert graphene_field.type._meta.name == "TYPES"
155+
assert graphene_field.type.es.value == "Spanish"
156+
assert graphene_field.type.en.value == "English"
157157

158158

159159
def test_should_columproperty_convert():
@@ -270,23 +270,19 @@ def test_should_postgresql_uuid_convert():
270270

271271

272272
def test_should_postgresql_enum_convert():
273-
field = assert_column_conversion(
274-
postgresql.ENUM("one", "two", name="two_numbers"), graphene.Field
273+
graphene_field = assert_column_conversion(
274+
postgresql.ENUM("one", "two", name="two_numbers"), graphene.Enum
275275
)
276-
field_type = field.type()
277-
assert field_type.__class__.__name__ == "two_numbers"
278-
assert isinstance(field_type, graphene.Enum)
279-
assert hasattr(field_type, "two")
276+
assert graphene_field.type.__name__ == "two_numbers"
277+
assert hasattr(graphene_field.type, "two")
280278

281279

282280
def test_should_postgresql_py_enum_convert():
283-
field = assert_column_conversion(
284-
postgresql.ENUM(enum.Enum("TwoNumbers", "one two"), name="two_numbers"), graphene.Field
281+
graphene_field = assert_column_conversion(
282+
postgresql.ENUM(enum.Enum("TwoNumbers", "one two"), name="two_numbers"), graphene.Enum
285283
)
286-
field_type = field.type()
287-
assert field_type.__class__.__name__ == "TwoNumbers"
288-
assert isinstance(field_type, graphene.Enum)
289-
assert hasattr(field_type, "two")
284+
assert graphene_field.type.__name__ == "TwoNumbers"
285+
assert hasattr(graphene_field.type, "two")
290286

291287

292288
def test_should_postgresql_array_convert():

0 commit comments

Comments
 (0)