Skip to content

Commit cbb2f03

Browse files
committed
try to improve code coverage
1 parent c7a8c96 commit cbb2f03

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

google/cloud/bigquery/_job_helpers.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,27 @@ def do_query():
169169
else:
170170
return query_job
171171

172-
if job_retry is None:
173-
future = do_query()
174-
else:
175-
# Per https://github.com/googleapis/python-bigquery/issues/2134, sometimes
176-
# we get a 404 error. In this case, if we get this far, assume that the job
177-
# doesn't actually exist and try again. We can't add 404 to the default
178-
# job_retry because that happens for errors like "this table does not
179-
# exist", which probably won't resolve with a retry.
172+
if job_retry is not None:
173+
180174
def do_query_predicate(exc) -> bool:
181175
if isinstance(exc, core_exceptions.RetryError):
182176
exc = exc.cause
183177

178+
# Per https://github.com/googleapis/python-bigquery/issues/2134, sometimes
179+
# we get a 404 error. In this case, if we get this far, assume that the job
180+
# doesn't actually exist and try again. We can't add 404 to the default
181+
# job_retry because that happens for errors like "this table does not
182+
# exist", which probably won't resolve with a retry.
184183
if isinstance(exc, core_exceptions.NotFound):
185184
return True
186185

187186
# Reference the original job_retry to avoid recursion.
188187
return job_retry._predicate(exc)
189188

190189
do_query_retry = job_retry.with_predicate(do_query_predicate)
191-
future = do_query_retry(do_query)()
190+
do_query = do_query_retry(do_query)
191+
192+
future = do_query()
192193

193194
# The future might be in a failed state now, but if it's
194195
# unrecoverable, we'll find out when we ask for it's result, at which

tests/unit/test_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5311,6 +5311,36 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_fails(self):
53115311
with pytest.raises(DataLoss, match="we lost your job, sorry"):
53125312
client.query("SELECT 1;", job_id=None)
53135313

5314+
def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_fails_no_retries(self):
5315+
from google.api_core.exceptions import Conflict
5316+
from google.api_core.exceptions import DataLoss
5317+
from google.cloud.bigquery.job import QueryJob
5318+
5319+
creds = _make_credentials()
5320+
http = object()
5321+
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
5322+
5323+
job_create_error = Conflict("Job already exists.")
5324+
job_begin_patcher = mock.patch.object(
5325+
QueryJob, "_begin", side_effect=job_create_error
5326+
)
5327+
get_job_patcher = mock.patch.object(
5328+
client, "get_job", side_effect=DataLoss("we lost your job, sorry")
5329+
)
5330+
5331+
with job_begin_patcher, get_job_patcher:
5332+
# If get job request fails but supposedly there does exist a job
5333+
# with this ID already, raise the exception explaining why we
5334+
# couldn't recover the job.
5335+
with pytest.raises(DataLoss, match="we lost your job, sorry"):
5336+
client.query(
5337+
"SELECT 1;",
5338+
job_id=None,
5339+
# Explicitly test with no retries to make sure those branches are covered.
5340+
retry=None,
5341+
job_retry=None,
5342+
)
5343+
53145344
def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404(self):
53155345
"""Regression test for https://github.com/googleapis/python-bigquery/issues/2134
53165346

0 commit comments

Comments
 (0)