mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-28 20:43:43 +03:00
9a773b9d7b
* Use ruff in pre-commit * Add pyupgrade * Add isort * Add bugbear * Fix B015 Pointless comparison * Fix B026 * B018 false positive * Remove flake8 and isort config from setup.cfg * Remove black and flake8 from dev dependencies * Update black * Show list of fixes applied with autofix on * Fix typo * Add C4 flake8-comprehensions * Add ruff to dev dependencies * Fix up
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
from io import StringIO
|
|
from textwrap import dedent
|
|
from unittest.mock import mock_open, patch
|
|
|
|
from django.core import management
|
|
|
|
from graphene import ObjectType, Schema, String
|
|
|
|
|
|
@patch("graphene_django.management.commands.graphql_schema.Command.save_json_file")
|
|
def test_generate_json_file_on_call_graphql_schema(savefile_mock):
|
|
out = StringIO()
|
|
management.call_command("graphql_schema", schema="", stdout=out)
|
|
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()
|
|
|
|
|
|
@patch("json.dump")
|
|
def test_json_files_are_canonical(dump_mock):
|
|
open_mock = mock_open()
|
|
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
|
|
management.call_command("graphql_schema", schema="")
|
|
|
|
open_mock.assert_called_once()
|
|
|
|
dump_mock.assert_called_once()
|
|
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"
|
|
|
|
|
|
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()
|
|
handle.write.assert_called_once()
|
|
|
|
schema_output = handle.write.call_args[0][0]
|
|
assert schema_output == dedent(
|
|
"""\
|
|
type Query {
|
|
hi: String
|
|
}"""
|
|
)
|