Skip to content

Commit 40e5252

Browse files
ulgenstnightingale
andauthored
Use the Django TestCase's Client (#1084)
* Use the Django Client test utility instance that Django provides with its TestCase class. This allows GraphQL tests to make use of the stateful client methods like login() * Add missing test case initializer call * Don't break backward compability * Add test for pending deprecation warning on GraphQLTestCase._client Co-authored-by: Tom Nightingale <[email protected]>
1 parent 8c48516 commit 40e5252

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

graphene_django/tests/test_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def runTest(self):
5151
pass
5252

5353
tc = TestClass()
54+
tc._pre_setup()
5455
tc.setUpClass()
56+
5557
tc.query("query { }", op_name="QueryName")
5658
body = json.loads(post_mock.call_args.args[1])
5759
# `operationName` field from https://graphql.org/learn/serving-over-http/#post-request

graphene_django/utils/testing.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
2+
import warnings
23

3-
from django.test import TestCase, Client
4+
from django.test import Client, TestCase
45

56
DEFAULT_GRAPHQL_URL = "/graphql/"
67

@@ -68,12 +69,6 @@ class GraphQLTestCase(TestCase):
6869
# URL to graphql endpoint
6970
GRAPHQL_URL = DEFAULT_GRAPHQL_URL
7071

71-
@classmethod
72-
def setUpClass(cls):
73-
super(GraphQLTestCase, cls).setUpClass()
74-
75-
cls._client = Client()
76-
7772
def query(self, query, op_name=None, input_data=None, variables=None, headers=None):
7873
"""
7974
Args:
@@ -99,10 +94,19 @@ def query(self, query, op_name=None, input_data=None, variables=None, headers=No
9994
input_data=input_data,
10095
variables=variables,
10196
headers=headers,
102-
client=self._client,
97+
client=self.client,
10398
graphql_url=self.GRAPHQL_URL,
10499
)
105100

101+
@property
102+
def _client(self):
103+
warnings.warn(
104+
"Using `_client` is deprecated in favour of `client`.",
105+
PendingDeprecationWarning,
106+
stacklevel=2,
107+
)
108+
return self.client
109+
106110
def assertResponseNoErrors(self, resp, msg=None):
107111
"""
108112
Assert that the call went through correctly. 200 means the syntax is ok, if there are no `errors`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
from .. import GraphQLTestCase
4+
from ...tests.test_types import with_local_registry
5+
6+
7+
@with_local_registry
8+
def test_graphql_test_case_deprecated_client():
9+
"""
10+
Test that `GraphQLTestCase._client`'s should raise pending deprecation warning.
11+
"""
12+
13+
class TestClass(GraphQLTestCase):
14+
GRAPHQL_SCHEMA = True
15+
16+
def runTest(self):
17+
pass
18+
19+
tc = TestClass()
20+
tc._pre_setup()
21+
tc.setUpClass()
22+
23+
with pytest.warns(PendingDeprecationWarning):
24+
tc._client

0 commit comments

Comments
 (0)