From 46048cd21844366e3c0b2b58e45a2fe135ae0b9a Mon Sep 17 00:00:00 2001 From: Sam Parkinson Date: Sat, 11 Mar 2017 07:40:04 +1100 Subject: [PATCH] Add tests for DjangoObjectType only/exclude_fields --- graphene_django/tests/test_types.py | 43 +++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index c617fe4..0ae12c0 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -3,12 +3,12 @@ from mock import patch 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): @@ -124,3 +124,42 @@ type RootQuery { } """.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