Skip to content

Add tests for DjangoObjectType only/exclude_fields #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from graphene import Interface, ObjectType, Schema
from graphene.relay import Node

from ..registry import reset_global_registry
from .. import registry
from ..types import DjangoObjectType
from .models import Article as ArticleModel
from .models import Reporter as ReporterModel

reset_global_registry()
registry.reset_global_registry()


class Reporter(DjangoObjectType):
Expand Down Expand Up @@ -124,3 +124,42 @@ def test_schema_representation():
}
""".lstrip()
assert str(schema) == expected


def with_local_registry(func):
def inner(*args, **kwargs):
old = registry.get_global_registry()
registry.reset_global_registry()
try:
retval = func(*args, **kwargs)
except Exception as e:
registry.registry = old
raise e
else:
registry.registry = old
return retval
return inner


@with_local_registry
def test_django_objecttype_only_fields():
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
only_fields = ('id', 'email', 'films')


fields = list(Reporter._meta.fields.keys())
assert fields == ['id', 'email', 'films']


@with_local_registry
def test_django_objecttype_exclude_fields():
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
exclude_fields = ('email')


fields = list(Reporter._meta.fields.keys())
assert 'email' not in fields