Skip to content

Commit e9f27b3

Browse files
authored
Fix tests b0rken by 'google-resumable-media 0.4.0' release. (googleapis#9204)
1 parent adfc6d3 commit e9f27b3

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

storage/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
dependencies = [
3232
"google-auth >= 1.2.0",
3333
"google-cloud-core >= 1.0.3, < 2.0dev",
34-
"google-resumable-media >= 0.3.1",
34+
"google-resumable-media >= 0.4.0",
3535
]
3636
extras = {}
3737

storage/tests/unit/test_blob.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,15 @@ def test__get_download_url_on_the_fly_with_kms_key_name(self):
755755
@staticmethod
756756
def _mock_requests_response(status_code, headers, content=b"", stream=False):
757757
import requests
758+
from urllib3.response import HTTPResponse
758759

759760
response = requests.Response()
760761
response.status_code = status_code
761762
response.headers.update(headers)
762763
if stream:
763-
raw = io.BytesIO(content)
764+
raw = mock.create_autospec(HTTPResponse, instance=True)
764765
raw.headers = headers
766+
raw.stream.return_value = iter([content])
765767
response.raw = raw
766768
response._content = False
767769
else:
@@ -814,7 +816,12 @@ def _check_session_mocks(self, client, transport, expected_url, headers=None):
814816
headers["range"] = "bytes=3-5"
815817
headers["accept-encoding"] = "gzip"
816818
call = mock.call(
817-
"GET", expected_url, data=None, headers=headers, timeout=mock.ANY
819+
"GET",
820+
expected_url,
821+
data=None,
822+
headers=headers,
823+
stream=True,
824+
timeout=mock.ANY,
818825
)
819826
self.assertEqual(transport.request.mock_calls, [call, call])
820827

@@ -909,7 +916,12 @@ def test__do_download_chunked(self):
909916
# ``headers`` was modified (in place) once for each API call.
910917
self.assertEqual(headers, {"range": "bytes=3-5"})
911918
call = mock.call(
912-
"GET", download_url, data=None, headers=headers, timeout=mock.ANY
919+
"GET",
920+
download_url,
921+
data=None,
922+
headers=headers,
923+
stream=True,
924+
timeout=mock.ANY,
913925
)
914926
self.assertEqual(transport.request.mock_calls, [call, call])
915927

@@ -937,7 +949,12 @@ def test__do_download_chunked_with_range(self):
937949
# ``headers`` was modified (in place) once for each API call.
938950
self.assertEqual(headers, {"range": "bytes=3-4"})
939951
call = mock.call(
940-
"GET", download_url, data=None, headers=headers, timeout=mock.ANY
952+
"GET",
953+
download_url,
954+
data=None,
955+
headers=headers,
956+
stream=True,
957+
timeout=mock.ANY,
941958
)
942959
self.assertEqual(transport.request.mock_calls, [call, call])
943960

@@ -1086,32 +1103,27 @@ def test_download_to_filename_wo_updated(self):
10861103
self._download_to_filename_helper()
10871104

10881105
def test_download_to_filename_corrupted(self):
1106+
from urllib3.response import HTTPResponse
10891107
from google.resumable_media import DataCorruption
10901108
from google.resumable_media.requests.download import _CHECKSUM_MISMATCH
10911109

10921110
blob_name = "blob-name"
10931111
transport = mock.Mock(spec=["request"])
10941112
empty_hash = base64.b64encode(hashlib.md5(b"").digest()).decode(u"utf-8")
10951113
headers = {"x-goog-hash": "md5=" + empty_hash}
1096-
mock_raw = mock.Mock(headers=headers, spec=["headers"])
1114+
chunks = (b"noms1", b"coooookies2")
1115+
mock_raw = mock.create_autospec(HTTPResponse, instance=True)
1116+
mock_raw.headers = headers
1117+
mock_raw.stream.return_value = iter(chunks)
10971118
response = mock.MagicMock(
10981119
headers=headers,
10991120
status_code=http_client.OK,
11001121
raw=mock_raw,
1101-
spec=[
1102-
"__enter__",
1103-
"__exit__",
1104-
"headers",
1105-
"iter_content",
1106-
"status_code",
1107-
"raw",
1108-
],
1122+
spec=["__enter__", "__exit__", "headers", "status_code", "raw"],
11091123
)
11101124
# i.e. context manager returns ``self``.
11111125
response.__enter__.return_value = response
11121126
response.__exit__.return_value = None
1113-
chunks = (b"noms1", b"coooookies2")
1114-
response.iter_content.return_value = iter(chunks)
11151127

11161128
transport.request.return_value = response
11171129
# Create a fake client/bucket and use them in the Blob() constructor.
@@ -1146,9 +1158,7 @@ def test_download_to_filename_corrupted(self):
11461158
# Check the mocks.
11471159
response.__enter__.assert_called_once_with()
11481160
response.__exit__.assert_called_once_with(None, None, None)
1149-
response.iter_content.assert_called_once_with(
1150-
chunk_size=8192, decode_unicode=False
1151-
)
1161+
mock_raw.stream.assert_called_once_with(8192, decode_content=False)
11521162
transport.request.assert_called_once_with(
11531163
"GET",
11541164
media_link,

0 commit comments

Comments
 (0)