Improved List/NonNull of_type exceptions and tests. Fixed #337

This commit is contained in:
Syrus Akbary 2016-11-14 19:52:40 -08:00
parent 78a1b18e44
commit 2e58f53f18
2 changed files with 49 additions and 1 deletions

View File

@ -9,6 +9,15 @@ class Structure(UnmountedType):
def __init__(self, of_type, *args, **kwargs): def __init__(self, of_type, *args, **kwargs):
super(Structure, self).__init__(*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 self.of_type = of_type
def get_type(self): def get_type(self):
@ -56,7 +65,7 @@ class NonNull(Structure):
super(NonNull, self).__init__(*args, **kwargs) super(NonNull, self).__init__(*args, **kwargs)
assert not isinstance(self.of_type, NonNull), ( assert not isinstance(self.of_type, NonNull), (
'Can only create NonNull of a Nullable GraphQLType but got: {}.' 'Can only create NonNull of a Nullable GraphQLType but got: {}.'
).format(type) ).format(self.of_type)
def __str__(self): def __str__(self):
return '{}!'.format(self.of_type) return '{}!'.format(self.of_type)

View File

@ -10,12 +10,51 @@ def test_list():
assert str(_list) == '[String]' 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(): def test_nonnull():
nonnull = NonNull(String) nonnull = NonNull(String)
assert nonnull.of_type == String assert nonnull.of_type == String
assert str(nonnull) == '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(): def test_list_comparasion():
list1 = List(String) list1 = List(String)
list2 = List(String) list2 = List(String)