Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
release type: minor
---

Make `assert_no_errors` assertion failures report response errors for verbose output. When a test fails due to GraphQL errors, the assertion now includes the actual error details, making debugging easier.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failures is a work that triggers alex pre-commit. Maybe use another word?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I slightly changed it, hope it works.

2 changes: 1 addition & 1 deletion strawberry/aiohttp/test/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def query(

if assert_no_errors:
assert resp.status == 200
assert response.errors is None
assert response.errors is None, response.errors

return response

Expand Down
2 changes: 1 addition & 1 deletion strawberry/test/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def query(
)

if assert_no_errors:
assert response.errors is None
assert response.errors is None, response.errors

return response

Expand Down
84 changes: 84 additions & 0 deletions tests/test/test_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
"""Test that assert_no_errors includes response.errors in the AssertionError message."""

from contextlib import nullcontext
from typing import Any

import pytest

from strawberry.utils.await_maybe import await_maybe

query_to_non_existent_field = "{ nonExistentField { id } }"


def check_non_existent_field_error(errors: Any):
assert isinstance(errors, list)
assert len(errors) == 1
error = errors[0]
assert isinstance(error, dict)
assert "nonExistentField" in error["message"]
assert "Cannot query field" in error["message"]
assert error["locations"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather assert inline and not have a function like this 😊

and I'd reduce the number of assert to the bare minimum

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed that function and compare error with constant right away at each place where it needed.



@pytest.mark.parametrize(
("assert_no_errors", "expectation"),
Expand All @@ -18,3 +33,72 @@ async def test_query_with_assert_no_errors_option(
await await_maybe(
graphql_client.query(query, assert_no_errors=assert_no_errors)
)


@pytest.mark.asgi
def test_asgi_client_assert_no_errors_verbose_message():
from starlette.testclient import TestClient

from strawberry.asgi import GraphQL
from strawberry.asgi.test import GraphQLTestClient
from tests.views.schema import schema

client = GraphQLTestClient(TestClient(GraphQL(schema)))

with pytest.raises(AssertionError) as exc_info:
client.query(query_to_non_existent_field)

check_non_existent_field_error(exc_info.value.args[0])


def test_graphql_test_client_assert_no_errors_verbose_message():
from strawberry.test.client import GraphQLTestClient
from tests.views.schema import schema

client = GraphQLTestClient(schema)

with pytest.raises(AssertionError) as exc_info:
client.query(query_to_non_existent_field)

check_non_existent_field_error(exc_info.value.args[0])


@pytest.mark.django
def test_django_client_assert_no_errors_verbose_message():
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Outdated
from django.test.client import Client

from strawberry.django.test import GraphQLTestClient

client = GraphQLTestClient(Client())

with pytest.raises(AssertionError) as exc_info:
client.query(query_to_non_existent_field)

check_non_existent_field_error(exc_info.value.args[0])


@pytest.mark.aiohttp
async def test_aiohttp_client_assert_no_errors_verbose_message():
try:
from aiohttp import web
from aiohttp.test_utils import TestClient as AiohttpTestClient
from aiohttp.test_utils import TestServer

from strawberry.aiohttp.test import GraphQLTestClient
from strawberry.aiohttp.views import GraphQLView
except ImportError:
pytest.skip("Aiohttp not installed")

from tests.views.schema import schema

view = GraphQLView(schema=schema)
app = web.Application()
app.router.add_route("*", "/graphql/", view)

async with AiohttpTestClient(TestServer(app)) as client:
graphql_client = GraphQLTestClient(client)

with pytest.raises(AssertionError) as exc_info:
await graphql_client.query(query_to_non_existent_field)

check_non_existent_field_error(exc_info.value.args[0])
Loading