Switch operation_name to operationName in GraphQLTestCase (#936)

* Add op_name test

* Replace "operation_name" with "operationName"

* Improve test comments

* Add method for Python 2.7
This commit is contained in:
Noelle Leigh 2020-04-19 16:11:33 -04:00 committed by GitHub
parent 23b6419b42
commit dc5c971498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -1,6 +1,10 @@
from django.utils.translation import gettext_lazy import json
from ..utils import camelize, get_model_fields import pytest
from django.utils.translation import gettext_lazy
from mock import patch
from ..utils import camelize, get_model_fields, GraphQLTestCase
from .models import Film, Reporter from .models import Film, Reporter
@ -30,3 +34,27 @@ def test_camelize():
"valueA": "value_b" "valueA": "value_b"
} }
assert camelize({0: {"field_a": ["errors"]}}) == {0: {"fieldA": ["errors"]}} assert camelize({0: {"field_a": ["errors"]}}) == {0: {"fieldA": ["errors"]}}
@pytest.mark.django_db
@patch("graphene_django.utils.testing.Client.post")
def test_graphql_test_case_op_name(post_mock):
"""
Test that `GraphQLTestCase.query()`'s `op_name` argument produces an `operationName` field.
"""
class TestClass(GraphQLTestCase):
GRAPHQL_SCHEMA = True
def runTest(self):
pass
tc = TestClass()
tc.setUpClass()
tc.query("query { }", op_name="QueryName")
body = json.loads(post_mock.call_args.args[1])
# `operationName` field from https://graphql.org/learn/serving-over-http/#post-request
assert (
"operationName",
"QueryName",
) in body.items(), "Field 'operationName' is not present in the final request."

View File

@ -32,20 +32,20 @@ class GraphQLTestCase(TestCase):
supply the op_name. For annon queries ("{ ... }"), supply the op_name. For annon queries ("{ ... }"),
should be None (default). should be None (default).
input_data (dict) - If provided, the $input variable in GraphQL will be set input_data (dict) - If provided, the $input variable in GraphQL will be set
to this value. If both ``input_data`` and ``variables``, to this value. If both ``input_data`` and ``variables``,
are provided, the ``input`` field in the ``variables`` are provided, the ``input`` field in the ``variables``
dict will be overwritten with this value. dict will be overwritten with this value.
variables (dict) - If provided, the "variables" field in GraphQL will be variables (dict) - If provided, the "variables" field in GraphQL will be
set to this value. set to this value.
headers (dict) - If provided, the headers in POST request to GRAPHQL_URL headers (dict) - If provided, the headers in POST request to GRAPHQL_URL
will be set to this value. will be set to this value.
Returns: Returns:
Response object from client Response object from client
""" """
body = {"query": query} body = {"query": query}
if op_name: if op_name:
body["operation_name"] = op_name body["operationName"] = op_name
if variables: if variables:
body["variables"] = variables body["variables"] = variables
if input_data: if input_data: