Skip to content

Commit

Permalink
Removed auto serialization from TestClientBase
Browse files Browse the repository at this point in the history
  • Loading branch information
dmezzogori committed Jun 1, 2023
1 parent 3e4e8f8 commit 9a8eacd
Showing 1 changed file with 22 additions and 34 deletions.
56 changes: 22 additions & 34 deletions kwik/tests/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import urllib.parse
from typing import TYPE_CHECKING, Any, Generic, Literal, Mapping, TypeVar, get_args
from typing import TYPE_CHECKING, Any, Literal, Mapping, TypeVar

from fastapi.encoders import jsonable_encoder
from fastapi.testclient import TestClient
Expand All @@ -13,16 +13,15 @@

ResponseSchema = TypeVar("ResponseSchema", bound=BaseModel)

EndpointReturn = Mapping[str, Any] | list[Mapping[str, Any]]

def assert_status_code_and_return_response(
response: Response, status_code: int = 200
) -> Mapping[str, Any] | list[Mapping[str, Any]]:
print(response.json())

def assert_status_code_and_return_response(response: Response, status_code: int = 200) -> EndpointReturn:
assert response.status_code == status_code
return response.json()


class TestClientBase(Generic[ResponseSchema]):
class TestClientBase:
"""
Base class for all test clients.
"""
Expand All @@ -32,7 +31,6 @@ class TestClientBase(Generic[ResponseSchema]):
def __init__(self, client: TestClient, headers: dict[str, str]) -> None:
self.client = client
self.headers = headers
self.response_schema = get_args(self.__orig_bases__[0])[0]

@property
def get_uri(self) -> str:
Expand Down Expand Up @@ -60,20 +58,18 @@ def make_post_data(self, **kwargs):
def make_put_data(self, **kwargs):
raise NotImplementedError

def get(self, id_: int, status_code: int = 200) -> ResponseSchema | None:
response = assert_status_code_and_return_response(
def get(self, id_: int, status_code: int = 200) -> EndpointReturn | None:
return assert_status_code_and_return_response(
self.client.get(f"{self.get_uri}/{id_}", headers=self.headers), status_code=status_code
)
if status_code == 200:
return self.response_schema(**response)

def get_multi(
self,
filters: dict[str, str] | None = None,
sorting: tuple[str, Literal["asc", "desc"]] | None = None,
skip: int | None = None,
limit: int | None = None,
) -> list[ResponseSchema]:
) -> EndpointReturn:
exclude_keys = {
"creation_time",
"last_modification_time",
Expand All @@ -94,33 +90,25 @@ def get_multi(
else:
uri = self.get_multi_uri

return [
self.response_schema(**item)
for item in assert_status_code_and_return_response(self.client.get(uri, headers=self.headers))["data"]
]
return assert_status_code_and_return_response(self.client.get(uri, headers=self.headers))

def post(self, data: BaseModel, status_code: int = 200, post_uri: str | None = None) -> ResponseSchema | None:
response = assert_status_code_and_return_response(
def post(self, data: BaseModel, status_code: int = 200, post_uri: str | None = None) -> EndpointReturn:
return assert_status_code_and_return_response(
self.client.post(f"{post_uri or self.post_uri}", json=jsonable_encoder(data), headers=self.headers),
status_code=status_code,
)
if status_code == 200:
return self.response_schema(**response)

def update(self, id_: int, data: BaseModel) -> ResponseSchema:
return self.response_schema(
**assert_status_code_and_return_response(
self.client.put(
f"{self.put_uri}/{id_}",
json=data.dict(),
headers=self.headers,
)

def update(self, id_: int, data: BaseModel, put_uri: str | None = None) -> EndpointReturn:
put_uri = put_uri or f"{self.put_uri}/{id_}"
return assert_status_code_and_return_response(
self.client.put(
put_uri,
json=jsonable_encoder(data),
headers=self.headers,
)
)

def delete(self, id_: int) -> ResponseSchema:
return self.response_schema(
**assert_status_code_and_return_response(
self.client.delete(f"{self.delete_uri}/{id_}", headers=self.headers)
)
def delete(self, id_: int) -> EndpointReturn:
return assert_status_code_and_return_response(
self.client.delete(f"{self.delete_uri}/{id_}", headers=self.headers)
)

0 comments on commit 9a8eacd

Please sign in to comment.