|
| 1 | +from http import HTTPStatus as status |
| 2 | + |
| 3 | +import pytest |
| 4 | +from requests import Response |
| 5 | +from requests.exceptions import ConnectionError, Timeout |
| 6 | + |
| 7 | +from abacatepay._base_client import BaseClient |
| 8 | +from abacatepay.utils._exceptions import ( |
| 9 | + APIConnectionError, |
| 10 | + APIError, |
| 11 | + APITimeoutError, |
| 12 | + BadRequestError, |
| 13 | + ForbiddenRequest, |
| 14 | + InternalServerError, |
| 15 | + NotFoundError, |
| 16 | + UnauthorizedRequest, |
| 17 | +) |
| 18 | + |
| 19 | +url = "https://api.abacatepay.com/v1/billing/list" |
| 20 | +client = BaseClient('fake-api-key') |
| 21 | + |
| 22 | + |
| 23 | +def test_request_return_the_response_if_status_200(responses): |
| 24 | + responses.add( |
| 25 | + responses.GET, |
| 26 | + url, |
| 27 | + status=status.OK, |
| 28 | + ) |
| 29 | + |
| 30 | + response = client._request(url, "GET") |
| 31 | + |
| 32 | + assert response is not None |
| 33 | + assert isinstance(response, Response) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize('status_code,exc_classname', [ |
| 37 | + (status.BAD_REQUEST, BadRequestError), |
| 38 | + (status.FORBIDDEN, ForbiddenRequest), |
| 39 | + (status.UNAUTHORIZED, UnauthorizedRequest), |
| 40 | + (status.NOT_FOUND, NotFoundError), |
| 41 | + (status.INTERNAL_SERVER_ERROR, InternalServerError), |
| 42 | + (status.IM_A_TEAPOT, APIError) |
| 43 | +]) |
| 44 | +def test_request_raise_the_correct_exception_when_status_is_different_of_200(responses, exc_classname, status_code): |
| 45 | + responses.add( |
| 46 | + responses.GET, |
| 47 | + url, |
| 48 | + status=status_code, |
| 49 | + ) |
| 50 | + |
| 51 | + with pytest.raises(exc_classname): |
| 52 | + client._request(url, "GET") |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize('requests_exc_class,api_exc_class', [ |
| 56 | + (Timeout, APITimeoutError), |
| 57 | + (ConnectionError, APIConnectionError), |
| 58 | +]) |
| 59 | +def test_request_override_requests_timeout_and_connection_error(mocker, requests_exc_class, api_exc_class): |
| 60 | + mocker.patch('abacatepay._base_client.requests.Session.send', side_effect=requests_exc_class) |
| 61 | + with pytest.raises(api_exc_class): |
| 62 | + client._request(url, "GET") |
0 commit comments