From e1e24327b07e377125cb813f0f80f290d02c641f Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Fri, 21 Oct 2016 09:12:28 -0700 Subject: [PATCH] Improved options testing and coverage --- graphene/types/options.py | 10 +++++----- graphene/types/tests/test_options.py | 30 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 graphene/types/tests/test_options.py diff --git a/graphene/types/options.py b/graphene/types/options.py index 50e982c7..0002db68 100644 --- a/graphene/types/options.py +++ b/graphene/types/options.py @@ -19,18 +19,18 @@ class Options(object): for attr_name, value in defaults.items(): if attr_name in meta_attrs: value = meta_attrs.pop(attr_name) - elif hasattr(meta, attr_name): - value = getattr(meta, attr_name) setattr(self, attr_name, value) - # If meta_attrs is not empty, it implicit means + # If meta_attrs is not empty, it implicitly means # it received invalid attributes if meta_attrs: raise TypeError( "Invalid attributes: {}".format( - ','.join(meta_attrs.keys()) + ', '.join(sorted(meta_attrs.keys())) ) ) def __repr__(self): - return ''.format(props(self)) + options_props = props(self) + props_as_attrs = ' '.join(['{}={}'.format(key, value) for key, value in options_props.items()]) + return ''.format(props_as_attrs) diff --git a/graphene/types/tests/test_options.py b/graphene/types/tests/test_options.py new file mode 100644 index 00000000..fbcba2db --- /dev/null +++ b/graphene/types/tests/test_options.py @@ -0,0 +1,30 @@ +import pytest + +from ..options import Options + + +def test_options(): + class BaseOptions: + option_1 = False + name = True + meta = Options(BaseOptions, name=False, option_1=False) + assert meta.name == True + assert meta.option_1 == False + + +def test_options_extra_attrs(): + class BaseOptions: + name = True + type = True + + with pytest.raises(Exception) as exc_info: + meta = Options(BaseOptions) + + assert str(exc_info.value) == 'Invalid attributes: name, type' + + +def test_options_repr(): + class BaseOptions: + name = True + meta = Options(BaseOptions, name=False) + assert repr(meta) == ''