2020-03-13 13:04:55 +03:00
|
|
|
from textwrap import dedent
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
from django.core import management
|
2020-03-13 13:04:55 +03:00
|
|
|
from mock import mock_open, patch
|
2016-09-18 02:29:00 +03:00
|
|
|
from six import StringIO
|
|
|
|
|
2020-03-13 13:04:55 +03:00
|
|
|
from graphene import ObjectType, Schema, String
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2020-03-13 13:04:55 +03:00
|
|
|
@patch("graphene_django.management.commands.graphql_schema.Command.save_json_file")
|
|
|
|
def test_generate_json_file_on_call_graphql_schema(savefile_mock, settings):
|
2016-09-18 02:29:00 +03:00
|
|
|
out = StringIO()
|
2018-07-20 02:51:33 +03:00
|
|
|
management.call_command("graphql_schema", schema="", stdout=out)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()
|
2019-03-31 13:30:29 +03:00
|
|
|
|
|
|
|
|
2019-04-30 12:02:23 +03:00
|
|
|
@patch("json.dump")
|
2020-03-13 13:04:55 +03:00
|
|
|
def test_json_files_are_canonical(dump_mock):
|
2019-03-31 13:30:29 +03:00
|
|
|
open_mock = mock_open()
|
2019-04-30 12:02:23 +03:00
|
|
|
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
|
|
|
|
management.call_command("graphql_schema", schema="")
|
2019-03-31 13:30:29 +03:00
|
|
|
|
|
|
|
open_mock.assert_called_once()
|
|
|
|
|
|
|
|
dump_mock.assert_called_once()
|
2019-04-30 12:02:23 +03:00
|
|
|
assert dump_mock.call_args[1][
|
|
|
|
"sort_keys"
|
|
|
|
], "json.mock() should be used to sort the output"
|
|
|
|
assert (
|
|
|
|
dump_mock.call_args[1]["indent"] > 0
|
|
|
|
), "output should be pretty-printed by default"
|
2020-03-13 13:04:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|