Skip to content

Commit 5797001

Browse files
committed
coverage passes
1 parent ba24a27 commit 5797001

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

google/cloud/storage/_media/requests/download.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def _write_to_stream(self, response):
140140
)
141141
raise DataCorruption(response, msg)
142142

143+
143144
def consume(
144145
self,
145146
transport,

tests/resumable_media/unit/requests/test_download.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ def test__write_to_stream_no_hash_check(self):
5252
chunk_size=_request_helpers._SINGLE_GET_CHUNK_SIZE, decode_unicode=False
5353
)
5454

55+
def test__write_to_stream_empty_chunks(self):
56+
stream = io.BytesIO()
57+
download = download_mod.Download(EXAMPLE_URL, stream=stream)
58+
59+
response = _mock_response(chunks=[], headers={})
60+
61+
ret_val = download._write_to_stream(response)
62+
assert ret_val is None
63+
64+
assert stream.getvalue() == b""
65+
assert download._bytes_downloaded == 0
66+
67+
# Check mocks.
68+
response.__enter__.assert_called_once_with()
69+
response.__exit__.assert_called_once_with(None, None, None)
70+
response.iter_content.assert_called_once_with(
71+
chunk_size=_request_helpers._SINGLE_GET_CHUNK_SIZE, decode_unicode=False
72+
)
73+
5574
@pytest.mark.parametrize("checksum", ["md5", "crc32c", None])
5675
def test__write_to_stream_with_hash_check_success(self, checksum):
5776
stream = io.BytesIO()
@@ -1156,11 +1175,11 @@ def test_decompress(self):
11561175
md5_hash.update.assert_called_once_with(data)
11571176

11581177

1159-
def _mock_response(status_code=http.client.OK, chunks=(), headers=None):
1178+
def _mock_response(status_code=http.client.OK, chunks=None, headers=None):
11601179
if headers is None:
11611180
headers = {}
11621181

1163-
if chunks:
1182+
if chunks is not None:
11641183
mock_raw = mock.Mock(headers=headers, spec=["headers"])
11651184
response = mock.MagicMock(
11661185
headers=headers,

tests/unit/test_exceptions.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from importlib import reload
16+
from unittest.mock import Mock
17+
import sys
18+
19+
def test_exceptions_imports_correctly_in_base_case():
20+
try:
21+
mock = Mock(spec=[])
22+
sys.modules['google.resumable_media'] = mock
23+
24+
from google.cloud.storage import exceptions
25+
reload(exceptions)
26+
invalid_response = exceptions.InvalidResponse(Mock())
27+
ir_base_names = [base.__name__ for base in invalid_response.__class__.__bases__]
28+
assert ir_base_names == ["Exception"]
29+
30+
data_corruption = exceptions.DataCorruption(Mock())
31+
dc_base_names = [base.__name__ for base in data_corruption.__class__.__bases__]
32+
assert dc_base_names == ["Exception"]
33+
finally:
34+
del sys.modules['google.resumable_media']
35+
reload(exceptions)
36+
37+
def test_exceptions_imports_correctly_in_resumable_media_installed_case():
38+
try:
39+
mock = Mock(spec=["InvalidResponse", "DataCorruption"])
40+
41+
class InvalidResponse(Exception):
42+
def __init__(self, response, *args):
43+
super().__init__(*args)
44+
self.response = response
45+
46+
class DataCorruption(Exception):
47+
def __init__(self, response, *args):
48+
super().__init__(*args)
49+
self.response = response
50+
51+
mock.InvalidResponse = InvalidResponse
52+
mock.DataCorruption = DataCorruption
53+
54+
sys.modules['google.resumable_media'] = mock
55+
56+
from google.cloud.storage import exceptions
57+
reload(exceptions)
58+
invalid_response = exceptions.InvalidResponse(Mock())
59+
ir_base_names = [base.__name__ for base in invalid_response.__class__.__bases__]
60+
assert ir_base_names == ["InvalidResponse"]
61+
62+
data_corruption = exceptions.DataCorruption(Mock())
63+
dc_base_names = [base.__name__ for base in data_corruption.__class__.__bases__]
64+
assert dc_base_names == ["DataCorruption"]
65+
finally:
66+
del sys.modules['google.resumable_media']
67+
reload(exceptions)

0 commit comments

Comments
 (0)