-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_services.py
336 lines (289 loc) · 11.1 KB
/
test_services.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import datetime
import ssl
from asyncio import AbstractEventLoop, gather
from typing import List
from unittest.mock import AsyncMock, patch
import aiodns
import pytest
from aiohttp.http_exceptions import HttpProcessingError
from aioresponses import CallbackResult, aioresponses
from pydantic import BaseModel
from yarl import URL
import biar
import biar.errors
BASE_URL = "https://api.com/v1"
@pytest.fixture
def mock_server() -> aioresponses:
with aioresponses() as m:
yield m
class TestRequest:
@pytest.mark.asyncio
async def test_request_structured_many(self, mock_server: aioresponses):
# arrange
class MyModel(BaseModel):
key: str
headers = {"Content-Type": "application/json"}
response_json_content = {"key": "value"}
for i in range(2):
mock_server.get(
url=URL(BASE_URL),
headers=headers,
payload=response_json_content,
)
target_response = [
biar.model.StructuredResponse(
url=URL(BASE_URL),
status_code=200,
headers=headers,
json_content=response_json_content,
structured_content=MyModel(key="value"),
)
] * 2
# act
output_response = await biar.request_structured_many(
model=MyModel,
urls=[BASE_URL] * 2,
config=biar.RequestConfig(download_text_content=False),
)
# assert
assert target_response == output_response
@pytest.mark.asyncio
async def test_request_many_payload(self, mock_server: aioresponses):
# arrange
def callback(url: URL, **kwargs):
if url == URL(BASE_URL) / "1" and kwargs["json"] == {
"key": "1",
"ts": "2023-01-01T00:00:00",
}:
return CallbackResult(status=200)
elif url == URL(BASE_URL) / "2" and kwargs["json"] == {
"key": "2",
"ts": "2023-01-02T00:00:00",
}:
return CallbackResult(status=200)
return CallbackResult(status=500)
mock_server.put(url=URL(BASE_URL) / "1", callback=callback)
mock_server.put(url=URL(BASE_URL) / "2", callback=callback)
class StructuredContent(BaseModel):
key: str
ts: datetime.datetime
# act
output_responses = await biar.request_many(
urls=[URL(BASE_URL) / "1", URL(BASE_URL) / "2"],
config=biar.RequestConfig(method="PUT"),
payloads=[
biar.Payload(
structured_content=StructuredContent(
key="1", ts="2023-01-01T00:00:00"
)
),
biar.Payload(
structured_content=StructuredContent(
key="2", ts="2023-01-02T00:00:00"
)
),
],
)
# assert
assert all([response.status_code == 200 for response in output_responses])
@pytest.mark.asyncio
async def test_request_retry_success(self, mock_server: aioresponses):
# arrange
mock_server.get(
url=BASE_URL, status=500, payload={"Detail": "Internal Server Error"}
)
mock_server.get(url=BASE_URL, status=200)
# act
start_ts = datetime.datetime.utcnow()
output_response = await biar.request(
url=BASE_URL,
config=biar.RequestConfig(
method="GET",
download_json_content=False,
retryer=biar.model.Retryer(
attempts=2,
min_delay=1,
max_delay=1,
),
),
)
end_ts = datetime.datetime.utcnow()
elapsed_time = (end_ts - start_ts).total_seconds()
# assert
assert output_response.status_code == 200
assert 1 < elapsed_time < 2
@pytest.mark.asyncio
async def test_request_retry_exception(self, mock_server: aioresponses):
# arrange
mock_server.get(url=BASE_URL, status=500)
mock_server.get(url=BASE_URL, exception=HttpProcessingError())
mock_server.get(url=BASE_URL, exception=HttpProcessingError())
retrier = biar.model.Retryer(attempts=2, max_delay=0)
# act and assert
with pytest.raises(HttpProcessingError):
_ = await biar.request(
url=BASE_URL,
config=biar.RequestConfig(
method="GET",
retryer=retrier,
),
)
@pytest.mark.asyncio
async def test_request_retry_status_fail(self, mock_server: aioresponses):
# arrange
mock_server.get(url=BASE_URL, status=500)
mock_server.get(url=BASE_URL, status=500)
mock_server.get(url=BASE_URL, status=500)
retrier = biar.model.Retryer(attempts=2, max_delay=0)
# act and assert
with pytest.raises(biar.errors.ResponseEvaluationError):
_ = await biar.request(
url=BASE_URL,
config=biar.RequestConfig(
method="GET",
retryer=retrier,
),
)
def test_request_rate_limit(
self, event_loop: AbstractEventLoop, mock_server: aioresponses
):
# arrange
mock_server.get(url=BASE_URL, status=200)
mock_server.get(url=BASE_URL, status=200)
mock_server.get(url=BASE_URL, status=200)
rate_limiter = biar.model.RateLimiter(rate=2, time_frame=1, identity="api")
config = biar.RequestConfig(
method="GET",
rate_limiter=rate_limiter,
)
async_requests = [
biar.request(url=BASE_URL, config=config),
biar.request(url=BASE_URL, config=config),
biar.request(url=BASE_URL, config=config),
]
# act
start_ts = datetime.datetime.utcnow()
_ = event_loop.run_until_complete(gather(*async_requests))
end_ts = datetime.datetime.utcnow()
elapsed_time = (end_ts - start_ts).total_seconds()
# assert
assert 1 < elapsed_time < 2
@pytest.mark.asyncio
async def test_request_structured_many_value_error(self):
# arrange
class MyModel(BaseModel):
key: str
# act and assert
with pytest.raises(ValueError):
_ = await biar.request_structured_many(
model=MyModel,
urls=[BASE_URL] * 2,
config=biar.RequestConfig(download_text_content=False),
payloads=[biar.Payload(structured_content=MyModel(key="value"))],
)
@pytest.mark.asyncio
async def test_request_structured_retry_on_content(self, mock_server: aioresponses):
# arrange
class MyModel(BaseModel):
key: str
items: List[int]
mock_server.get(url=BASE_URL, status=200, payload={"key": "value", "items": []})
mock_server.get(url=BASE_URL, status=200, payload={"key": "value", "items": []})
mock_server.get(
url=BASE_URL, status=200, payload={"key": "value", "items": [1, 2, 3]}
)
# act
output_response = await biar.request_structured(
model=MyModel,
url=BASE_URL,
config=biar.RequestConfig(
method="GET",
retryer=biar.model.Retryer(
attempts=3,
max_delay=0,
retry_based_on_content_callback=lambda x: not x.items,
),
),
)
# assert
assert output_response.structured_content.items == [1, 2, 3]
@pytest.mark.asyncio
async def test_poll(self, mock_server: aioresponses):
# arrange
class MyModel(BaseModel):
status: str
mock_server.get(url=BASE_URL, status=200, payload={"status": "pending"})
mock_server.get(url=BASE_URL, status=200, payload={"status": "pending"})
mock_server.get(url=BASE_URL, status=200, payload={"status": "success"})
# act
output_response = await biar.poll(
model=MyModel,
poll_config=biar.PollConfig(
timeout=1, interval=0, success_condition=lambda x: x.status == "success"
),
url=BASE_URL,
)
# assert
assert output_response.structured_content.status == "success"
@pytest.mark.asyncio
async def test_poll_timeout(self, mock_server: aioresponses):
# arrange
class MyModel(BaseModel):
status: str
mock_server.get(url=BASE_URL, status=200, payload={"status": "pending"})
mock_server.get(url=BASE_URL, status=200, payload={"status": "pending"})
mock_server.get(url=BASE_URL, status=200, payload={"status": "pending"})
# act and assert
with pytest.raises(biar.errors.PollError):
_ = await biar.poll(
model=MyModel,
poll_config=biar.PollConfig(
timeout=0.1,
interval=0.05,
success_condition=lambda x: x.status == "success",
),
url=BASE_URL,
)
def test_get_ssl_context():
# arrange
extra_certificate = """
-----BEGIN CERTIFICATE-----
MIICVDCCAdugAwIBAgIQZ3SdjXfYO2rbIvT/WeK/zjAKBggqhkjOPQQDAzBsMQsw
CQYDVQQGEwJHUjE3MDUGA1UECgwuSGVsbGVuaWMgQWNhZGVtaWMgYW5kIFJlc2Vh
cmNoIEluc3RpdHV0aW9ucyBDQTEkMCIGA1UEAwwbSEFSSUNBIFRMUyBFQ0MgUm9v
dCBDQSAyMDIxMB4XDTIxMDIxOTExMDExMFoXDTQ1MDIxMzExMDEwOVowbDELMAkG
A1UEBhMCR1IxNzA1BgNVBAoMLkhlbGxlbmljIEFjYWRlbWljIGFuZCBSZXNlYXJj
aCBJbnN0aXR1dGlvbnMgQ0ExJDAiBgNVBAMMG0hBUklDQSBUTFMgRUNDIFJvb3Qg
Q0EgMjAyMTB2MBAGByqGSM49AgEGBSuBBAAiA2IABDgI/rGgltJ6rK9JOtDA4MM7
KKrxcm1lAEeIhPyaJmuqS7psBAqIXhfyVYf8MLA04jRYVxqEU+kw2anylnTDUR9Y
STHMmE5gEYd103KUkE+bECUqqHgtvpBBWJAVcqeht6NCMEAwDwYDVR0TAQH/BAUw
AwEB/zAdBgNVHQ4EFgQUyRtTgRL+BNUW0aq8mm+3oJUZbsowDgYDVR0PAQH/BAQD
AgGGMAoGCCqGSM49BAMDA2cAMGQCMBHervjcToiwqfAircJRQO9gcS3ujwLEXQNw
SaSS6sUUiHCm0w2wqsosQJz76YJumgIwK0eaB8bRwoF8yguWGEEbo/QwCZ61IygN
nxS2PFOiTAZpffpskcYqSUXm7LcT4Tps
-----END CERTIFICATE-----
"""
# act
ssl_context = biar.get_ssl_context(extra_certificate=extra_certificate)
# assert
isinstance(ssl_context, ssl.SSLContext)
class TestIsHostReachableService:
@pytest.mark.asyncio
async def test_is_host_reachable(self):
# act
with patch("aiodns.DNSResolver.query", new=AsyncMock()) as mock:
output = await biar.is_host_reachable(host="google.com")
# assert
assert output is True
mock.assert_called_once_with("google.com", qtype="A")
@pytest.mark.asyncio
async def test_is_host_reachable_error(self):
# arrange
async def side_effect(*args, **kwargs):
raise aiodns.error.DNSError
# act
with patch("aiodns.DNSResolver.query", side_effect=side_effect) as mock:
output = await biar.is_host_reachable(host="google.com")
# assert
assert output is False
assert "google.com" in mock.mock_calls[0].args