This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
533 lines (472 loc) · 19.3 KB
/
test_exceptions.py
File metadata and controls
533 lines (472 loc) · 19.3 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import pytest
import sys
import google.cloud.bigtable.data.exceptions as bigtable_exceptions
# try/except added for compatibility with python < 3.8
try:
from unittest import mock
except ImportError: # pragma: NO COVER
import mock # type: ignore
class TracebackTests311:
"""
Provides a set of tests that should be run on python 3.11 and above,
to verify that the exception traceback looks as expected
"""
@pytest.mark.skipif(
sys.version_info < (3, 11), reason="requires python3.11 or higher"
)
def test_311_traceback(self):
"""
Exception customizations should not break rich exception group traceback in python 3.11
"""
import traceback
sub_exc1 = RuntimeError("first sub exception")
sub_exc2 = ZeroDivisionError("second sub exception")
sub_group = self._make_one(excs=[sub_exc2])
exc_group = self._make_one(excs=[sub_exc1, sub_group])
expected_traceback = (
f" | google.cloud.bigtable.data.exceptions.{type(exc_group).__name__}: {str(exc_group)}",
" +-+---------------- 1 ----------------",
" | RuntimeError: first sub exception",
" +---------------- 2 ----------------",
f" | google.cloud.bigtable.data.exceptions.{type(sub_group).__name__}: {str(sub_group)}",
" +-+---------------- 1 ----------------",
" | ZeroDivisionError: second sub exception",
" +------------------------------------",
)
exception_caught = False
try:
raise exc_group
except self._get_class():
exception_caught = True
tb = traceback.format_exc()
tb_relevant_lines = tuple(tb.splitlines()[3:])
assert expected_traceback == tb_relevant_lines
assert exception_caught
@pytest.mark.skipif(
sys.version_info < (3, 11), reason="requires python3.11 or higher"
)
def test_311_traceback_with_cause(self):
"""
traceback should display nicely with sub-exceptions with __cause__ set
"""
import traceback
sub_exc1 = RuntimeError("first sub exception")
cause_exc = ImportError("cause exception")
sub_exc1.__cause__ = cause_exc
sub_exc2 = ZeroDivisionError("second sub exception")
exc_group = self._make_one(excs=[sub_exc1, sub_exc2])
expected_traceback = (
f" | google.cloud.bigtable.data.exceptions.{type(exc_group).__name__}: {str(exc_group)}",
" +-+---------------- 1 ----------------",
" | ImportError: cause exception",
" | ",
" | The above exception was the direct cause of the following exception:",
" | ",
" | RuntimeError: first sub exception",
" +---------------- 2 ----------------",
" | ZeroDivisionError: second sub exception",
" +------------------------------------",
)
exception_caught = False
try:
raise exc_group
except self._get_class():
exception_caught = True
tb = traceback.format_exc()
tb_relevant_lines = tuple(tb.splitlines()[3:])
assert expected_traceback == tb_relevant_lines
assert exception_caught
@pytest.mark.skipif(
sys.version_info < (3, 11), reason="requires python3.11 or higher"
)
def test_311_exception_group(self):
"""
Python 3.11+ should handle exepctions as native exception groups
"""
exceptions = [RuntimeError("mock"), ValueError("mock")]
instance = self._make_one(excs=exceptions)
# ensure split works as expected
runtime_error, others = instance.split(lambda e: isinstance(e, RuntimeError))
assert runtime_error.exceptions[0] == exceptions[0]
assert others.exceptions[0] == exceptions[1]
class TracebackTests310:
"""
Provides a set of tests that should be run on python 3.10 and under,
to verify that the exception traceback looks as expected
"""
@pytest.mark.skipif(
sys.version_info >= (3, 11), reason="requires python3.10 or lower"
)
def test_310_traceback(self):
"""
Exception customizations should not break rich exception group traceback in python 3.10
"""
import traceback
sub_exc1 = RuntimeError("first sub exception")
sub_exc2 = ZeroDivisionError("second sub exception")
sub_group = self._make_one(excs=[sub_exc2])
exc_group = self._make_one(excs=[sub_exc1, sub_group])
found_message = str(exc_group).splitlines()[0]
found_sub_message = str(sub_group).splitlines()[0]
expected_traceback = (
f"google.cloud.bigtable.data.exceptions.{type(exc_group).__name__}: {found_message}",
"--+---------------- 1 ----------------",
" | RuntimeError: first sub exception",
" +---------------- 2 ----------------",
f" | {type(sub_group).__name__}: {found_sub_message}",
" --+---------------- 1 ----------------",
" | ZeroDivisionError: second sub exception",
" +------------------------------------",
)
exception_caught = False
try:
raise exc_group
except self._get_class():
exception_caught = True
tb = traceback.format_exc()
tb_relevant_lines = tuple(tb.splitlines()[3:])
assert expected_traceback == tb_relevant_lines
assert exception_caught
@pytest.mark.skipif(
sys.version_info >= (3, 11), reason="requires python3.10 or lower"
)
def test_310_traceback_with_cause(self):
"""
traceback should display nicely with sub-exceptions with __cause__ set
"""
import traceback
sub_exc1 = RuntimeError("first sub exception")
cause_exc = ImportError("cause exception")
sub_exc1.__cause__ = cause_exc
sub_exc2 = ZeroDivisionError("second sub exception")
exc_group = self._make_one(excs=[sub_exc1, sub_exc2])
found_message = str(exc_group).splitlines()[0]
expected_traceback = (
f"google.cloud.bigtable.data.exceptions.{type(exc_group).__name__}: {found_message}",
"--+---------------- 1 ----------------",
" | ImportError: cause exception",
" | ",
" | The above exception was the direct cause of the following exception:",
" | ",
" | RuntimeError: first sub exception",
" +---------------- 2 ----------------",
" | ZeroDivisionError: second sub exception",
" +------------------------------------",
)
exception_caught = False
try:
raise exc_group
except self._get_class():
exception_caught = True
tb = traceback.format_exc()
tb_relevant_lines = tuple(tb.splitlines()[3:])
assert expected_traceback == tb_relevant_lines
assert exception_caught
class TestBigtableExceptionGroup(TracebackTests311, TracebackTests310):
"""
Subclass for MutationsExceptionGroup, RetryExceptionGroup, and ShardedReadRowsExceptionGroup
"""
def _get_class(self):
from google.cloud.bigtable.data.exceptions import _BigtableExceptionGroup
return _BigtableExceptionGroup
def _make_one(self, message="test_message", excs=None):
if excs is None:
excs = [RuntimeError("mock")]
return self._get_class()(message, excs=excs)
def test_raise(self):
"""
Create exception in raise statement, which calls __new__ and __init__
"""
test_msg = "test message"
test_excs = [Exception(test_msg)]
with pytest.raises(self._get_class()) as e:
raise self._get_class()(test_msg, test_excs)
found_message = str(e.value).splitlines()[
0
] # added to prase out subexceptions in <3.11
assert found_message == test_msg
assert list(e.value.exceptions) == test_excs
def test_raise_empty_list(self):
"""
Empty exception lists are not supported
"""
with pytest.raises(ValueError) as e:
raise self._make_one(excs=[])
assert "non-empty sequence" in str(e.value)
def test_exception_handling(self):
"""
All versions should inherit from exception
and support tranditional exception handling
"""
instance = self._make_one()
assert isinstance(instance, Exception)
try:
raise instance
except Exception as e:
assert isinstance(e, Exception)
assert e == instance
was_raised = True
assert was_raised
class TestMutationsExceptionGroup(TestBigtableExceptionGroup):
def _get_class(self):
from google.cloud.bigtable.data.exceptions import MutationsExceptionGroup
return MutationsExceptionGroup
def _make_one(self, excs=None, num_entries=3):
if excs is None:
excs = [RuntimeError("mock")]
return self._get_class()(excs, num_entries)
@pytest.mark.parametrize(
"exception_list,total_entries,expected_message",
[
([Exception()], 1, "1 failed entry from 1 attempted."),
([Exception()], 2, "1 failed entry from 2 attempted."),
(
[Exception(), RuntimeError()],
2,
"2 failed entries from 2 attempted.",
),
],
)
def test_raise(self, exception_list, total_entries, expected_message):
"""
Create exception in raise statement, which calls __new__ and __init__
"""
with pytest.raises(self._get_class()) as e:
raise self._get_class()(exception_list, total_entries)
found_message = str(e.value).splitlines()[
0
] # added to prase out subexceptions in <3.11
assert found_message == expected_message
assert list(e.value.exceptions) == exception_list
def test_raise_custom_message(self):
"""
should be able to set a custom error message
"""
custom_message = "custom message"
exception_list = [Exception()]
with pytest.raises(self._get_class()) as e:
raise self._get_class()(exception_list, 5, message=custom_message)
found_message = str(e.value).splitlines()[
0
] # added to prase out subexceptions in <3.11
assert found_message == custom_message
assert list(e.value.exceptions) == exception_list
@pytest.mark.parametrize(
"first_list_len,second_list_len,total_excs,entry_count,expected_message",
[
(3, 0, 3, 4, "3 failed entries from 4 attempted."),
(1, 0, 1, 2, "1 failed entry from 2 attempted."),
(0, 1, 1, 2, "1 failed entry from 2 attempted."),
(2, 2, 4, 4, "4 failed entries from 4 attempted."),
(
1,
1,
3,
2,
"3 failed entries from 2 attempted. (first 1 and last 1 attached as sub-exceptions; 1 truncated)",
),
(
1,
2,
100,
2,
"100 failed entries from 2 attempted. (first 1 and last 2 attached as sub-exceptions; 97 truncated)",
),
(
2,
1,
4,
9,
"4 failed entries from 9 attempted. (first 2 and last 1 attached as sub-exceptions; 1 truncated)",
),
(
3,
0,
10,
10,
"10 failed entries from 10 attempted. (first 3 attached as sub-exceptions; 7 truncated)",
),
(
0,
3,
10,
10,
"10 failed entries from 10 attempted. (last 3 attached as sub-exceptions; 7 truncated)",
),
],
)
def test_from_truncated_lists(
self, first_list_len, second_list_len, total_excs, entry_count, expected_message
):
"""
Should be able to make MutationsExceptionGroup using a pair of
lists representing a larger truncated list of exceptions
"""
first_list = [Exception()] * first_list_len
second_list = [Exception()] * second_list_len
with pytest.raises(self._get_class()) as e:
raise self._get_class().from_truncated_lists(
first_list, second_list, total_excs, entry_count
)
found_message = str(e.value).splitlines()[
0
] # added to prase out subexceptions in <3.11
assert found_message == expected_message
assert list(e.value.exceptions) == first_list + second_list
class TestRetryExceptionGroup(TestBigtableExceptionGroup):
def _get_class(self):
from google.cloud.bigtable.data.exceptions import RetryExceptionGroup
return RetryExceptionGroup
def _make_one(self, excs=None):
if excs is None:
excs = [RuntimeError("mock")]
return self._get_class()(excs=excs)
@pytest.mark.parametrize(
"exception_list,expected_message",
[
([Exception()], "1 failed attempt"),
([Exception(), RuntimeError()], "2 failed attempts"),
(
[Exception(), ValueError("test")],
"2 failed attempts",
),
(
[
bigtable_exceptions.RetryExceptionGroup(
[Exception(), ValueError("test")]
)
],
"1 failed attempt",
),
],
)
def test_raise(self, exception_list, expected_message):
"""
Create exception in raise statement, which calls __new__ and __init__
"""
with pytest.raises(self._get_class()) as e:
raise self._get_class()(exception_list)
found_message = str(e.value).splitlines()[
0
] # added to prase out subexceptions in <3.11
assert found_message == expected_message
assert list(e.value.exceptions) == exception_list
class TestShardedReadRowsExceptionGroup(TestBigtableExceptionGroup):
def _get_class(self):
from google.cloud.bigtable.data.exceptions import ShardedReadRowsExceptionGroup
return ShardedReadRowsExceptionGroup
def _make_one(self, excs=None, succeeded=None, num_entries=3):
if excs is None:
excs = [RuntimeError("mock")]
succeeded = succeeded or []
return self._get_class()(excs, succeeded, num_entries)
@pytest.mark.parametrize(
"exception_list,succeeded,total_entries,expected_message",
[
([Exception()], [], 1, "1 sub-exception (from 1 query attempted)"),
([Exception()], [1], 2, "1 sub-exception (from 2 queries attempted)"),
(
[Exception(), RuntimeError()],
[0, 1],
2,
"2 sub-exceptions (from 2 queries attempted)",
),
],
)
def test_raise(self, exception_list, succeeded, total_entries, expected_message):
"""
Create exception in raise statement, which calls __new__ and __init__
"""
with pytest.raises(self._get_class()) as e:
raise self._get_class()(exception_list, succeeded, total_entries)
found_message = str(e.value).splitlines()[
0
] # added to prase out subexceptions in <3.11
assert found_message == expected_message
assert list(e.value.exceptions) == exception_list
assert e.value.successful_rows == succeeded
class TestFailedMutationEntryError:
def _get_class(self):
from google.cloud.bigtable.data.exceptions import FailedMutationEntryError
return FailedMutationEntryError
def _make_one(self, idx=9, entry=mock.Mock(), cause=RuntimeError("mock")):
return self._get_class()(idx, entry, cause)
def test_raise(self):
"""
Create exception in raise statement, which calls __new__ and __init__
"""
test_idx = 2
test_entry = mock.Mock()
test_exc = ValueError("test")
with pytest.raises(self._get_class()) as e:
raise self._get_class()(test_idx, test_entry, test_exc)
assert str(e.value) == "Failed idempotent mutation entry at index 2"
assert e.value.index == test_idx
assert e.value.entry == test_entry
assert e.value.__cause__ == test_exc
assert isinstance(e.value, Exception)
assert test_entry.is_idempotent.call_count == 1
def test_raise_idempotent(self):
"""
Test raise with non idempotent entry
"""
test_idx = 2
test_entry = unittest.mock.Mock()
test_entry.is_idempotent.return_value = False
test_exc = ValueError("test")
with pytest.raises(self._get_class()) as e:
raise self._get_class()(test_idx, test_entry, test_exc)
assert str(e.value) == "Failed non-idempotent mutation entry at index 2"
assert e.value.index == test_idx
assert e.value.entry == test_entry
assert e.value.__cause__ == test_exc
assert test_entry.is_idempotent.call_count == 1
def test_no_index(self):
"""
Instances without an index should display different error string
"""
test_idx = None
test_entry = unittest.mock.Mock()
test_exc = ValueError("test")
with pytest.raises(self._get_class()) as e:
raise self._get_class()(test_idx, test_entry, test_exc)
assert str(e.value) == "Failed idempotent mutation entry"
assert e.value.index == test_idx
assert e.value.entry == test_entry
assert e.value.__cause__ == test_exc
assert isinstance(e.value, Exception)
assert test_entry.is_idempotent.call_count == 1
class TestFailedQueryShardError:
def _get_class(self):
from google.cloud.bigtable.data.exceptions import FailedQueryShardError
return FailedQueryShardError
def _make_one(self, idx=9, query=mock.Mock(), cause=RuntimeError("mock")):
return self._get_class()(idx, query, cause)
def test_raise(self):
"""
Create exception in raise statement, which calls __new__ and __init__
"""
test_idx = 2
test_query = mock.Mock()
test_exc = ValueError("test")
with pytest.raises(self._get_class()) as e:
raise self._get_class()(test_idx, test_query, test_exc)
assert str(e.value) == "Failed query at index 2"
assert e.value.index == test_idx
assert e.value.query == test_query
assert e.value.__cause__ == test_exc
assert isinstance(e.value, Exception)