Skip to content

Commit bb8e7b0

Browse files
authored
Merge pull request #21 from prodigyeducation/async-test
Rewrite async tests without 3rd party library
2 parents 36f6f92 + 1437531 commit bb8e7b0

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
author_email="opensource@prodigygame.com",
3030
license="MIT",
3131
packages=["python_graphql_client"],
32-
install_requires=["aiohttp==3.6.2", "requests==2.22.0", "asynctest==0.13.0"],
32+
install_requires=["aiohttp==3.6.2", "requests==2.22.0"],
3333
extras_require={
3434
"dev": [
3535
"pre-commit",

tests/test_graphql_client.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
"""Tests for main graphql client module."""
22

3-
import unittest
4-
from unittest.mock import MagicMock
3+
from unittest import IsolatedAsyncioTestCase, TestCase
4+
from unittest.mock import AsyncMock, MagicMock, patch
55

66
from aiohttp import web
7-
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
8-
from asynctest import CoroutineMock, patch
97
from requests.exceptions import HTTPError
108

119
from python_graphql_client import GraphqlClient
1210

1311

14-
class TestGraphqlClientConstructor(unittest.TestCase):
12+
class TestGraphqlClientConstructor(TestCase):
1513
"""Test cases for the __init__ function in the client class."""
1614

1715
def test_init_client_no_endpoint(self):
@@ -34,7 +32,7 @@ def test_init_client_headers(self):
3432
self.assertEqual(client.headers, headers)
3533

3634

37-
class TestGraphqlClientExecute(unittest.TestCase):
35+
class TestGraphqlClientExecute(TestCase):
3836
"""Test cases for the synchronous graphql request function."""
3937

4038
@patch("python_graphql_client.graphql_client.requests")
@@ -127,26 +125,17 @@ def test_execute_query_with_operation_name(self, post_mock):
127125
)
128126

129127

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):
139129
"""Test cases for the asynchronous graphQL request function."""
140130

141131
async def get_application(self):
142132
"""Override base class method to properly use async tests."""
143133
return web.Application()
144134

145-
@unittest_run_loop
146135
@patch("aiohttp.ClientSession.post")
147136
async def test_execute_basic_query(self, mock_post):
148137
"""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()
150139
client = GraphqlClient(endpoint="http://www.test-api.com/")
151140
query = """
152141
{
@@ -162,11 +151,10 @@ async def test_execute_basic_query(self, mock_post):
162151
"http://www.test-api.com/", json={"query": query}, headers={}
163152
)
164153

165-
@unittest_run_loop
166154
@patch("aiohttp.ClientSession.post")
167155
async def test_execute_query_with_variables(self, mock_post):
168156
"""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()
170158
client = GraphqlClient(endpoint="http://www.test-api.com/")
171159
query = ""
172160
variables = {"id": 123}
@@ -179,11 +167,10 @@ async def test_execute_query_with_variables(self, mock_post):
179167
headers={},
180168
)
181169

182-
@unittest_run_loop
183170
@patch("aiohttp.ClientSession.post")
184171
async def test_execute_query_with_headers(self, mock_post):
185172
"""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()
187174
client = GraphqlClient(
188175
endpoint="http://www.test-api.com/",
189176
headers={"Content-Type": "application/json", "Existing": "123"},
@@ -202,11 +189,10 @@ async def test_execute_query_with_headers(self, mock_post):
202189
},
203190
)
204191

205-
@unittest_run_loop
206192
@patch("aiohttp.ClientSession.post")
207193
async def test_execute_query_with_operation_name(self, mock_post):
208194
"""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()
210196
client = GraphqlClient(endpoint="http://www.test-api.com/")
211197
query = """
212198
query firstQuery {
@@ -230,7 +216,3 @@ async def test_execute_query_with_operation_name(self, mock_post):
230216
json={"query": query, "operationName": operation_name},
231217
headers={},
232218
)
233-
234-
235-
if __name__ == "__main__":
236-
unittest.main()

0 commit comments

Comments
 (0)