From 0d2863033847874b663fcd562dc8cd7bf0b6c79b Mon Sep 17 00:00:00 2001 From: Tom Nightingale Date: Wed, 26 Feb 2020 17:04:58 -0800 Subject: [PATCH 1/4] 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() --- graphene_django/utils/testing.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index 871c44054..1c990dc31 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -68,12 +68,6 @@ class GraphQLTestCase(TestCase): # URL to graphql endpoint GRAPHQL_URL = DEFAULT_GRAPHQL_URL - @classmethod - def setUpClass(cls): - super(GraphQLTestCase, cls).setUpClass() - - cls._client = Client() - def query(self, query, op_name=None, input_data=None, variables=None, headers=None): """ Args: @@ -99,7 +93,7 @@ def query(self, query, op_name=None, input_data=None, variables=None, headers=No input_data=input_data, variables=variables, headers=headers, - client=self._client, + client=self.client, graphql_url=self.GRAPHQL_URL, ) From d545ad44fceee6492fdf0602e33a37ed7b067068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Wed, 30 Dec 2020 06:03:19 +0300 Subject: [PATCH 2/4] Add missing test case initializer call --- graphene_django/tests/test_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graphene_django/tests/test_utils.py b/graphene_django/tests/test_utils.py index f5a8b0576..e7aa02732 100644 --- a/graphene_django/tests/test_utils.py +++ b/graphene_django/tests/test_utils.py @@ -51,7 +51,9 @@ def runTest(self): pass tc = TestClass() + tc._pre_setup() 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 From 8cbadd037ce2aeb13a9ae9de3bd56562be0f07ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Wed, 30 Dec 2020 06:13:52 +0300 Subject: [PATCH 3/4] Don't break backward compability --- graphene_django/utils/testing.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index 1c990dc31..b758ac8c7 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -1,6 +1,7 @@ import json +import warnings -from django.test import TestCase, Client +from django.test import Client, TestCase DEFAULT_GRAPHQL_URL = "/graphql/" @@ -97,6 +98,15 @@ def query(self, query, op_name=None, input_data=None, variables=None, headers=No graphql_url=self.GRAPHQL_URL, ) + @property + def _client(self): + warnings.warn( + "Using `_client` is deprecated in favour of `client`.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.client + def assertResponseNoErrors(self, resp, msg=None): """ Assert that the call went through correctly. 200 means the syntax is ok, if there are no `errors`, From 6b8a5506de82388adb056bc7cf12290fdd4234ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Clgen=20Sar=C4=B1kavak?= Date: Wed, 30 Dec 2020 06:29:49 +0300 Subject: [PATCH 4/4] Add test for pending deprecation warning on GraphQLTestCase._client --- graphene_django/utils/tests/test_testing.py | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 graphene_django/utils/tests/test_testing.py diff --git a/graphene_django/utils/tests/test_testing.py b/graphene_django/utils/tests/test_testing.py new file mode 100644 index 000000000..df7832130 --- /dev/null +++ b/graphene_django/utils/tests/test_testing.py @@ -0,0 +1,24 @@ +import pytest + +from .. import GraphQLTestCase +from ...tests.test_types import with_local_registry + + +@with_local_registry +def test_graphql_test_case_deprecated_client(): + """ + Test that `GraphQLTestCase._client`'s should raise pending deprecation warning. + """ + + class TestClass(GraphQLTestCase): + GRAPHQL_SCHEMA = True + + def runTest(self): + pass + + tc = TestClass() + tc._pre_setup() + tc.setUpClass() + + with pytest.warns(PendingDeprecationWarning): + tc._client