-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_es_exceptions.py
More file actions
254 lines (203 loc) · 7.85 KB
/
test_es_exceptions.py
File metadata and controls
254 lines (203 loc) · 7.85 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import pytest
from biothings.web.query.pipeline import (
capturesESExceptions,
RawQueryInterrupt,
QueryPipelineInterrupt,
EndScrollInterrupt,
RawResultInterrupt,
QueryPipelineException,
RequestError,
NotFoundError,
ConflictError,
AuthenticationException,
AuthorizationException,
TransportError,
)
@pytest.mark.asyncio
async def test_raw_query_interrupt():
@capturesESExceptions
async def func():
raise RawQueryInterrupt({"error": "test_error"})
with pytest.raises(QueryPipelineInterrupt) as exc_info:
await func()
assert exc_info.value.code == 200
assert exc_info.value.summary == None
assert exc_info.value.details == {"error": "test_error"}
@pytest.mark.asyncio
async def test_end_scroll_interrupt():
@capturesESExceptions
async def func():
raise EndScrollInterrupt()
with pytest.raises(QueryPipelineInterrupt) as exc_info:
await func()
assert exc_info.value.code == 200
assert exc_info.value.summary == None
assert exc_info.value.details == {"success": False, "error": "No more results to return."}
@pytest.mark.asyncio
async def test_raw_result_interrupt():
@capturesESExceptions
async def func():
raise RawResultInterrupt(type("obj", (object,), {"body": "test_body"}))
with pytest.raises(QueryPipelineInterrupt) as exc_info:
await func()
assert exc_info.value.code == 200
assert exc_info.value.summary == None
assert exc_info.value.details == "test_body"
@pytest.mark.asyncio
async def test_assertion_error():
@capturesESExceptions
async def func():
raise AssertionError("test_assertion_error")
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 500
assert exc_info.value.summary == "test_assertion_error"
assert exc_info.value.details == None
@pytest.mark.asyncio
async def test_value_error():
@capturesESExceptions
async def func():
raise ValueError("test_value_error")
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 400
assert exc_info.value.summary == "ValueError"
assert exc_info.value.details == "test_value_error"
@pytest.mark.asyncio
async def test_connection_error():
@capturesESExceptions
async def func():
raise ConnectionError(message="test_connection_error", meta={}, body={})
with pytest.raises(Exception) as exc_info:
await func()
assert exc_info.value.code == 400
assert exc_info.value.summary == "TypeError"
assert exc_info.value.details == "ConnectionError() takes no keyword arguments"
@pytest.mark.asyncio
async def test_request_error():
@capturesESExceptions
async def func():
raise RequestError(message="test_request_error", meta={}, body={})
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 400
assert exc_info.value.summary == "test_request_error"
@pytest.mark.asyncio
async def test_not_found_error():
@capturesESExceptions
async def func():
raise NotFoundError(message="test_not_found_error", meta={}, body={})
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 404
assert exc_info.value.summary == "test_not_found_error"
assert exc_info.value.details == {}
@pytest.mark.asyncio
async def test_conflict_error():
@capturesESExceptions
async def func():
raise ConflictError(message="test_conflict_error", meta={}, body={})
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 409
assert exc_info.value.summary == "test_conflict_error"
assert exc_info.value.details == {}
@pytest.mark.asyncio
async def test_authentication_exception():
@capturesESExceptions
async def func():
raise AuthenticationException(message="test_authentication_exception", meta={}, body={})
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 403
assert exc_info.value.summary == "test_authentication_exception"
assert exc_info.value.details == {}
@pytest.mark.asyncio
async def test_authorization_exception():
@capturesESExceptions
async def func():
raise AuthorizationException(message="test_authorization_exception", meta={}, body={})
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 403
assert exc_info.value.summary == "test_authorization_exception"
assert exc_info.value.details == {}
@pytest.mark.asyncio
async def test_index_not_found_exception():
@capturesESExceptions
async def func():
exc = Exception(message="test_index_not_found_exception", meta={}, body={})
exc.status_code = 500
exc.info = {"error": {"type": "index_not_found_exception", "reason": "test_reason"}}
raise exc
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 400
assert exc_info.value.summary == "TypeError"
assert exc_info.value.details == "Exception() takes no keyword arguments"
@pytest.mark.asyncio
async def test_es_rejected_execution_exception():
@capturesESExceptions
async def func():
exc = TransportError("test_es_rejected_execution_exception")
exc.status_code = 503
exc.info = {"error": {"type": "es_rejected_execution_exception", "reason": "rejected execution of TimedRunnable..."}}
raise exc
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 503
assert exc_info.value.summary == "Service Unavailable"
assert exc_info.value.details == "Elasticsearch cluster overloaded"
@pytest.mark.asyncio
async def test_search_phase_execution_exception_rejected_execution():
@capturesESExceptions
async def func():
exc = TransportError("test_generic_exception")
exc.status_code = 500
exc.info = {"error": {"type": "search_phase_execution_exception", "reason": "rejected execution"}}
raise exc
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 503
assert exc_info.value.summary == ""
assert exc_info.value.details == None
@pytest.mark.asyncio
async def test_search_phase_execution_exception_not_rejected_execution():
@capturesESExceptions
async def func():
exc = TransportError("test_generic_exception")
exc.status_code = 500
exc.error = "test_generic_exception"
exc.info = {
"error": {
"type": "search_phase_execution_exception",
"reason": "any kind of execution",
"root_cause": [{"reason": "reason"}],
}
}
raise exc
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 500
assert exc_info.value.summary == "test_generic_exception"
assert exc_info.value.details["debug"]["error"]["type"] == "search_phase_execution_exception"
assert exc_info.value.details["debug"]["error"]["reason"] == "any kind of execution"
assert exc_info.value.details["debug"]["error"]["root_cause"][0]["reason"] == "reason"
@pytest.mark.asyncio
async def test_too_many_requests_error():
@capturesESExceptions
async def func():
exc = TransportError(
{
"status_code": 429,
}
)
exc.status_code = 429
exc.error = "too_many_requests"
exc.info = "too_many_requests"
raise exc
with pytest.raises(QueryPipelineException) as exc_info:
await func()
assert exc_info.value.code == 503
assert exc_info.value.summary == ""
assert exc_info.value.details == None