Skip to content

Commit

Permalink
tests for process_errors() (graphql_ws)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Jul 12, 2023
1 parent 44be3bb commit 68c898b
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions tests/websockets/test_graphql_ws.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from typing import AsyncGenerator
from unittest.mock import Mock, patch

import pytest
import pytest_asyncio
Expand All @@ -17,6 +18,7 @@
GQL_START,
GQL_STOP,
)
from tests.views.schema import Schema

from ..http.clients import AioHttpClient, HttpClient, WebSocketClient

Expand Down Expand Up @@ -195,25 +197,28 @@ async def test_subscription_cancellation(ws: WebSocketClient):


async def test_subscription_errors(ws: WebSocketClient):
await ws.send_json(
{
"type": GQL_START,
"id": "demo",
"payload": {"query": 'subscription { error(message: "TEST ERR") }'},
}
)
process_errors = Mock()
with patch.object(Schema, "process_errors", process_errors):
await ws.send_json(
{
"type": GQL_START,
"id": "demo",
"payload": {"query": 'subscription { error(message: "TEST ERR") }'},
}
)

response = await ws.receive_json()
assert response["type"] == GQL_DATA
assert response["id"] == "demo"
assert response["payload"]["data"] is None
assert len(response["payload"]["errors"]) == 1
assert response["payload"]["errors"][0]["path"] == ["error"]
assert response["payload"]["errors"][0]["message"] == "TEST ERR"
response = await ws.receive_json()
assert response["type"] == GQL_DATA
assert response["id"] == "demo"
assert response["payload"]["data"] is None
assert len(response["payload"]["errors"]) == 1
assert response["payload"]["errors"][0]["path"] == ["error"]
assert response["payload"]["errors"][0]["message"] == "TEST ERR"
process_errors.assert_called_once()

response = await ws.receive_json()
assert response["type"] == GQL_COMPLETE
assert response["id"] == "demo"
response = await ws.receive_json()
assert response["type"] == GQL_COMPLETE
assert response["id"] == "demo"


async def test_subscription_exceptions(ws: WebSocketClient):
Expand All @@ -238,23 +243,26 @@ async def test_subscription_exceptions(ws: WebSocketClient):


async def test_subscription_field_error(ws: WebSocketClient):
await ws.send_json(
{
"type": GQL_START,
"id": "invalid-field",
"payload": {"query": "subscription { notASubscriptionField }"},
}
)
process_errors = Mock()
with patch.object(Schema, "process_errors", process_errors):
await ws.send_json(
{
"type": GQL_START,
"id": "invalid-field",
"payload": {"query": "subscription { notASubscriptionField }"},
}
)

response = await ws.receive_json()
assert response["type"] == GQL_ERROR
assert response["id"] == "invalid-field"
assert response["payload"] == {
"locations": [{"line": 1, "column": 16}],
"message": (
"Cannot query field 'notASubscriptionField' on type 'Subscription'."
),
}
response = await ws.receive_json()
assert response["type"] == GQL_ERROR
assert response["id"] == "invalid-field"
assert response["payload"] == {
"locations": [{"line": 1, "column": 16}],
"message": (
"Cannot query field 'notASubscriptionField' on type 'Subscription'."
),
}
process_errors.assert_called_once()


async def test_subscription_syntax_error(ws: WebSocketClient):
Expand Down

0 comments on commit 68c898b

Please sign in to comment.