Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Dec 1, 2017
1 parent baa454d commit 76b32ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
24 changes: 11 additions & 13 deletions graphene_sqlalchemy/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,19 @@ def get_type_for_enum(self, sql_type):
items = [(key.upper(), value.value)
for key, value in sql_type.enum_class.__members__.items()]
else:
name = to_type_name(sql_type.name)
if not name:
name = sql_type.name
if name:
name = to_type_name(name)
else:
name = 'Enum{}'.format(len(self._registry_enums) + 1)
items = [(key.upper(), key) for key in sql_type.enums]
if name:
gql_type = self._registry_enums.get(name)
if gql_type:
if dict(items) != {
key: value.value for key, value
in gql_type._meta.enum.__members__.items()}:
raise TypeError(
'Different enums with the same name {}'.format(name))
else:
name = 'Enum{}'.format(len(self._registry_enums) + 1)
gql_type = None
gql_type = self._registry_enums.get(name)
if gql_type:
if dict(items) != {
key: value.value for key, value
in gql_type._meta.enum.__members__.items()}:
raise TypeError(
'Different enums with the same name {}'.format(name))
if not gql_type:
gql_type = Enum(name, items)
self._registry_enums[name] = gql_type
Expand Down
26 changes: 24 additions & 2 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def test_should_unicodetext_convert_string():

def test_should_enum_convert_enum():
field = assert_column_conversion(
types.Enum(enum.Enum('TwoNumbersPyEnum', 'one two')), graphene.Field)
types.Enum('one', 'two'), graphene.Field)
field_type = field.type()
assert field_type.__class__.__name__ == 'TwoNumbersPyEnum'
assert field_type.__class__.__name__.startswith('Enum')
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, 'ONE')
assert not hasattr(field_type, 'one')
Expand All @@ -96,6 +96,28 @@ def test_should_enum_convert_enum():
assert hasattr(field_type, 'ONE')
assert not hasattr(field_type, 'one')
assert hasattr(field_type, 'TWO')
field = assert_column_conversion(
types.Enum(enum.Enum('TwoNumbersPyEnum', 'one two')), graphene.Field)
field_type = field.type()
assert field_type.__class__.__name__ == 'TwoNumbersPyEnum'
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, 'ONE')
assert not hasattr(field_type, 'one')
assert hasattr(field_type, 'TWO')


def test_should_conflicting_enum_raise_error():
some_type = types.Enum(enum.Enum('ConflictingEnum', 'cat cow'))
field = assert_column_conversion(some_type, graphene.Field)
field_type = field.type()
assert isinstance(field_type, graphene.Enum)
assert hasattr(field_type, 'COW')
same_type = types.Enum(enum.Enum('ConflictingEnum', 'cat cow'))
field = assert_column_conversion(same_type, graphene.Field)
assert field_type == field.type()
conflicting_type = types.Enum(enum.Enum('ConflictingEnum', 'cat horse'))
with raises(TypeError):
assert_column_conversion(conflicting_type, graphene.Field)


def test_should_small_integer_convert_int():
Expand Down

0 comments on commit 76b32ff

Please sign in to comment.