Skip to content

Commit

Permalink
fixed exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Jan 13, 2024
1 parent 7332a73 commit ba0d6f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
2 changes: 1 addition & 1 deletion google/cloud/bigtable/data/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _retry_exception_factory(
timeout_val_str = f"of {timeout_val:0.1f}s " if timeout_val is not None else ""
# if failed due to timeout, raise deadline exceeded as primary exception
source_exc: Exception = core_exceptions.DeadlineExceeded(
f"operation_timeout{timeout_val_str} exceeded"
f"operation_timeout {timeout_val_str}exceeded"
)
elif exc_list:
# otherwise, raise non-retryable error as primary exception
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/data/_async/test__mutate_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ async def test_mutate_rows_incomplete_ignored(self):
AsyncMock(),
) as attempt_mock:
attempt_mock.side_effect = _MutateRowsIncomplete("ignored")
found_exc = None
try:
with pytest.raises(MutationsExceptionGroup) as e:
instance = self._make_one(
client,
table,
Expand All @@ -339,11 +338,9 @@ async def test_mutate_rows_incomplete_ignored(self):
mock.Mock(),
)
await instance.start()
except MutationsExceptionGroup as e:
found_exc = e
assert attempt_mock.call_count > 0
assert len(found_exc.exceptions) == 1
assert isinstance(found_exc.exceptions[0].__cause__, DeadlineExceeded)
assert len(e.value.exceptions) == 1
assert isinstance(e.value.exceptions[0].__cause__, DeadlineExceeded)

@pytest.mark.asyncio
async def test_run_attempt_single_entry_success(self):
Expand Down
67 changes: 31 additions & 36 deletions tests/unit/data/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,17 +1524,16 @@ async def test_read_rows_timeout(self, operation_timeout):
async with self._make_table() as table:
read_rows = table.client._gapic_client.read_rows
query = ReadRowsQuery()
chunks = [self._make_chunk(row_key=b"test_1")]
chunks = [core_exceptions.DeadlineExceeded("test timeout")] * 5
read_rows.side_effect = lambda *args, **kwargs: self._make_gapic_stream(
chunks, sleep_time=1
chunks, sleep_time=0.05
)
try:
with pytest.raises(core_exceptions.DeadlineExceeded) as e:
await table.read_rows(query, operation_timeout=operation_timeout)
except core_exceptions.DeadlineExceeded as e:
assert (
e.message
== f"operation_timeout of {operation_timeout:0.1f}s exceeded"
)
assert (
e.value.message
== f"operation_timeout of {operation_timeout:0.1f}s exceeded"
)

@pytest.mark.parametrize(
"per_request_t, operation_t, expected_num",
Expand Down Expand Up @@ -1570,22 +1569,21 @@ async def test_read_rows_attempt_timeout(
query = ReadRowsQuery()
chunks = [core_exceptions.DeadlineExceeded("mock deadline")]

try:
with pytest.raises(core_exceptions.DeadlineExceeded) as e:
await table.read_rows(
query,
operation_timeout=operation_t,
attempt_timeout=per_request_t,
)
except core_exceptions.DeadlineExceeded as e:
retry_exc = e.__cause__
if expected_num == 0:
assert retry_exc is None
else:
assert type(retry_exc) is RetryExceptionGroup
assert f"{expected_num} failed attempts" in str(retry_exc)
assert len(retry_exc.exceptions) == expected_num
for sub_exc in retry_exc.exceptions:
assert sub_exc.message == "mock deadline"
retry_exc = e.value.__cause__
if expected_num == 0:
assert retry_exc is None
else:
assert type(retry_exc) is RetryExceptionGroup
assert f"{expected_num} failed attempts" in str(retry_exc)
assert len(retry_exc.exceptions) == expected_num
for sub_exc in retry_exc.exceptions:
assert sub_exc.message == "mock deadline"
assert read_rows.call_count == expected_num
# check timeouts
for _, call_kwargs in read_rows.call_args_list[:-1]:
Expand Down Expand Up @@ -1617,13 +1615,12 @@ async def test_read_rows_retryable_error(self, exc_type):
)
query = ReadRowsQuery()
expected_error = exc_type("mock error")
try:
with pytest.raises(core_exceptions.DeadlineExceeded) as e:
await table.read_rows(query, operation_timeout=0.1)
except core_exceptions.DeadlineExceeded as e:
retry_exc = e.__cause__
root_cause = retry_exc.exceptions[0]
assert type(root_cause) is exc_type
assert root_cause == expected_error
retry_exc = e.value.__cause__
root_cause = retry_exc.exceptions[0]
assert type(root_cause) is exc_type
assert root_cause == expected_error

@pytest.mark.parametrize(
"exc_type",
Expand All @@ -1648,10 +1645,9 @@ async def test_read_rows_non_retryable_error(self, exc_type):
)
query = ReadRowsQuery()
expected_error = exc_type("mock error")
try:
with pytest.raises(exc_type) as e:
await table.read_rows(query, operation_timeout=0.1)
except exc_type as e:
assert e == expected_error
assert e.value == expected_error

@pytest.mark.asyncio
async def test_read_rows_revise_request(self):
Expand All @@ -1678,15 +1674,14 @@ async def test_read_rows_revise_request(self):
self._make_chunk(row_key=b"test_1"),
core_exceptions.Aborted("mock retryable error"),
]
try:
with pytest.raises(InvalidChunk):
await table.read_rows(query)
except InvalidChunk:
revise_rowset.assert_called()
first_call_kwargs = revise_rowset.call_args_list[0].kwargs
assert first_call_kwargs["row_set"] == query._to_pb(table).rows
assert first_call_kwargs["last_seen_row_key"] == b"test_1"
revised_call = read_rows.call_args_list[1].args[0]
assert revised_call.rows == return_val
revise_rowset.assert_called()
first_call_kwargs = revise_rowset.call_args_list[0].kwargs
assert first_call_kwargs["row_set"] == query._to_pb(table).rows
assert first_call_kwargs["last_seen_row_key"] == b"test_1"
revised_call = read_rows.call_args_list[1].args[0]
assert revised_call.rows == return_val

@pytest.mark.asyncio
async def test_read_rows_default_timeouts(self):
Expand Down

0 comments on commit ba0d6f5

Please sign in to comment.