Skip to content

Commit 5ceaae0

Browse files
committed
Test for subscribe method
1 parent 5288d1b commit 5ceaae0

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

tests/test_graphql_client.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for main graphql client module."""
22

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

66
from aiohttp import web
77
from requests.exceptions import HTTPError
@@ -216,3 +216,32 @@ async def test_execute_query_with_operation_name(self, mock_post):
216216
json={"query": query, "operationName": operation_name},
217217
headers={},
218218
)
219+
220+
221+
class TestGraphqlClientSubscriptions(IsolatedAsyncioTestCase):
222+
@patch("websockets.connect")
223+
async def test_subscribe(self, mock_connect):
224+
mock_websocket = mock_connect.return_value.__aenter__.return_value
225+
mock_websocket.send = AsyncMock()
226+
mock_websocket.__aiter__.return_value = [
227+
'{"type": "data", "id": "1", "payload": {"data": {"messageAdded": "test1"}}}',
228+
'{"type": "data", "id": "1", "payload": {"data": {"messageAdded": "test2"}}}',
229+
]
230+
231+
client = GraphqlClient(endpoint="ws://www.test-api.com/graphql")
232+
query = """
233+
subscription onMessageAdded {
234+
messageAdded
235+
}
236+
"""
237+
238+
mock_handle = MagicMock()
239+
240+
await client.subscribe(query=query, handle=mock_handle)
241+
242+
mock_handle.assert_has_calls(
243+
[
244+
call({"data": {"messageAdded": "test1"}}),
245+
call({"data": {"messageAdded": "test2"}}),
246+
]
247+
)

0 commit comments

Comments
 (0)