1
1
"""Tests for main graphql client module."""
2
2
3
- import unittest
4
- from unittest .mock import MagicMock
3
+ from unittest import IsolatedAsyncioTestCase , TestCase
4
+ from unittest .mock import AsyncMock , MagicMock , patch
5
5
6
6
from aiohttp import web
7
- from aiohttp .test_utils import AioHTTPTestCase , unittest_run_loop
8
- from asynctest import CoroutineMock , patch
9
7
from requests .exceptions import HTTPError
10
8
11
9
from python_graphql_client import GraphqlClient
12
10
13
11
14
- class TestGraphqlClientConstructor (unittest . TestCase ):
12
+ class TestGraphqlClientConstructor (TestCase ):
15
13
"""Test cases for the __init__ function in the client class."""
16
14
17
15
def test_init_client_no_endpoint (self ):
@@ -34,7 +32,7 @@ def test_init_client_headers(self):
34
32
self .assertEqual (client .headers , headers )
35
33
36
34
37
- class TestGraphqlClientExecute (unittest . TestCase ):
35
+ class TestGraphqlClientExecute (TestCase ):
38
36
"""Test cases for the synchronous graphql request function."""
39
37
40
38
@patch ("python_graphql_client.graphql_client.requests" )
@@ -127,26 +125,17 @@ def test_execute_query_with_operation_name(self, post_mock):
127
125
)
128
126
129
127
130
- class AsyncMock (MagicMock ):
131
- """Utility class for mocking coroutines / async code."""
132
-
133
- async def __call__ (self , * args , ** kwargs ):
134
- """Pass arguments through in callable function."""
135
- return super ().__call__ (* args , ** kwargs )
136
-
137
-
138
- class TestGraphqlClientExecuteAsync (AioHTTPTestCase ):
128
+ class TestGraphqlClientExecuteAsync (IsolatedAsyncioTestCase ):
139
129
"""Test cases for the asynchronous graphQL request function."""
140
130
141
131
async def get_application (self ):
142
132
"""Override base class method to properly use async tests."""
143
133
return web .Application ()
144
134
145
- @unittest_run_loop
146
135
@patch ("aiohttp.ClientSession.post" )
147
136
async def test_execute_basic_query (self , mock_post ):
148
137
"""Sends a graphql POST request to an endpoint."""
149
- mock_post .return_value .__aenter__ .return_value .json = CoroutineMock ()
138
+ mock_post .return_value .__aenter__ .return_value .json = AsyncMock ()
150
139
client = GraphqlClient (endpoint = "http://www.test-api.com/" )
151
140
query = """
152
141
{
@@ -162,11 +151,10 @@ async def test_execute_basic_query(self, mock_post):
162
151
"http://www.test-api.com/" , json = {"query" : query }, headers = {}
163
152
)
164
153
165
- @unittest_run_loop
166
154
@patch ("aiohttp.ClientSession.post" )
167
155
async def test_execute_query_with_variables (self , mock_post ):
168
156
"""Sends a graphql POST request with variables."""
169
- mock_post .return_value .__aenter__ .return_value .json = CoroutineMock ()
157
+ mock_post .return_value .__aenter__ .return_value .json = AsyncMock ()
170
158
client = GraphqlClient (endpoint = "http://www.test-api.com/" )
171
159
query = ""
172
160
variables = {"id" : 123 }
@@ -179,11 +167,10 @@ async def test_execute_query_with_variables(self, mock_post):
179
167
headers = {},
180
168
)
181
169
182
- @unittest_run_loop
183
170
@patch ("aiohttp.ClientSession.post" )
184
171
async def test_execute_query_with_headers (self , mock_post ):
185
172
"""Sends a graphql POST request with headers."""
186
- mock_post .return_value .__aenter__ .return_value .json = CoroutineMock ()
173
+ mock_post .return_value .__aenter__ .return_value .json = AsyncMock ()
187
174
client = GraphqlClient (
188
175
endpoint = "http://www.test-api.com/" ,
189
176
headers = {"Content-Type" : "application/json" , "Existing" : "123" },
@@ -202,11 +189,10 @@ async def test_execute_query_with_headers(self, mock_post):
202
189
},
203
190
)
204
191
205
- @unittest_run_loop
206
192
@patch ("aiohttp.ClientSession.post" )
207
193
async def test_execute_query_with_operation_name (self , mock_post ):
208
194
"""Sends a graphql POST request with the operationName key set."""
209
- mock_post .return_value .__aenter__ .return_value .json = CoroutineMock ()
195
+ mock_post .return_value .__aenter__ .return_value .json = AsyncMock ()
210
196
client = GraphqlClient (endpoint = "http://www.test-api.com/" )
211
197
query = """
212
198
query firstQuery {
@@ -230,7 +216,3 @@ async def test_execute_query_with_operation_name(self, mock_post):
230
216
json = {"query" : query , "operationName" : operation_name },
231
217
headers = {},
232
218
)
233
-
234
-
235
- if __name__ == "__main__" :
236
- unittest .main ()
0 commit comments