2019-03-29 13:51:40 +03:00
|
|
|
import json
|
|
|
|
|
2019-05-09 00:45:28 +03:00
|
|
|
from django.test import TestCase, Client
|
2019-03-29 13:51:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class GraphQLTestCase(TestCase):
|
|
|
|
"""
|
|
|
|
Based on: https://www.sam.today/blog/testing-graphql-with-graphene-django/
|
|
|
|
"""
|
|
|
|
|
|
|
|
# URL to graphql endpoint
|
2019-04-30 12:02:23 +03:00
|
|
|
GRAPHQL_URL = "/graphql/"
|
2019-03-29 13:51:40 +03:00
|
|
|
# Here you need to set your graphql schema for the tests
|
|
|
|
GRAPHQL_SCHEMA = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2019-03-29 14:53:18 +03:00
|
|
|
super(GraphQLTestCase, cls).setUpClass()
|
2019-03-29 13:51:40 +03:00
|
|
|
|
|
|
|
if not cls.GRAPHQL_SCHEMA:
|
2019-04-30 12:02:23 +03:00
|
|
|
raise AttributeError(
|
|
|
|
"Variable GRAPHQL_SCHEMA not defined in GraphQLTestCase."
|
|
|
|
)
|
2019-03-29 13:51:40 +03:00
|
|
|
|
2019-06-10 02:30:48 +03:00
|
|
|
cls._client = Client()
|
2019-03-29 13:51:40 +03:00
|
|
|
|
2020-05-09 14:25:24 +03:00
|
|
|
def query(
|
|
|
|
self, query, operation_name=None, input_data=None, variables=None, headers=None
|
|
|
|
):
|
2019-03-29 13:51:40 +03:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
query (string) - GraphQL query to run
|
2020-05-09 14:25:24 +03:00
|
|
|
operation_name (string) - If the query is a mutation or named query, you must
|
2019-03-29 13:51:40 +03:00
|
|
|
supply the op_name. For annon queries ("{ ... }"),
|
|
|
|
should be None (default).
|
|
|
|
input_data (dict) - If provided, the $input variable in GraphQL will be set
|
2020-04-19 23:11:33 +03:00
|
|
|
to this value. If both ``input_data`` and ``variables``,
|
2019-09-07 21:49:29 +03:00
|
|
|
are provided, the ``input`` field in the ``variables``
|
|
|
|
dict will be overwritten with this value.
|
|
|
|
variables (dict) - If provided, the "variables" field in GraphQL will be
|
2019-12-26 14:45:18 +03:00
|
|
|
set to this value.
|
|
|
|
headers (dict) - If provided, the headers in POST request to GRAPHQL_URL
|
2020-04-19 23:11:33 +03:00
|
|
|
will be set to this value.
|
2019-03-29 13:51:40 +03:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Response object from client
|
|
|
|
"""
|
2019-04-30 12:02:23 +03:00
|
|
|
body = {"query": query}
|
2020-05-09 14:25:24 +03:00
|
|
|
if operation_name:
|
|
|
|
body["operationName"] = operation_name
|
2019-09-07 21:49:29 +03:00
|
|
|
if variables:
|
|
|
|
body["variables"] = variables
|
2019-03-29 13:51:40 +03:00
|
|
|
if input_data:
|
2019-09-07 21:49:29 +03:00
|
|
|
if variables in body:
|
|
|
|
body["variables"]["input"] = input_data
|
|
|
|
else:
|
|
|
|
body["variables"] = {"input": input_data}
|
2019-12-26 14:45:18 +03:00
|
|
|
if headers:
|
|
|
|
resp = self._client.post(
|
|
|
|
self.GRAPHQL_URL,
|
|
|
|
json.dumps(body),
|
|
|
|
content_type="application/json",
|
|
|
|
**headers
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
resp = self._client.post(
|
|
|
|
self.GRAPHQL_URL, json.dumps(body), content_type="application/json"
|
|
|
|
)
|
2019-03-29 13:51:40 +03:00
|
|
|
return resp
|
|
|
|
|
2019-03-29 14:53:18 +03:00
|
|
|
def assertResponseNoErrors(self, resp):
|
2019-03-29 13:51:40 +03:00
|
|
|
"""
|
|
|
|
Assert that the call went through correctly. 200 means the syntax is ok, if there are no `errors`,
|
|
|
|
the call was fine.
|
2019-03-29 14:53:18 +03:00
|
|
|
:resp HttpResponse: Response
|
2019-03-29 13:51:40 +03:00
|
|
|
"""
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2019-07-11 22:32:07 +03:00
|
|
|
content = json.loads(resp.content)
|
2019-04-30 12:02:23 +03:00
|
|
|
self.assertNotIn("errors", list(content.keys()))
|
2019-03-29 13:51:40 +03:00
|
|
|
|
2019-03-29 14:53:18 +03:00
|
|
|
def assertResponseHasErrors(self, resp):
|
2019-03-29 13:51:40 +03:00
|
|
|
"""
|
|
|
|
Assert that the call was failing. Take care: Even with errors, GraphQL returns status 200!
|
2019-03-29 14:53:18 +03:00
|
|
|
:resp HttpResponse: Response
|
2019-03-29 13:51:40 +03:00
|
|
|
"""
|
|
|
|
content = json.loads(resp.content)
|
2019-04-30 12:02:23 +03:00
|
|
|
self.assertIn("errors", list(content.keys()))
|