From 0f8219196104d32dc24eefd07abcb05dc11b6fff Mon Sep 17 00:00:00 2001 From: Willi Date: Fri, 27 Sep 2024 16:47:38 +0530 Subject: [PATCH] refactors Session mocking in tests to mocker.patch and mocker.spy API --- tests/sources/helpers/rest_client/test_client.py | 13 +++---------- tests/sources/rest_api/integration/test_offline.py | 6 +----- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/tests/sources/helpers/rest_client/test_client.py b/tests/sources/helpers/rest_client/test_client.py index 5ec48e2972..488d7ef525 100644 --- a/tests/sources/helpers/rest_client/test_client.py +++ b/tests/sources/helpers/rest_client/test_client.py @@ -444,29 +444,22 @@ def test_configurable_timeout(self, mocker) -> None: import requests - original_send = requests.Session.send - requests.Session.send = mocker.Mock() # type: ignore[method-assign] + mocked_send = mocker.patch.object(requests.Session, "send") rest_client.get("/posts/1") - assert requests.Session.send.call_args[1] == { # type: ignore[attr-defined] + assert mocked_send.call_args[1] == { "timeout": 42, "proxies": ANY, "stream": ANY, "verify": ANY, "cert": ANY, } - # restore, otherwise side-effect on subsequent tests - requests.Session.send = original_send # type: ignore[method-assign] def test_request_kwargs(self, mocker) -> None: - def send_spy(*args, **kwargs): - return original_send(*args, **kwargs) - rest_client = RESTClient( base_url="https://api.example.com", session=Client().session, ) - original_send = rest_client.session.send - mocked_send = mocker.patch.object(rest_client.session, "send", side_effect=send_spy) + mocked_send = mocker.spy(rest_client.session, "send") rest_client.get( path="/posts/1", diff --git a/tests/sources/rest_api/integration/test_offline.py b/tests/sources/rest_api/integration/test_offline.py index 57cffc99d0..cb91e0d680 100644 --- a/tests/sources/rest_api/integration/test_offline.py +++ b/tests/sources/rest_api/integration/test_offline.py @@ -373,12 +373,8 @@ def test_multiple_response_actions_on_every_response(mock_api_server, mocker): class CustomSession(Session): pass - def send_spy(*args, **kwargs): - return original_send(*args, **kwargs) - my_session = CustomSession() - original_send = my_session.send - mocked_send = mocker.patch.object(my_session, "send", side_effect=send_spy) + mocked_send = mocker.spy(my_session, "send") source = rest_api_source( {