Repository navigation
Expand file tree
/
Copy pathtest_client_request_queue.py
More file actions
232 lines (186 loc) · 9.11 KB
/
Copy pathtest_client_request_queue.py
File metadata and controls
232 lines (186 loc) · 9.11 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from __future__ import annotations
import gzip
import json
import re
from typing import TYPE_CHECKING, Any
import brotli
import pytest
from werkzeug import Request, Response
from apify_client import ApifyClient, ApifyClientAsync
from apify_client.errors import ApifyApiError
if TYPE_CHECKING:
from collections.abc import Callable
from pytest_httpserver import HTTPServer
from apify_client._typeddicts import RequestDraftDict
_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT = """{
"data": {
"processedRequests": [
{
"requestId": "YiKoxjkaS9gjGTqhF",
"uniqueKey": "http://example.com/1",
"wasAlreadyPresent": true,
"wasAlreadyHandled": false
}
],
"unprocessedRequests": [
{
"uniqueKey": "http://example.com/2",
"url": "http://example.com/2",
"method": "GET"
}
]
}
}"""
async def test_batch_not_processed_raises_exception_async(httpserver: HTTPServer) -> None:
"""Test that client exceptions are not silently ignored"""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(status=401)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
with pytest.raises(ApifyApiError):
await rq_client.batch_add_requests(requests=requests)
async def test_batch_processed_partially_async(httpserver: HTTPServer) -> None:
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(
status=200, response_data=_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT
)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
batch_response = await rq_client.batch_add_requests(requests=requests)
assert requests[0]['unique_key'] in {request.unique_key for request in batch_response.processed_requests}
assert len(batch_response.unprocessed_requests) == 1
assert batch_response.unprocessed_requests[0].unique_key == requests[1]['unique_key']
def test_batch_not_processed_raises_exception_sync(httpserver: HTTPServer) -> None:
"""Test that client exceptions are not silently ignored"""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(status=401)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
with pytest.raises(ApifyApiError):
rq_client.batch_add_requests(requests=requests)
def test_batch_processed_partially_sync(httpserver: HTTPServer) -> None:
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(
token='placeholder_token',
api_url=server_url,
api_public_url=server_url,
)
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_data(
status=200, response_data=_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT
)
requests: list[RequestDraftDict] = [
{'unique_key': 'http://example.com/1', 'url': 'http://example.com/1', 'method': 'GET'},
{'unique_key': 'http://example.com/2', 'url': 'http://example.com/2', 'method': 'GET'},
]
rq_client = client.request_queue(request_queue_id='whatever')
batch_response = rq_client.batch_add_requests(requests=requests)
assert requests[0]['unique_key'] in {request.unique_key for request in batch_response.processed_requests}
assert len(batch_response.unprocessed_requests) == 1
assert batch_response.unprocessed_requests[0].unique_key == requests[1]['unique_key']
_ADD_REQUEST_RESPONSE_CONTENT = """{
"data": {
"requestId": "YiKoxjkaS9gjGTqhF",
"wasAlreadyPresent": false,
"wasAlreadyHandled": false
}
}"""
_FULLY_ADDED_BATCH_RESPONSE_CONTENT = """{
"data": {
"processedRequests": [
{
"requestId": "YiKoxjkaS9gjGTqhF",
"uniqueKey": "http://example.com/1",
"wasAlreadyPresent": false,
"wasAlreadyHandled": false
}
],
"unprocessedRequests": []
}
}"""
_SNAKE_CASE_REQUEST: dict[str, Any] = {
'unique_key': 'http://example.com/1',
'url': 'http://example.com/1',
'user_data': {'label': 'DETAIL'},
'no_retry': True,
}
_EXPECTED_CAMEL_CASE_REQUEST = {
'uniqueKey': 'http://example.com/1',
'url': 'http://example.com/1',
'userData': {'label': 'DETAIL'},
'noRetry': True,
}
def _make_json_capture_handler(received_bodies: list[Any], response_content: str) -> Callable[[Request], Response]:
def handler(request: Request) -> Response:
body = request.get_data()
encoding = request.headers.get('Content-Encoding')
if encoding == 'br':
body = brotli.decompress(body)
elif encoding == 'gzip':
body = gzip.decompress(body)
received_bodies.append(json.loads(body))
return Response(status=201, response=response_content, content_type='application/json')
return handler
def test_add_request_camel_cases_fields_undeclared_on_model_sync(httpserver: HTTPServer) -> None:
"""Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(token='placeholder_token', api_url=server_url, api_public_url=server_url)
received_bodies: list[Any] = []
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler(
_make_json_capture_handler(received_bodies, _ADD_REQUEST_RESPONSE_CONTENT)
)
client.request_queue(request_queue_id='whatever').add_request(_SNAKE_CASE_REQUEST) # ty: ignore[invalid-argument-type]
assert received_bodies == [_EXPECTED_CAMEL_CASE_REQUEST]
async def test_add_request_camel_cases_fields_undeclared_on_model_async(httpserver: HTTPServer) -> None:
"""Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(token='placeholder_token', api_url=server_url, api_public_url=server_url)
received_bodies: list[Any] = []
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler(
_make_json_capture_handler(received_bodies, _ADD_REQUEST_RESPONSE_CONTENT)
)
await client.request_queue(request_queue_id='whatever').add_request(_SNAKE_CASE_REQUEST) # ty: ignore[invalid-argument-type]
assert received_bodies == [_EXPECTED_CAMEL_CASE_REQUEST]
def test_batch_add_requests_camel_cases_fields_undeclared_on_model_sync(httpserver: HTTPServer) -> None:
"""Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClient(token='placeholder_token', api_url=server_url, api_public_url=server_url)
received_bodies: list[Any] = []
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler(
_make_json_capture_handler(received_bodies, _FULLY_ADDED_BATCH_RESPONSE_CONTENT)
)
client.request_queue(request_queue_id='whatever').batch_add_requests(requests=[_SNAKE_CASE_REQUEST]) # ty: ignore[invalid-argument-type]
assert received_bodies == [[_EXPECTED_CAMEL_CASE_REQUEST]]
async def test_batch_add_requests_camel_cases_fields_undeclared_on_model_async(httpserver: HTTPServer) -> None:
"""Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload."""
server_url = httpserver.url_for('/').removesuffix('/')
client = ApifyClientAsync(token='placeholder_token', api_url=server_url, api_public_url=server_url)
received_bodies: list[Any] = []
httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler(
_make_json_capture_handler(received_bodies, _FULLY_ADDED_BATCH_RESPONSE_CONTENT)
)
await client.request_queue(request_queue_id='whatever').batch_add_requests(requests=[_SNAKE_CASE_REQUEST]) # ty: ignore[invalid-argument-type]
assert received_bodies == [[_EXPECTED_CAMEL_CASE_REQUEST]]