Skip to content

Commit 0d1ae51

Browse files
committed
Update MockRouter for new pytest-httpx release
1 parent c89e7d9 commit 0d1ae51

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

src/ghga_service_commons/api/mock_router.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@
2323
from typing import Any, Callable, Generic, TypeVar, cast, get_type_hints
2424

2525
import httpx
26-
import pytest
2726
from pydantic import BaseModel
2827

2928
from ghga_service_commons.httpyexpect.server.exceptions import HttpException
3029

31-
__all__ = [
32-
"MockRouter",
33-
"assert_all_responses_were_requested",
34-
"HttpException",
35-
]
30+
__all__ = ["MockRouter", "HttpException"]
3631

3732
BRACKET_PATTERN = re.compile(r"{.*?}")
3833

@@ -67,17 +62,6 @@ def _get_signature_info(endpoint_function: Callable) -> dict[str, Any]:
6762
return signature_parameters
6863

6964

70-
@pytest.fixture
71-
def assert_all_responses_were_requested() -> bool:
72-
"""Whether httpx checks that all registered responses are sent back.
73-
74-
This is set to false because the registered endpoints are considered mocked even if
75-
they aren't used in a given test. If this is True (default), pytest_httpx will raise
76-
an error if a given test doesn't hit every mocked endpoint.
77-
"""
78-
return False
79-
80-
8165
class RegisteredEndpoint(BaseModel):
8266
"""Endpoint data with the url turned into regex string to get parameters in path."""
8367

tests/integration/test_mock_router.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
from fastapi import HTTPException
2424
from pytest_httpx import HTTPXMock, httpx_mock # noqa: F401
2525

26-
from ghga_service_commons.api.mock_router import ( # noqa: F401
27-
MockRouter,
28-
assert_all_responses_were_requested,
29-
)
26+
from ghga_service_commons.api.mock_router import MockRouter
3027
from ghga_service_commons.httpyexpect.server.exceptions import HttpException
3128
from tests.integration.fixtures.mock_api import app
3229

@@ -85,7 +82,8 @@ def test_get_two_path_variables(httpx_mock: HTTPXMock): # noqa: F811
8582
"""Make sure the handler can parse paths with more than one variable."""
8683
httpx_mock.add_callback(callback=app.handle_request)
8784

88-
expected = ["4", 9] # pass str number as a sanity check that it stays a str
85+
# pass str number as a sanity check that it stays a str
86+
expected = ["4", 9]
8987

9088
with httpx.Client(base_url=BASE_URL) as client:
9189
response = client.get(f"/items/{expected[0]}/sizes/{expected[1]}")
@@ -186,6 +184,9 @@ def dummy(parameter1) -> None:
186184
"""Define a dummy function with missing type-hint info."""
187185

188186

187+
@pytest.mark.httpx_mock(
188+
assert_all_responses_were_requested=False, can_send_already_matched_responses=True
189+
)
189190
def test_handler_errors_filtering(httpx_mock: HTTPXMock): # noqa: F811
190191
"""Make sure only the specified errors are passed to the handler.
191192

0 commit comments

Comments
 (0)