Skip to content

Commit

Permalink
Improved List/NonNull of_type exceptions and tests. Fixed #337
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Nov 15, 2016
1 parent 78a1b18 commit 2e58f53
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion graphene/types/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ class Structure(UnmountedType):

def __init__(self, of_type, *args, **kwargs):
super(Structure, self).__init__(*args, **kwargs)
if not isinstance(of_type, Structure) and isinstance(of_type, UnmountedType):
cls_name = type(self).__name__
of_type_name = type(of_type).__name__
raise Exception("{} could not have a mounted {}() as inner type. Try with {}({}).".format(
cls_name,
of_type_name,
cls_name,
of_type_name,
))
self.of_type = of_type

def get_type(self):
Expand Down Expand Up @@ -56,7 +65,7 @@ def __init__(self, *args, **kwargs):
super(NonNull, self).__init__(*args, **kwargs)
assert not isinstance(self.of_type, NonNull), (
'Can only create NonNull of a Nullable GraphQLType but got: {}.'
).format(type)
).format(self.of_type)

def __str__(self):
return '{}!'.format(self.of_type)
Expand Down
39 changes: 39 additions & 0 deletions graphene/types/tests/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,51 @@ def test_list():
assert str(_list) == '[String]'


def test_list_with_unmounted_type():
with pytest.raises(Exception) as exc_info:
List(String())

assert str(exc_info.value) == 'List could not have a mounted String() as inner type. Try with List(String).'


def test_list_inherited_works_list():
_list = List(List(String))
assert isinstance(_list.of_type, List)
assert _list.of_type.of_type == String


def test_list_inherited_works_nonnull():
_list = List(NonNull(String))
assert isinstance(_list.of_type, NonNull)
assert _list.of_type.of_type == String


def test_nonnull():
nonnull = NonNull(String)
assert nonnull.of_type == String
assert str(nonnull) == 'String!'


def test_nonnull_inherited_works_list():
_list = NonNull(List(String))
assert isinstance(_list.of_type, List)
assert _list.of_type.of_type == String


def test_nonnull_inherited_dont_work_nonnull():
with pytest.raises(Exception) as exc_info:
NonNull(NonNull(String))

assert str(exc_info.value) == 'Can only create NonNull of a Nullable GraphQLType but got: String!.'


def test_nonnull_with_unmounted_type():
with pytest.raises(Exception) as exc_info:
NonNull(String())

assert str(exc_info.value) == 'NonNull could not have a mounted String() as inner type. Try with NonNull(String).'


def test_list_comparasion():
list1 = List(String)
list2 = List(String)
Expand Down

0 comments on commit 2e58f53

Please sign in to comment.