Skip to content

Commit 7c64673

Browse files
authored
Merge pull request #1 from community-of-python/fix-naming
fix naming of test client and arguments
2 parents 4145c82 + fd43f8e commit 7c64673

File tree

7 files changed

+32
-32
lines changed

7 files changed

+32
-32
lines changed

base_client/base_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
T = typing.TypeVar("T")
2727

2828

29-
@dataclasses.dataclass
29+
@dataclasses.dataclass(kw_only=True, slots=True)
3030
class BaseClient:
3131
client: httpx.AsyncClient
32-
retryer: circuit_breaker_box.Retrier[httpx.Response] | None = None
32+
retrier: circuit_breaker_box.Retrier[httpx.Response] | None = None
3333

3434
async def send(self, *, request: httpx.Request) -> httpx.Response:
35-
if self.retryer:
36-
return await self.retryer.retry(self._process_request, request.url.host, request=request)
35+
if self.retrier:
36+
return await self.retrier.retry(self._process_request, request.url.host, request=request)
3737
return await self._process_request(request)
3838

3939
def prepare_request( # noqa: PLR0913

examples/example_client_with_retry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def main() -> None:
4747
)
4848
client = SomeSpecificClient(
4949
client=httpx.AsyncClient(base_url=SOME_HOST, timeout=httpx.Timeout(1)),
50-
retryer=retrier_with_circuit_breaker,
50+
retrier=retrier_with_circuit_breaker,
5151
)
5252
answer = await client.some_method(params={})
5353
logger.debug(answer)

examples/example_client_with_retry_circuit_breaker_in_memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def main() -> None:
6363
)
6464
client = SomeSpecificClient(
6565
client=httpx.AsyncClient(base_url=SOME_HOST, timeout=httpx.Timeout(1)),
66-
retryer=retrier_with_circuit_breaker,
66+
retrier=retrier_with_circuit_breaker,
6767
)
6868
answer = await client.some_method(params={})
6969
logger.debug(answer)

examples/example_client_with_retry_circuit_breaker_redis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
@dataclasses.dataclass
21-
class TestRedisConnection(aioredis.Redis): # type: ignore[type-arg]
21+
class FakeRedisConnection(aioredis.Redis): # type: ignore[type-arg]
2222
async def incr(self, host: str | bytes, amount: int = 1) -> int:
2323
logger.debug("host: %s, amount: %d{amount}", host, amount)
2424
return amount
@@ -62,7 +62,7 @@ async def main() -> None:
6262
circuit_breaker = circuit_breaker_box.CircuitBreakerRedis(
6363
reset_timeout_in_seconds=RESET_TIMEOUT_IN_SECONDS,
6464
max_failure_count=CIRCUIT_BREAKER_MAX_FAILURE_COUNT,
65-
redis_connection=TestRedisConnection(),
65+
redis_connection=FakeRedisConnection(),
6666
)
6767
retrier_with_circuit_breaker = circuit_breaker_box.Retrier[httpx.Response](
6868
circuit_breaker=circuit_breaker,
@@ -72,7 +72,7 @@ async def main() -> None:
7272
)
7373
client = SomeSpecificClient(
7474
client=httpx.AsyncClient(base_url=SOME_HOST, timeout=httpx.Timeout(1)),
75-
retryer=retrier_with_circuit_breaker,
75+
retrier=retrier_with_circuit_breaker,
7676
)
7777
answer = await client.some_method(params={"foo": "bar"})
7878
logger.debug(answer.model_dump())

tests/conftest.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import tenacity
55

66
import base_client
7-
from examples.example_client_with_retry_circuit_breaker_redis import TestRedisConnection
7+
from examples.example_client_with_retry_circuit_breaker_redis import FakeRedisConnection
88

99

1010
TEST_BASE_URL = "http://example.com/"
1111

1212

13-
class TestClient(base_client.BaseClient):
13+
class FakeClient(base_client.BaseClient):
1414
async def fetch_async(self, request: httpx.Request) -> httpx.Response:
1515
return await self.send(request=request)
1616

@@ -22,9 +22,9 @@ async def fetch_async(self, request: httpx.Request) -> httpx.Response:
2222

2323

2424
@pytest.fixture(name="test_client_with_circuit_breaker_redis")
25-
def test_client_with_circuit_breaker_redis() -> TestClient:
25+
def test_client_with_circuit_breaker_redis() -> FakeClient:
2626
circuit_breaker = circuit_breaker_box.CircuitBreakerRedis(
27-
redis_connection=TestRedisConnection(),
27+
redis_connection=FakeRedisConnection(),
2828
reset_timeout_in_seconds=RESET_TIMEOUT_IN_SECONDS,
2929
max_failure_count=CLIENT_MAX_FAILURE_COUNT,
3030
)
@@ -34,14 +34,14 @@ def test_client_with_circuit_breaker_redis() -> TestClient:
3434
retry_cause=tenacity.retry_if_exception_type((httpx.RequestError, base_client.errors.HttpStatusError)),
3535
wait_strategy=tenacity.wait_none(),
3636
)
37-
return TestClient(
37+
return FakeClient(
3838
client=httpx.AsyncClient(base_url=TEST_BASE_URL, timeout=httpx.Timeout(1)),
39-
retryer=retrier_with_circuit_breaker,
39+
retrier=retrier_with_circuit_breaker,
4040
)
4141

4242

4343
@pytest.fixture(name="test_client_with_circuit_breaker_in_memory")
44-
def test_client_with_circuit_breaker_in_memory() -> TestClient:
44+
def test_client_with_circuit_breaker_in_memory() -> FakeClient:
4545
circuit_breaker = circuit_breaker_box.CircuitBreakerInMemory(
4646
reset_timeout_in_seconds=RESET_TIMEOUT_IN_SECONDS,
4747
max_cache_size=MAX_CACHE_SIZE,
@@ -53,12 +53,12 @@ def test_client_with_circuit_breaker_in_memory() -> TestClient:
5353
retry_cause=tenacity.retry_if_exception_type((httpx.RequestError, base_client.errors.HttpStatusError)),
5454
wait_strategy=tenacity.wait_none(),
5555
)
56-
return TestClient(
56+
return FakeClient(
5757
client=httpx.AsyncClient(base_url=TEST_BASE_URL, timeout=httpx.Timeout(1)),
58-
retryer=retrier_with_circuit_breaker,
58+
retrier=retrier_with_circuit_breaker,
5959
)
6060

6161

6262
@pytest.fixture(name="test_client")
63-
def fixture_test_client() -> TestClient:
64-
return TestClient(client=httpx.AsyncClient(base_url=TEST_BASE_URL, timeout=httpx.Timeout(1)))
63+
def fixture_test_client() -> FakeClient:
64+
return FakeClient(client=httpx.AsyncClient(base_url=TEST_BASE_URL, timeout=httpx.Timeout(1)))

tests/test_base_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import base_client
88
from base_client.response import response_to_model
9-
from tests.conftest import TEST_BASE_URL, TestClient
9+
from tests.conftest import TEST_BASE_URL, FakeClient
1010

1111

1212
@respx.mock
@@ -35,15 +35,15 @@
3535
),
3636
],
3737
)
38-
async def test_client_async(test_client: TestClient, expected_request: httpx.Request) -> None:
38+
async def test_client_async(test_client: FakeClient, expected_request: httpx.Request) -> None:
3939
mocked_route = respx.get(expected_request.url).mock(return_value=httpx.Response(status_code=httpx.codes.OK))
4040
response = await test_client.fetch_async(expected_request)
4141
assert mocked_route.called
4242
assert response.status_code == httpx.codes.OK
4343

4444

4545
@respx.mock
46-
async def test_client_request_404(test_client: TestClient) -> None:
46+
async def test_client_request_404(test_client: FakeClient) -> None:
4747
mocked_route = respx.get(TEST_BASE_URL).mock(return_value=httpx.Response(status_code=httpx.codes.NOT_FOUND))
4848
response = await test_client.fetch_async(test_client.prepare_request(method="GET", url=TEST_BASE_URL))
4949
assert mocked_route.called
@@ -75,7 +75,7 @@ async def test_client_request_404(test_client: TestClient) -> None:
7575
(httpx.TooManyRedirects("TooManyRedirects message"), httpx.TooManyRedirects),
7676
],
7777
)
78-
async def test_retries(side_effect: type[Exception], expected_raise: type[Exception], test_client: TestClient) -> None:
78+
async def test_retries(side_effect: type[Exception], expected_raise: type[Exception], test_client: FakeClient) -> None:
7979
mocked_route = respx.get(TEST_BASE_URL).mock(side_effect=side_effect)
8080
with pytest.raises(expected_raise):
8181
await test_client.fetch_async(test_client.prepare_request(method="GET", url=TEST_BASE_URL))
@@ -98,7 +98,7 @@ async def test_retries(side_effect: type[Exception], expected_raise: type[Except
9898
],
9999
)
100100
async def test_wont_retry(
101-
side_effect: type[Exception], expected_raise: type[Exception], test_client: TestClient
101+
side_effect: type[Exception], expected_raise: type[Exception], test_client: FakeClient
102102
) -> None:
103103
mocked_route = respx.get(TEST_BASE_URL).mock(side_effect=side_effect)
104104

@@ -115,7 +115,7 @@ async def test_wont_retry(
115115
(599, base_client.HttpServerError),
116116
],
117117
)
118-
async def test_validate_response(status_code: int, side_effect: type[Exception], test_client: TestClient) -> None:
118+
async def test_validate_response(status_code: int, side_effect: type[Exception], test_client: FakeClient) -> None:
119119
response = httpx.Response(
120120
status_code=status_code,
121121
content=b"",
@@ -145,6 +145,6 @@ class TestModel(pydantic.BaseModel):
145145
(httpx.URL(TEST_BASE_URL + "?1=2"), [("3", "4")], TEST_BASE_URL + "?1=2&3=4"),
146146
],
147147
)
148-
async def test_prepare_request(url: str, params: dict[str, str], expected_url: str, test_client: TestClient) -> None:
148+
async def test_prepare_request(url: str, params: dict[str, str], expected_url: str, test_client: FakeClient) -> None:
149149
request = test_client.prepare_request(method="GET", url=url, params=params)
150150
assert request.url == expected_url

tests/test_circuit_breaker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import pytest
66
import respx
77

8-
from examples.example_client_with_retry_circuit_breaker_redis import TestRedisConnection
9-
from tests.conftest import CLIENT_MAX_FAILURE_COUNT, TEST_BASE_URL, TestClient
8+
from examples.example_client_with_retry_circuit_breaker_redis import FakeRedisConnection
9+
from tests.conftest import CLIENT_MAX_FAILURE_COUNT, TEST_BASE_URL, FakeClient
1010

1111

1212
@pytest.mark.parametrize(
@@ -35,7 +35,7 @@
3535
)
3636
@respx.mock
3737
async def test_circuit_breaker_in_memory(
38-
test_client_with_circuit_breaker_in_memory: TestClient, side_effect: Exception
38+
test_client_with_circuit_breaker_in_memory: FakeClient, side_effect: Exception
3939
) -> None:
4040
mocked_route = respx.get(TEST_BASE_URL).mock(side_effect=side_effect)
4141

@@ -64,13 +64,13 @@ async def test_circuit_breaker_redis(
6464
side_effect: type[Exception],
6565
expected_raise: type[Exception],
6666
errors_by_host_in_redis: int,
67-
test_client_with_circuit_breaker_redis: TestClient,
67+
test_client_with_circuit_breaker_redis: FakeClient,
6868
monkeypatch: pytest.MonkeyPatch,
6969
) -> None:
7070
async def mock_return(*args: typing.Any, **kwargs: typing.Any) -> int: # noqa: ARG001, ANN401
7171
return errors_by_host_in_redis
7272

73-
monkeypatch.setattr(TestRedisConnection, "get", mock_return)
73+
monkeypatch.setattr(FakeRedisConnection, "get", mock_return)
7474

7575
respx.get(TEST_BASE_URL).mock(side_effect=side_effect)
7676
with pytest.raises(expected_raise):

0 commit comments

Comments
 (0)