mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-06-18 20:43:22 +03:00
Add test for exporting graphql file
This commit is contained in:
parent
40f329cae0
commit
033c512cf5
|
@ -1,17 +1,21 @@
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from mock import patch, mock_open
|
from mock import mock_open, patch
|
||||||
from six import StringIO
|
from six import StringIO
|
||||||
|
|
||||||
|
from graphene import ObjectType, Schema, String
|
||||||
|
|
||||||
|
|
||||||
@patch("graphene_django.management.commands.graphql_schema.Command.save_json_file")
|
@patch("graphene_django.management.commands.graphql_schema.Command.save_json_file")
|
||||||
def test_generate_file_on_call_graphql_schema(savefile_mock, settings):
|
def test_generate_json_file_on_call_graphql_schema(savefile_mock, settings):
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
management.call_command("graphql_schema", schema="", stdout=out)
|
management.call_command("graphql_schema", schema="", stdout=out)
|
||||||
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()
|
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()
|
||||||
|
|
||||||
|
|
||||||
@patch("json.dump")
|
@patch("json.dump")
|
||||||
def test_files_are_canonical(dump_mock):
|
def test_json_files_are_canonical(dump_mock):
|
||||||
open_mock = mock_open()
|
open_mock = mock_open()
|
||||||
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
|
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
|
||||||
management.call_command("graphql_schema", schema="")
|
management.call_command("graphql_schema", schema="")
|
||||||
|
@ -25,3 +29,34 @@ def test_files_are_canonical(dump_mock):
|
||||||
assert (
|
assert (
|
||||||
dump_mock.call_args[1]["indent"] > 0
|
dump_mock.call_args[1]["indent"] > 0
|
||||||
), "output should be pretty-printed by default"
|
), "output should be pretty-printed by default"
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_graphql_file_on_call_graphql_schema():
|
||||||
|
class Query(ObjectType):
|
||||||
|
hi = String()
|
||||||
|
|
||||||
|
mock_schema = Schema(query=Query)
|
||||||
|
|
||||||
|
open_mock = mock_open()
|
||||||
|
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
|
||||||
|
management.call_command(
|
||||||
|
"graphql_schema", schema=mock_schema, out="schema.graphql"
|
||||||
|
)
|
||||||
|
|
||||||
|
open_mock.assert_called_once()
|
||||||
|
|
||||||
|
handle = open_mock()
|
||||||
|
assert handle.write.called_once()
|
||||||
|
|
||||||
|
schema_output = handle.write.call_args[0][0]
|
||||||
|
assert schema_output == dedent(
|
||||||
|
"""\
|
||||||
|
schema {
|
||||||
|
query: Query
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
hi: String
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user