Skip to content

Commit 20afb8e

Browse files
committed
coverage and test
1 parent a820538 commit 20afb8e

File tree

8 files changed

+282
-56
lines changed

8 files changed

+282
-56
lines changed

google/cloud/storage/_helpers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"object, or None, instead."
5353
)
5454

55+
5556
def _get_storage_host():
5657
return os.environ.get(STORAGE_EMULATOR_ENV_VAR, _DEFAULT_STORAGE_HOST)
5758

@@ -593,9 +594,13 @@ def _api_core_retry_to_resumable_media_retry(retry, num_retries=None):
593594
raise ValueError("num_retries and retry arguments are mutually exclusive")
594595

595596
elif retry is not None:
596-
return resumable_media.RetryStrategy(max_sleep=retry._maximum, max_cumulative_retry=retry._deadline, initial_delay=retry._initial, multiplier=retry._multiplier)
597+
return resumable_media.RetryStrategy(
598+
max_sleep=retry._maximum,
599+
max_cumulative_retry=retry._deadline,
600+
initial_delay=retry._initial,
601+
multiplier=retry._multiplier,
602+
)
597603
elif num_retries is not None:
598604
return resumable_media.RetryStrategy(max_retries=num_retries)
599605
else:
600606
return resumable_media.RetryStrategy(max_retries=0)
601-

google/cloud/storage/blob.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,9 @@ def _do_multipart_upload(
17541754
upload_url = _add_query_parameters(base_url, name_value_pairs)
17551755
upload = MultipartUpload(upload_url, headers=headers, checksum=checksum)
17561756

1757-
upload._retry_strategy = _api_core_retry_to_resumable_media_retry(retry, num_retries)
1757+
upload._retry_strategy = _api_core_retry_to_resumable_media_retry(
1758+
retry, num_retries
1759+
)
17581760

17591761
response = upload.transmit(
17601762
transport, data, object_metadata, content_type, timeout=timeout
@@ -1936,7 +1938,9 @@ def _initiate_resumable_upload(
19361938
upload_url, chunk_size, headers=headers, checksum=checksum
19371939
)
19381940

1939-
upload._retry_strategy = _api_core_retry_to_resumable_media_retry(retry, num_retries)
1941+
upload._retry_strategy = _api_core_retry_to_resumable_media_retry(
1942+
retry, num_retries
1943+
)
19401944

19411945
upload.initiate(
19421946
transport,
@@ -2092,7 +2096,7 @@ def _do_upload(
20922096
if_metageneration_not_match,
20932097
timeout=_DEFAULT_TIMEOUT,
20942098
checksum=None,
2095-
retry=None
2099+
retry=None,
20962100
):
20972101
"""Determine an upload strategy and then perform the upload.
20982102
@@ -2192,7 +2196,10 @@ def _do_upload(
21922196
# arguments into query_params dictionaries. Media operations work
21932197
# differently, so here we make a "fake" query_params to feed to the
21942198
# ConditionalRetryPolicy.
2195-
query_params = {"ifGenerationMatch": if_generation_match, "ifMetagenerationMatch": if_metageneration_match}
2199+
query_params = {
2200+
"ifGenerationMatch": if_generation_match,
2201+
"ifMetagenerationMatch": if_metageneration_match,
2202+
}
21962203
retry = retry.get_retry_policy_if_conditions_met(query_params=query_params)
21972204

21982205
if size is not None and size <= _MAX_MULTIPART_SIZE:
@@ -2395,7 +2402,7 @@ def upload_from_file(
23952402
if_metageneration_not_match,
23962403
timeout=timeout,
23972404
checksum=checksum,
2398-
retry=retry
2405+
retry=retry,
23992406
)
24002407
self._set_properties(created_json)
24012408
except resumable_media.InvalidResponse as exc:
@@ -3524,7 +3531,7 @@ def open(
35243531
"retry". For uploads only, the following additional arguments are
35253532
supported: "content_type", "num_retries", "predefined_acl",
35263533
"checksum". "num_retries" is supported for backwards-compatibility
3527-
reasons only; please use "retry" with a Retry object or
3534+
reasons only; please use "retry" with a Retry object or
35283535
ConditionalRetryPolicy instead.
35293536
35303537
:returns: A 'BlobReader' or 'BlobWriter' from

google/cloud/storage/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,10 @@ def download_blob_to_file(
711711
# arguments into query_params dictionaries. Media operations work
712712
# differently, so here we make a "fake" query_params to feed to the
713713
# ConditionalRetryPolicy.
714-
query_params = {"ifGenerationMatch": if_generation_match, "ifMetagenerationMatch": if_metageneration_match}
714+
query_params = {
715+
"ifGenerationMatch": if_generation_match,
716+
"ifMetagenerationMatch": if_metageneration_match,
717+
}
715718
retry = retry.get_retry_policy_if_conditions_met(query_params=query_params)
716719

717720
if not isinstance(blob_or_uri, Blob):

google/cloud/storage/fileio.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class BlobReader(io.BufferedIOBase):
6666
bytes than the chunk_size are requested, the remainder is buffered.
6767
The default is the chunk_size of the blob, or 40MiB.
6868
69-
70-
7169
:param download_kwargs: Keyword arguments to pass to the underlying API
7270
calls. The following arguments are supported: "if_generation_match",
7371
"if_generation_not_match", "if_metageneration_match",
@@ -86,7 +84,7 @@ def __init__(self, blob, chunk_size=None, retry=DEFAULT_RETRY, **download_kwargs
8684
self._pos = 0
8785
self._buffer = io.BytesIO()
8886
self._chunk_size = chunk_size or blob.chunk_size or DEFAULT_CHUNK_SIZE
89-
self._retry=retry
87+
self._retry = retry
9088
self._download_kwargs = download_kwargs
9189

9290
def read(self, size=-1):
@@ -216,7 +214,14 @@ class BlobWriter(io.BufferedIOBase):
216214
"num_retries", "predefined_acl", "checksum".
217215
"""
218216

219-
def __init__(self, blob, chunk_size=None, text_mode=False, retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED, **upload_kwargs):
217+
def __init__(
218+
self,
219+
blob,
220+
chunk_size=None,
221+
text_mode=False,
222+
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
223+
**upload_kwargs
224+
):
220225
for kwarg in upload_kwargs:
221226
if kwarg not in VALID_UPLOAD_KWARGS:
222227
raise ValueError(
@@ -291,7 +296,12 @@ def _initiate_upload(self):
291296
# arguments into query_params dictionaries. Media operations work
292297
# differently, so here we make a "fake" query_params to feed to the
293298
# ConditionalRetryPolicy.
294-
query_params = {"ifGenerationMatch": self._upload_kwargs.get("if_generation_match"), "ifMetagenerationMatch": self._upload_kwargs.get("if_metageneration_match")}
299+
query_params = {
300+
"ifGenerationMatch": self._upload_kwargs.get("if_generation_match"),
301+
"ifMetagenerationMatch": self._upload_kwargs.get(
302+
"if_metageneration_match"
303+
),
304+
}
295305
retry = retry.get_retry_policy_if_conditions_met(query_params=query_params)
296306

297307
self._upload_and_transport = self._blob._initiate_resumable_upload(

tests/unit/test__helpers.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,17 @@ def test_hostname_and_scheme(self):
558558

559559
class Test__api_core_retry_to_resumable_media_retry(unittest.TestCase):
560560
def test_conflict(self):
561-
from google.cloud.storage._helpers import _api_core_retry_to_resumable_media_retry
561+
from google.cloud.storage._helpers import (
562+
_api_core_retry_to_resumable_media_retry,
563+
)
562564

563565
with self.assertRaises(ValueError):
564566
_api_core_retry_to_resumable_media_retry(retry=DEFAULT_RETRY, num_retries=2)
565567

566568
def test_retry(self):
567-
from google.cloud.storage._helpers import _api_core_retry_to_resumable_media_retry
569+
from google.cloud.storage._helpers import (
570+
_api_core_retry_to_resumable_media_retry,
571+
)
568572

569573
retry_strategy = _api_core_retry_to_resumable_media_retry(retry=DEFAULT_RETRY)
570574
self.assertEqual(retry_strategy.max_sleep, DEFAULT_RETRY._maximum)
@@ -573,13 +577,19 @@ def test_retry(self):
573577
self.assertEqual(retry_strategy.multiplier, DEFAULT_RETRY._multiplier)
574578

575579
def test_num_retries(self):
576-
from google.cloud.storage._helpers import _api_core_retry_to_resumable_media_retry
580+
from google.cloud.storage._helpers import (
581+
_api_core_retry_to_resumable_media_retry,
582+
)
577583

578-
retry_strategy = _api_core_retry_to_resumable_media_retry(retry=None, num_retries=2)
584+
retry_strategy = _api_core_retry_to_resumable_media_retry(
585+
retry=None, num_retries=2
586+
)
579587
self.assertEqual(retry_strategy.max_retries, 2)
580588

581589
def test_none(self):
582-
from google.cloud.storage._helpers import _api_core_retry_to_resumable_media_retry
590+
from google.cloud.storage._helpers import (
591+
_api_core_retry_to_resumable_media_retry,
592+
)
583593

584594
retry_strategy = _api_core_retry_to_resumable_media_retry(retry=None)
585595
self.assertEqual(retry_strategy.max_retries, 0)

0 commit comments

Comments
 (0)