Skip to content

Commit

Permalink
Rename misleading test client argument name (#3661)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorJohn authored Oct 7, 2024
1 parent b30f5f1 commit c25bb39
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 11 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
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.
18 changes: 16 additions & 2 deletions strawberry/aiohttp/test/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from typing import (
Any,
Dict,
Expand All @@ -16,8 +17,9 @@ async def query(
query: str,
variables: Optional[Dict[str, Mapping]] = None,
headers: Optional[Dict[str, object]] = None,
asserts_errors: Optional[bool] = True,
asserts_errors: Optional[bool] = None,
files: Optional[Dict[str, object]] = None,
assert_no_errors: Optional[bool] = True,
) -> Response:
body = self._build_body(query, variables, files)

Expand All @@ -29,7 +31,19 @@ async def query(
data=data.get("data"),
extensions=data.get("extensions"),
)
if asserts_errors:

if asserts_errors is not None:
warnings.warn(
"The `asserts_errors` argument has been renamed to `assert_no_errors`",
DeprecationWarning,
stacklevel=2,
)

assert_no_errors = (
assert_no_errors if asserts_errors is None else asserts_errors
)

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

Expand Down
18 changes: 16 additions & 2 deletions strawberry/test/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import warnings
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Coroutine, Dict, List, Mapping, Optional, Union
Expand Down Expand Up @@ -36,8 +37,9 @@ def query(
query: str,
variables: Optional[Dict[str, Mapping]] = None,
headers: Optional[Dict[str, object]] = None,
asserts_errors: Optional[bool] = True,
asserts_errors: Optional[bool] = None,
files: Optional[Dict[str, object]] = None,
assert_no_errors: Optional[bool] = True,
) -> Union[Coroutine[Any, Any, Response], Response]:
body = self._build_body(query, variables, files)

Expand All @@ -49,7 +51,19 @@ def query(
data=data.get("data"),
extensions=data.get("extensions"),
)
if asserts_errors:

if asserts_errors is not None:
warnings.warn(
"The `asserts_errors` argument has been renamed to `assert_no_errors`",
DeprecationWarning,
stacklevel=2,
)

assert_no_errors = (
assert_no_errors if asserts_errors is None else asserts_errors
)

if assert_no_errors:
assert response.errors is None

return response
Expand Down
7 changes: 0 additions & 7 deletions tests/django/test_graphql_test_client.py

This file was deleted.

Empty file added tests/test/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions tests/test/conftest.py
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
34 changes: 34 additions & 0 deletions tests/test/test_client.py
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})
)

0 comments on commit c25bb39

Please sign in to comment.