|
| 1 | +# Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +from unittest import TestCase |
| 4 | + |
| 5 | +import requests |
| 6 | +import requests_mock |
| 7 | +from airbyte_cdk.sources.streams.http.error_handlers.response_models import ResponseAction, create_fallback_error_resolution |
| 8 | +from airbyte_cdk.utils.airbyte_secrets_utils import update_secrets |
| 9 | +from airbyte_protocol.models import FailureType |
| 10 | + |
| 11 | +_A_SECRET = "a-secret" |
| 12 | +_A_URL = "https://a-url.com" |
| 13 | + |
| 14 | + |
| 15 | +class DefaultErrorResolutionTest(TestCase): |
| 16 | + |
| 17 | + def setUp(self) -> None: |
| 18 | + update_secrets([_A_SECRET]) |
| 19 | + |
| 20 | + def tearDown(self) -> None: |
| 21 | + # to avoid other tests being impacted by added secrets |
| 22 | + update_secrets([]) |
| 23 | + |
| 24 | + def test_given_none_when_create_fallback_error_resolution_then_return_error_resolution(self) -> None: |
| 25 | + error_resolution = create_fallback_error_resolution(None) |
| 26 | + |
| 27 | + assert error_resolution.failure_type == FailureType.system_error |
| 28 | + assert error_resolution.response_action == ResponseAction.RETRY |
| 29 | + assert error_resolution.error_message == "Error handler did not receive a valid response or exception. This is unexpected please contact Airbyte Support" |
| 30 | + |
| 31 | + def test_given_exception_when_create_fallback_error_resolution_then_return_error_resolution(self) -> None: |
| 32 | + exception = ValueError("This is an exception") |
| 33 | + |
| 34 | + error_resolution = create_fallback_error_resolution(exception) |
| 35 | + |
| 36 | + assert error_resolution.failure_type == FailureType.system_error |
| 37 | + assert error_resolution.response_action == ResponseAction.RETRY |
| 38 | + assert error_resolution.error_message |
| 39 | + assert "ValueError" in error_resolution.error_message |
| 40 | + assert str(exception) in error_resolution.error_message |
| 41 | + |
| 42 | + def test_given_response_can_raise_for_status_when_create_fallback_error_resolution_then_error_resolution(self) -> None: |
| 43 | + response = self._create_response(512) |
| 44 | + |
| 45 | + error_resolution = create_fallback_error_resolution(response) |
| 46 | + |
| 47 | + assert error_resolution.failure_type == FailureType.system_error |
| 48 | + assert error_resolution.response_action == ResponseAction.RETRY |
| 49 | + assert error_resolution.error_message and "512 Server Error: None for url: https://a-url.com/" in error_resolution.error_message |
| 50 | + |
| 51 | + def test_given_response_is_ok_when_create_fallback_error_resolution_then_error_resolution(self) -> None: |
| 52 | + response = self._create_response(205) |
| 53 | + |
| 54 | + error_resolution = create_fallback_error_resolution(response) |
| 55 | + |
| 56 | + assert error_resolution.failure_type == FailureType.system_error |
| 57 | + assert error_resolution.response_action == ResponseAction.RETRY |
| 58 | + assert error_resolution.error_message and str(response.status_code) in error_resolution.error_message |
| 59 | + |
| 60 | + def _create_response(self, status_code: int) -> requests.Response: |
| 61 | + with requests_mock.Mocker() as http_mocker: |
| 62 | + http_mocker.get(_A_URL, status_code=status_code) |
| 63 | + return requests.get(_A_URL) |
0 commit comments