-
-
Notifications
You must be signed in to change notification settings - Fork 660
feat: make messages if there's assert_no_errors more verbose #4423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e3fd838
dee4c77
aff54cf
c091801
55359bb
4f189f7
bc59f2f
9acada6
11071ba
1cf9eec
1a303e2
98f167e
26aa062
1786f3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| 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"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed that function and compare |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("assert_no_errors", "expectation"), | ||
|
|
@@ -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(): | ||
|
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]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
failuresis a work that triggers alex pre-commit. Maybe use another word?There was a problem hiding this comment.
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.