Patching private variables

This commit is contained in:
Mateusz Bocian 2017-10-02 17:33:38 -04:00
parent 2cc701f444
commit b105731ada
3 changed files with 24 additions and 3 deletions

View File

@ -65,7 +65,7 @@ def test_generate_objecttype_with_fields():
def test_generate_objecttype_with_private_attributes(): def test_generate_objecttype_with_private_attributes():
class MyObjectType(ObjectType): class MyObjectType(ObjectType):
_private_state = None _private_state = Field(MyType)
assert '_private_state' not in MyObjectType._meta.fields assert '_private_state' not in MyObjectType._meta.fields
assert hasattr(MyObjectType, '_private_state') assert hasattr(MyObjectType, '_private_state')

View File

@ -297,6 +297,27 @@ def test_query_middlewares():
assert executed.data == {'hello': 'dlroW', 'other': 'rehto'} assert executed.data == {'hello': 'dlroW', 'other': 'rehto'}
def test_query_private_values():
class MyType(ObjectType):
_private = String()
public = String()
class Query(ObjectType):
field = Field(MyType)
hello_schema = Schema(Query)
executed = hello_schema.execute('{ field { Private } }')
assert executed.errors
executed = hello_schema.execute('{ field { _private } }')
assert executed.errors
executed = hello_schema.execute('{ field { public } }')
assert not executed.errors
assert executed.data == {'field': None}
def test_objecttype_on_instances(): def test_objecttype_on_instances():
class Ship: class Ship:
@ -442,8 +463,6 @@ def test_big_list_of_containers_multiple_fields_custom_resolvers_query_benchmark
def test_query_annotated_resolvers(): def test_query_annotated_resolvers():
import json
context = Context(key="context") context = Context(key="context")
class Query(ObjectType): class Query(ObjectType):

View File

@ -28,6 +28,8 @@ def yank_fields_from_attrs(attrs, _as=None, sort=True):
''' '''
fields_with_names = [] fields_with_names = []
for attname, value in list(attrs.items()): for attname, value in list(attrs.items()):
if attname.startswith('_'):
continue
field = get_field_as(value, _as) field = get_field_as(value, _as)
if not field: if not field:
continue continue