1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
import asyncio
import contextlib
import contextvars
import dataclasses
import inspect
import warnings
from typing import (
Any,
Optional,
Union,
cast,
)
import strawberry
from asgiref.sync import sync_to_async
from django.db import DEFAULT_DB_ALIAS, connections
from django.test.client import AsyncClient, Client
from django.test.utils import CaptureQueriesContext
from strawberry.test.client import Response
from strawberry.utils.inspect import in_async_context
from typing_extensions import override
from strawberry_django.optimizer import DjangoOptimizerExtension
from strawberry_django.test.client import TestClient
_client: contextvars.ContextVar["GraphQLTestClient"] = contextvars.ContextVar(
"_client_ctx",
)
def generate_query(query=None, mutation=None, enable_optimizer=False):
append_mutation = mutation and not query
if query is None:
@strawberry.type
class Query:
x: int
query = Query
extensions = []
if enable_optimizer:
extensions = [DjangoOptimizerExtension()]
schema = strawberry.Schema(query=query, mutation=mutation, extensions=extensions)
def process_result(result):
return result
async def query_async(query, variable_values, context_value):
result = await schema.execute(
query,
variable_values=variable_values,
context_value=context_value,
)
return process_result(result)
def query_sync(query, variable_values=None, context_value=None):
if append_mutation and not query.startswith("mutation"):
query = f"mutation {query}"
if in_async_context():
return query_async(
query,
variable_values=variable_values,
context_value=context_value,
)
result = schema.execute_sync(
query,
variable_values=variable_values,
context_value=context_value,
)
return process_result(result)
return query_sync
def dataclass(model):
def wrapper(cls):
return dataclasses.dataclass(cls)
return wrapper
def deep_tuple_to_list(data: tuple) -> list:
return_list = []
for elem in data:
if isinstance(elem, tuple):
return_list.append(deep_tuple_to_list(elem))
else:
return_list.append(elem)
return return_list
class AsyncCaptureQueriesContext:
wrapped: CaptureQueriesContext
def __init__(self, using: str):
super().__init__()
self.using = using
@sync_to_async
def wrapped_enter(self):
self.wrapped = CaptureQueriesContext(connection=connections[self.using])
return self.wrapped.__enter__() # noqa: PLC2801
def __enter__(self):
return asyncio.run(self.wrapped_enter())
def __exit__(self, exc_type, exc_value, traceback, /):
return asyncio.run(
sync_to_async(self.wrapped.__exit__)(exc_type, exc_value, traceback)
)
@contextlib.contextmanager
def assert_num_queries(n: int, *, using=DEFAULT_DB_ALIAS):
is_async = (gql_client := _client.get(None)) is not None and gql_client.is_async
if is_async:
ctx_manager = AsyncCaptureQueriesContext(using)
else:
ctx_manager = CaptureQueriesContext(connection=connections[using])
with ctx_manager as ctx:
yield ctx
executed = len(ctx)
assert executed == n, (
"{} queries executed, {} expected\nCaptured queries were:\n{}".format(
executed,
n,
"\n".join(
f"{i}. {q['sql']}" for i, q in enumerate(ctx.captured_queries, start=1)
),
)
)
class GraphQLTestClient(TestClient):
def __init__(
self,
path: str,
client: Union[Client, AsyncClient],
):
super().__init__(path, client=cast("Client", client))
self._token: Optional[contextvars.Token] = None
self.is_async = isinstance(client, AsyncClient)
def __enter__(self):
self._token = _client.set(self)
return self
def __exit__(self, *args, **kwargs):
assert self._token
_client.reset(self._token)
def request(
self,
body: dict[str, object],
headers: Optional[dict[str, object]] = None,
files: Optional[dict[str, object]] = None,
):
kwargs: dict[str, object] = {"data": body}
if files: # pragma:nocover
kwargs["format"] = "multipart"
else:
kwargs["content_type"] = "application/json"
return self.client.post(
self.path,
**kwargs, # type: ignore
)
@override
def query(
self,
query: str,
variables: Optional[dict[str, Any]] = None,
headers: Optional[dict[str, object]] = None,
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)
resp = self.request(body, headers, files)
if inspect.iscoroutine(resp):
resp = asyncio.run(resp)
data = self._decode(resp, type="multipart" if files else "json")
response = Response(
errors=data.get("errors"),
data=data.get("data"),
extensions=data.get("extensions"),
)
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
|