Skip to content

Commit 02ef2a5

Browse files
ovsdscatsona
authored andcommitted
test: BI-6602 add TestingHttpxClient (#1253)
* test: BI-6602 add TestingHttpxClient * fix: drop redundant contexts * fix: naming
1 parent 1200c22 commit 02ef2a5

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

lib/dl_httpx/dl_httpx/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TypedSchemaDictAnnotation,
1818
TypedSchemaListAnnotation,
1919
)
20+
from .testing import TestingHttpxClient
2021

2122

2223
__all__ = [
@@ -35,4 +36,5 @@
3536
"HttpStatusHttpxClientException",
3637
"RequestHttpxClientException",
3738
"NoRetriesHttpxClientException",
39+
"TestingHttpxClient",
3840
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pathlib
2+
3+
import dl_testing
4+
5+
6+
dl_testing.register_all_assert_rewrites(__name__, pathlib.Path(__file__).parent)
7+
8+
from .client import TestingHttpxClient
9+
10+
11+
__all__ = [
12+
"TestingHttpxClient",
13+
]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import asyncio
2+
from typing import (
3+
Any,
4+
Awaitable,
5+
Callable,
6+
)
7+
8+
import attrs
9+
10+
11+
@attrs.define(kw_only=True, auto_attribs=True, frozen=True)
12+
class TestingHttpxClient:
13+
sync_client: Any
14+
async_client: Any
15+
16+
def _sync_method_wrapper(
17+
self,
18+
sync_client_method: Callable[..., Any],
19+
async_client_method: Callable[..., Any],
20+
) -> Callable[..., Any]:
21+
def wrapper(*args: Any, **kwargs: Any) -> Any:
22+
sync_response = sync_client_method(*args, **kwargs)
23+
async_response = async_client_method(*args, **kwargs)
24+
25+
assert sync_response == async_response
26+
27+
return sync_response
28+
29+
return wrapper
30+
31+
def _async_method_wrapper(
32+
self,
33+
sync_client_method: Callable[..., Any],
34+
async_client_method: Callable[..., Awaitable[Any]],
35+
) -> Callable[..., Awaitable[Any]]:
36+
async def wrapper(*args: Any, **kwargs: Any) -> Any:
37+
async_response = await async_client_method(*args, **kwargs)
38+
sync_response = sync_client_method(*args, **kwargs)
39+
40+
assert async_response == sync_response
41+
42+
return async_response
43+
44+
return wrapper
45+
46+
def __getattr__(self, name: str) -> Callable[..., Any]:
47+
sync_client_method = getattr(self.sync_client, name)
48+
async_client_method = getattr(self.async_client, name)
49+
50+
if asyncio.iscoroutinefunction(async_client_method):
51+
return self._async_method_wrapper(sync_client_method, async_client_method)
52+
53+
return self._sync_method_wrapper(sync_client_method, async_client_method)
54+
55+
56+
__all__ = [
57+
"TestingHttpxClient",
58+
]

lib/dl_httpx/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ attrs = "*"
1212
dl-configs = {path = "../dl_configs"}
1313
dl-pydantic = {path = "../dl_pydantic"}
1414
dl-retrier = {path = "../dl_retrier"}
15+
dl-testing = {path = "../dl_testing"}
1516
httpx = "*"
1617
pydantic = "*"
1718
python = ">=3.10, <3.13"

0 commit comments

Comments
 (0)