Skip to content

Commit 929dafc

Browse files
committed
update test to use requests_mock and invoke public method only
1 parent c86a055 commit 929dafc

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

unit_tests/sources/streams/http/test_http_client.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -762,38 +762,44 @@ def test_given_different_headers_then_response_is_not_cached(requests_mock):
762762
],
763763
)
764764
def test_send_with_retry_raises_airbyte_traced_exception_with_failure_type(
765-
response_code, expected_failure_type, error_message, exception_class
765+
response_code, expected_failure_type, error_message, exception_class, requests_mock
766766
):
767+
if exception_class == UserDefinedBackoffException:
768+
769+
class CustomBackoffStrategy:
770+
def backoff_time(self, response_or_exception, attempt_count):
771+
return 0.1
772+
773+
backoff_strategy = CustomBackoffStrategy()
774+
response_action = ResponseAction.RETRY
775+
elif exception_class == RateLimitBackoffException:
776+
backoff_strategy = None
777+
response_action = ResponseAction.RATE_LIMITED
778+
else:
779+
backoff_strategy = None
780+
response_action = ResponseAction.RETRY
781+
767782
error_mapping = {
768-
response_code: ErrorResolution(ResponseAction.RETRY, expected_failure_type, error_message),
783+
response_code: ErrorResolution(response_action, expected_failure_type, error_message),
769784
}
785+
770786
http_client = HttpClient(
771787
name="test",
772788
logger=MagicMock(spec=logging.Logger),
773789
error_handler=HttpStatusErrorHandler(
774790
logger=MagicMock(), error_mapping=error_mapping, max_retries=1
775791
),
792+
backoff_strategy=backoff_strategy,
776793
)
777-
http_client._send = MagicMock(spec=http_client._send)
778-
http_client._send.__name__ = "_send"
779-
prepared_request = MagicMock(spec=requests.PreparedRequest)
780-
mocked_response = MagicMock(spec=requests.Response)
781-
mocked_response.status_code = response_code
782-
if exception_class == UserDefinedBackoffException:
783-
http_client._send.side_effect = exception_class(
784-
backoff=0,
785-
request=prepared_request,
786-
response=mocked_response,
787-
error_message=error_message,
788-
failure_type=expected_failure_type,
789-
)
790-
else:
791-
http_client._send.side_effect = exception_class(
792-
request=prepared_request,
793-
response=mocked_response,
794-
error_message=error_message,
795-
failure_type=expected_failure_type,
796-
)
794+
795+
requests_mock.register_uri(
796+
"GET",
797+
"https://airbyte.io/",
798+
status_code=response_code,
799+
json={"error": error_message},
800+
headers={},
801+
)
802+
797803
with pytest.raises(AirbyteTracedException) as e:
798-
http_client._send_with_retry(prepared_request, {})
804+
http_client.send_request(http_method="get", url="https://airbyte.io/", request_kwargs={})
799805
assert e.value.failure_type == expected_failure_type

0 commit comments

Comments
 (0)