-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_client_errors.py
More file actions
50 lines (37 loc) · 1.61 KB
/
test_client_errors.py
File metadata and controls
50 lines (37 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
from collections.abc import Generator
import httpx
import pytest
import respx
from apify_client._errors import ApifyApiError
from apify_client._http_client import HTTPClient, HTTPClientAsync
_TEST_URL = 'http://example.com'
_EXPECTED_MESSAGE = 'some_message'
_EXPECTED_TYPE = 'some_type'
_EXPECTED_DATA = {
'invalidItems': {'0': ["should have required property 'name'"], '1': ["should have required property 'name'"]}
}
@pytest.fixture(autouse=True)
def mocked_response() -> Generator[respx.MockRouter]:
response_content = json.dumps(
{'error': {'message': _EXPECTED_MESSAGE, 'type': _EXPECTED_TYPE, 'data': _EXPECTED_DATA}}
)
with respx.mock() as respx_mock:
respx_mock.get(_TEST_URL).mock(return_value=httpx.Response(400, content=response_content))
yield respx_mock
def test_client_apify_api_error_with_data() -> None:
"""Test that client correctly throws ApifyApiError with error data from response."""
client = HTTPClient()
with pytest.raises(ApifyApiError) as e:
client.call(method='GET', url=_TEST_URL)
assert e.value.message == _EXPECTED_MESSAGE
assert e.value.type == _EXPECTED_TYPE
assert e.value.data == _EXPECTED_DATA
async def test_async_client_apify_api_error_with_data() -> None:
"""Test that async client correctly throws ApifyApiError with error data from response."""
client = HTTPClientAsync()
with pytest.raises(ApifyApiError) as e:
await client.call(method='GET', url=_TEST_URL)
assert e.value.message == _EXPECTED_MESSAGE
assert e.value.type == _EXPECTED_TYPE
assert e.value.data == _EXPECTED_DATA