Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactors Session mocking in tests to mocker.patch and mocker.spy API #1889

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions tests/sources/helpers/rest_client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions tests/sources/rest_api/integration/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
Loading