From 84dbc58115f678e21ccd288d763f40fec748bd44 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sun, 3 Jan 2016 17:17:56 +0100 Subject: [PATCH] Improved Django Compatibility. Added graphql_schema command tests #73 --- .../management/commands/graphql_schema.py | 46 +++++++++++++++---- graphene/contrib/django/tests/test_command.py | 12 +++++ tests/django_settings.py | 1 + 3 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 graphene/contrib/django/tests/test_command.py diff --git a/graphene/contrib/django/management/commands/graphql_schema.py b/graphene/contrib/django/management/commands/graphql_schema.py index 57174e01..d0fec7e2 100644 --- a/graphene/contrib/django/management/commands/graphql_schema.py +++ b/graphene/contrib/django/management/commands/graphql_schema.py @@ -1,5 +1,6 @@ import importlib import json +from optparse import make_option from django.core.management.base import BaseCommand, CommandError @@ -8,6 +9,23 @@ class Command(BaseCommand): help = 'Dump Graphene schema JSON to file' can_import_settings = True + option_list = BaseCommand.option_list + ( + make_option( + '--schema', + type=str, + dest='schema', + default='', + help='Django app containing schema to dump, e.g. myproject.core.schema', + ), + make_option( + '--out', + type=str, + dest='out', + default='', + help='Output file (default: schema.json)' + ), + ) + def add_arguments(self, parser): from django.conf import settings parser.add_argument( @@ -24,15 +42,23 @@ class Command(BaseCommand): default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), help='Output file (default: schema.json)') - def handle(self, *args, **options): - schema_module = options['schema'] - if schema_module == '': - raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema') - i = importlib.import_module(schema_module) - - schema_dict = {'data': i.schema.introspect()} - - with open(options['out'], 'w') as outfile: + def save_file(self, out, schema_dict): + with open(out, 'w') as outfile: json.dump(schema_dict, outfile) - self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out'])) + def handle(self, *args, **options): + from django.conf import settings + schema = options.get('schema') or getattr(settings, 'GRAPHENE_SCHEMA', '') + out = options.get('out') or getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json') + + if schema == '': + raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema') + i = importlib.import_module(schema) + + schema_dict = {'data': i.schema.introspect()} + self.save_file(out, schema_dict) + + style = getattr(self, 'style', None) + SUCCESS = getattr(style, 'SUCCESS', lambda x: x) + + self.stdout.write(SUCCESS('Successfully dumped GraphQL schema to %s' % out)) diff --git a/graphene/contrib/django/tests/test_command.py b/graphene/contrib/django/tests/test_command.py new file mode 100644 index 00000000..d49c707a --- /dev/null +++ b/graphene/contrib/django/tests/test_command.py @@ -0,0 +1,12 @@ +from mock import patch +from six import StringIO + +from django.core import management + + +@patch('graphene.contrib.django.management.commands.graphql_schema.Command.save_file') +def test_generate_file_on_call_graphql_schema(savefile_mock, settings): + settings.GRAPHENE_SCHEMA = 'graphene.contrib.django.tests.test_urls' + out = StringIO() + management.call_command('graphql_schema', schema='', stdout=out) + assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue() diff --git a/tests/django_settings.py b/tests/django_settings.py index 998f68ab..1cf5dd38 100644 --- a/tests/django_settings.py +++ b/tests/django_settings.py @@ -1,6 +1,7 @@ SECRET_KEY = 1 INSTALLED_APPS = [ + 'graphene.contrib.django', 'graphene.contrib.django.tests', 'examples.starwars_django', ]