-
-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename misleading test client argument name (#3661)
- Loading branch information
1 parent
b30f5f1
commit c25bb39
Showing
7 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Release type: minor | ||
|
||
The AIOHTTP, ASGI, and Django test clients' `asserts_errors` option has been renamed to `assert_no_errors` to better reflect its purpose. | ||
This change is backwards-compatible, but the old option name will raise a deprecation warning. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
from contextlib import asynccontextmanager | ||
from typing import TYPE_CHECKING, AsyncGenerator | ||
|
||
import pytest | ||
|
||
from tests.views.schema import schema | ||
|
||
if TYPE_CHECKING: | ||
from strawberry.test import BaseGraphQLTestClient | ||
|
||
|
||
@asynccontextmanager | ||
async def aiohttp_graphql_client() -> AsyncGenerator[BaseGraphQLTestClient]: | ||
try: | ||
from aiohttp import web | ||
from aiohttp.test_utils import TestClient, TestServer | ||
from strawberry.aiohttp.test import GraphQLTestClient | ||
from strawberry.aiohttp.views import GraphQLView | ||
except ImportError: | ||
pytest.skip("Aiohttp not installed") | ||
|
||
view = GraphQLView(schema=schema) | ||
app = web.Application() | ||
app.router.add_route("*", "/graphql/", view) | ||
|
||
async with TestClient(TestServer(app)) as client: | ||
yield GraphQLTestClient(client) | ||
|
||
|
||
@asynccontextmanager | ||
async def asgi_graphql_client() -> AsyncGenerator[BaseGraphQLTestClient]: | ||
try: | ||
from starlette.testclient import TestClient | ||
|
||
from strawberry.asgi import GraphQL | ||
from strawberry.asgi.test import GraphQLTestClient | ||
except ImportError: | ||
pytest.skip("Starlette not installed") | ||
|
||
yield GraphQLTestClient(TestClient(GraphQL(schema))) | ||
|
||
|
||
@asynccontextmanager | ||
async def django_graphql_client() -> AsyncGenerator[BaseGraphQLTestClient]: | ||
try: | ||
from django.test.client import Client | ||
|
||
from strawberry.django.test import GraphQLTestClient | ||
except ImportError: | ||
pytest.skip("Django not installed") | ||
|
||
yield GraphQLTestClient(Client()) | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
pytest.param(aiohttp_graphql_client, marks=[pytest.mark.aiohttp]), | ||
pytest.param(asgi_graphql_client, marks=[pytest.mark.asgi]), | ||
pytest.param(django_graphql_client, marks=[pytest.mark.django]), | ||
] | ||
) | ||
async def graphql_client(request) -> AsyncGenerator[BaseGraphQLTestClient]: | ||
async with request.param() as graphql_client: | ||
yield graphql_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from contextlib import nullcontext | ||
|
||
import pytest | ||
|
||
from strawberry.utils.await_maybe import await_maybe | ||
|
||
|
||
@pytest.mark.parametrize("asserts_errors", [True, False]) | ||
async def test_query_asserts_errors_option_is_deprecated( | ||
graphql_client, asserts_errors | ||
): | ||
with pytest.warns( | ||
DeprecationWarning, | ||
match="The `asserts_errors` argument has been renamed to `assert_no_errors`", | ||
): | ||
await await_maybe( | ||
graphql_client.query("{ hello }", asserts_errors=asserts_errors) | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("option_name", ["asserts_errors", "assert_no_errors"]) | ||
@pytest.mark.parametrize( | ||
("assert_no_errors", "expectation"), | ||
[(True, pytest.raises(AssertionError)), (False, nullcontext())], | ||
) | ||
async def test_query_with_assert_no_errors_option( | ||
graphql_client, option_name, assert_no_errors, expectation | ||
): | ||
query = "{ ThisIsNotAValidQuery }" | ||
|
||
with expectation: | ||
await await_maybe( | ||
graphql_client.query(query, **{option_name: assert_no_errors}) | ||
) |