|
1 | 1 | """Tests for main graphql client module.""" |
2 | 2 |
|
3 | 3 | from unittest import IsolatedAsyncioTestCase, TestCase |
4 | | -from unittest.mock import AsyncMock, MagicMock, patch |
| 4 | +from unittest.mock import AsyncMock, MagicMock, call, patch |
5 | 5 |
|
6 | 6 | from aiohttp import web |
7 | 7 | from requests.exceptions import HTTPError |
@@ -216,3 +216,32 @@ async def test_execute_query_with_operation_name(self, mock_post): |
216 | 216 | json={"query": query, "operationName": operation_name}, |
217 | 217 | headers={}, |
218 | 218 | ) |
| 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