Dict or attr default resolver (#638)

* Add dict_or_attr resolver and set it as default

* Add some tests

* Dry up code

* Updated formatting
This commit is contained in:
Jonathan Kim 2019-03-11 18:24:11 +01:00 committed by Eran Kampf
parent bbe11c9b5e
commit ca9188a615
2 changed files with 18 additions and 2 deletions

View File

@ -6,7 +6,14 @@ def dict_resolver(attname, default_value, root, info, **args):
return root.get(attname, default_value)
default_resolver = attr_resolver
def dict_or_attr_resolver(attname, default_value, root, info, **args):
resolver = attr_resolver
if isinstance(root, dict):
resolver = dict_resolver
return resolver(attname, default_value, root, info, **args)
default_resolver = dict_or_attr_resolver
def set_default_resolver(resolver):

View File

@ -1,6 +1,7 @@
from ..resolver import (
attr_resolver,
dict_resolver,
dict_or_attr_resolver,
get_default_resolver,
set_default_resolver,
)
@ -36,8 +37,16 @@ def test_dict_resolver_default_value():
assert resolved == "default"
def test_dict_or_attr_resolver():
resolved = dict_or_attr_resolver("attr", None, demo_dict, info, **args)
assert resolved == "value"
resolved = dict_or_attr_resolver("attr", None, demo_obj, info, **args)
assert resolved == "value"
def test_get_default_resolver_is_attr_resolver():
assert get_default_resolver() == attr_resolver
assert get_default_resolver() == dict_or_attr_resolver
def test_set_default_resolver_workd():