feat: make messages if there's assert_no_errors more verbose - #4423
Conversation
Reviewer's GuideMakes GraphQL test clients’ assert_no_errors failures include the underlying response.errors and adds cross-backend tests to verify the verbose assertion messages. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Thanks for adding the Below is the changelog that will be used for the release. This release adds richer verbose output to This release was contributed by @Akay7 in #4423 Additional contributors: @bellini666, @pre-commit-ci[bot], @sourcery-ai[bot], @greptile-apps[bot] |
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The helper
check_non_existent_field_errorcould use a more precise type thanAny(e.g.list[dict[str, Any]]or the concrete error type returned by the client) to make its intent clearer and catch shape changes earlier. - The repeated imports of
schemaand test client setup logic across the three new tests could be slightly DRYed up by moving shared constants/imports to module scope or using a small fixture/helper for the GraphQL clients.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The helper `check_non_existent_field_error` could use a more precise type than `Any` (e.g. `list[dict[str, Any]]` or the concrete error type returned by the client) to make its intent clearer and catch shape changes earlier.
- The repeated imports of `schema` and test client setup logic across the three new tests could be slightly DRYed up by moving shared constants/imports to module scope or using a small fixture/helper for the GraphQL clients.
## Individual Comments
### Comment 1
<location path="tests/test/test_client.py" line_range="54-55" />
<code_context>
+ 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])
</code_context>
<issue_to_address>
**suggestion (testing):** Consider adding a test for the generic GraphQLTestClient (non-framework) to mirror the new behavior
You already validate the verbose assertion message for the ASGI, Django, and aiohttp clients. Since there is also a generic `strawberry.test.client.GraphQLTestClient` used outside these frameworks, please add a corresponding test for that client so the new `assert_no_errors` behavior is covered consistently across all variants.
```suggestion
def test_graphql_test_client_assert_no_errors_verbose_message():
from strawberry.test.client import GraphQLTestClient
from tests.views.schema import schema # noqa: F401
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():
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Greptile SummaryThis PR improves the
Confidence Score: 4/5Safe to merge — the change is a two-character addition to two assert statements, with no impact on production code paths. Both source-file changes are minimal and correct. The new tests cover all three client implementations. The only blemish is a dead-code import in the Django test that is suppressed with a noqa comment rather than removed. tests/test/test_client.py — the unused schema import in the Django test function. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[client.query called] --> B[Execute HTTP request]
B --> C[Parse response into Response object]
C --> D{assert_no_errors?}
D -- "False / None" --> E[Return Response]
D -- "True (default)" --> F{response.errors is None?}
F -- "Yes" --> E
F -- "No (before PR)" --> G["AssertionError (no message)"]
F -- "No (after PR)" --> H["AssertionError(response.errors)\n→ errors list visible in output"]
Reviews (1): Last reviewed commit: "feat: make messages if there's assert_no..." | Re-trigger Greptile |
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
for more information, see https://pre-commit.ci
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
bellini666
left a comment
There was a problem hiding this comment.
Please adjust the failing tests
| 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. |
There was a problem hiding this comment.
failures is a work that triggers alex pre-commit. Maybe use another word?
There was a problem hiding this comment.
I slightly changed it, hope it works.
| 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"] |
There was a problem hiding this comment.
I'd rather assert inline and not have a function like this 😊
and I'd reduce the number of assert to the bare minimum
There was a problem hiding this comment.
I removed that function and compare error with constant right away at each place where it needed.
…testClientVerboseMessaages
…testClientVerboseMessaages
bellini666
left a comment
There was a problem hiding this comment.
Simple and effective. Thank you :)
…testClientVerboseMessaages
|
This PR was published as 0.325.0. Thank you for contributing! |
Description
At test client if there assert_no_errors(default behavior) then displayed just that assert not passed, but no messages. Those changes fix that behavior and will expose response.errors
Port of strawberry-graphql/strawberry-django#828
Types of Changes
Checklist