From 76b32ff5f44ab99312697c7ffbaa398b38f42b6a Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Fri, 1 Dec 2017 13:16:59 +0100 Subject: [PATCH] Improve test coverage --- graphene_sqlalchemy/registry.py | 24 +++++++++---------- graphene_sqlalchemy/tests/test_converter.py | 26 +++++++++++++++++++-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/graphene_sqlalchemy/registry.py b/graphene_sqlalchemy/registry.py index b37e1096..f8548634 100644 --- a/graphene_sqlalchemy/registry.py +++ b/graphene_sqlalchemy/registry.py @@ -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 diff --git a/graphene_sqlalchemy/tests/test_converter.py b/graphene_sqlalchemy/tests/test_converter.py index b48d2956..869a5b45 100644 --- a/graphene_sqlalchemy/tests/test_converter.py +++ b/graphene_sqlalchemy/tests/test_converter.py @@ -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') @@ -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():