From 2e58f53f18050230f5947159334edf2528f78dca Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 14 Nov 2016 19:52:40 -0800 Subject: [PATCH] Improved List/NonNull of_type exceptions and tests. Fixed #337 --- graphene/types/structures.py | 11 ++++++- graphene/types/tests/test_structures.py | 39 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/graphene/types/structures.py b/graphene/types/structures.py index 1346155d..1ecfa83d 100644 --- a/graphene/types/structures.py +++ b/graphene/types/structures.py @@ -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): @@ -56,7 +65,7 @@ class NonNull(Structure): 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) diff --git a/graphene/types/tests/test_structures.py b/graphene/types/tests/test_structures.py index 9027895e..e45f09e2 100644 --- a/graphene/types/tests/test_structures.py +++ b/graphene/types/tests/test_structures.py @@ -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)