Skip to content

Commit c787d12

Browse files
authored
tests: suppress deprecation warnings in unit test output (#499)
Test hygeine: avoid importing module-under-test at module scope Closes #498.
1 parent a8aaaf6 commit c787d12

File tree

3 files changed

+148
-70
lines changed

3 files changed

+148
-70
lines changed

google/cloud/storage/blob.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import os
3737
import re
3838
import warnings
39-
import six
4039

40+
import six
4141
from six.moves.urllib.parse import parse_qsl
4242
from six.moves.urllib.parse import quote
4343
from six.moves.urllib.parse import urlencode
@@ -135,6 +135,10 @@
135135
_COMPOSE_IF_SOURCE_GENERATION_MISMATCH_ERROR = (
136136
"'if_source_generation_match' length must be the same as 'sources' length"
137137
)
138+
_DOWNLOAD_AS_STRING_DEPRECATED = (
139+
"Blob.download_as_string() is deprecated and will be removed in future. "
140+
"Use Blob.download_as_bytes() instead.",
141+
)
138142

139143

140144
_DEFAULT_CHUNKSIZE = 104857600 # 1024 * 1024 B * 100 = 100 MB
@@ -1514,10 +1518,7 @@ def download_as_string(
15141518
:raises: :class:`google.cloud.exceptions.NotFound`
15151519
"""
15161520
warnings.warn(
1517-
"Blob.download_as_string() is deprecated and will be removed in future."
1518-
"Use Blob.download_as_bytes() instead.",
1519-
PendingDeprecationWarning,
1520-
stacklevel=1,
1521+
_DOWNLOAD_AS_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2
15211522
)
15221523
return self.download_as_bytes(
15231524
client=client,

tests/unit/test_blob.py

+49-17
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,8 @@ def test_download_as_text_w_non_ascii_wo_explicit_encoding_w_charset(self):
20682068

20692069
@mock.patch("warnings.warn")
20702070
def test_download_as_string(self, mock_warn):
2071+
from google.cloud.storage.blob import _DOWNLOAD_AS_STRING_DEPRECATED
2072+
20712073
MEDIA_LINK = "http://example.com/media/"
20722074

20732075
client = self._make_client()
@@ -2096,14 +2098,14 @@ def test_download_as_string(self, mock_warn):
20962098
retry=DEFAULT_RETRY,
20972099
)
20982100

2099-
mock_warn.assert_called_with(
2100-
"Blob.download_as_string() is deprecated and will be removed in future."
2101-
"Use Blob.download_as_bytes() instead.",
2102-
PendingDeprecationWarning,
2103-
stacklevel=1,
2101+
mock_warn.assert_called_once_with(
2102+
_DOWNLOAD_AS_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2,
21042103
)
21052104

2106-
def test_download_as_string_no_retry(self):
2105+
@mock.patch("warnings.warn")
2106+
def test_download_as_string_no_retry(self, mock_warn):
2107+
from google.cloud.storage.blob import _DOWNLOAD_AS_STRING_DEPRECATED
2108+
21072109
MEDIA_LINK = "http://example.com/media/"
21082110

21092111
client = self._make_client()
@@ -2132,6 +2134,10 @@ def test_download_as_string_no_retry(self):
21322134
retry=None,
21332135
)
21342136

2137+
mock_warn.assert_called_once_with(
2138+
_DOWNLOAD_AS_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2,
2139+
)
2140+
21352141
def test__get_content_type_explicit(self):
21362142
blob = self._make_one(u"blob-name", bucket=None)
21372143

@@ -2718,7 +2724,7 @@ def test__initiate_resumable_upload_with_extra_headers(self):
27182724
def test__initiate_resumable_upload_with_retry(self):
27192725
self._initiate_resumable_helper(retry=DEFAULT_RETRY)
27202726

2721-
def test__initiate_resumable_upload_with_num_retries(self):
2727+
def test__initiate_resumable_upload_w_num_retries(self):
27222728
self._initiate_resumable_helper(num_retries=11)
27232729

27242730
def test__initiate_resumable_upload_with_retry_conflict(self):
@@ -2983,7 +2989,7 @@ def test__do_resumable_upload_with_size(self):
29832989
def test__do_resumable_upload_with_retry(self):
29842990
self._do_resumable_helper(retry=DEFAULT_RETRY)
29852991

2986-
def test__do_resumable_upload_with_num_retries(self):
2992+
def test__do_resumable_upload_w_num_retries(self):
29872993
self._do_resumable_helper(num_retries=8)
29882994

29892995
def test__do_resumable_upload_with_retry_conflict(self):
@@ -3129,7 +3135,7 @@ def test__do_upload_uses_resumable_w_custom_timeout(self):
31293135
def test__do_upload_with_retry(self):
31303136
self._do_upload_helper(retry=DEFAULT_RETRY)
31313137

3132-
def test__do_upload_with_num_retries(self):
3138+
def test__do_upload_w_num_retries(self):
31333139
self._do_upload_helper(num_retries=2)
31343140

31353141
def test__do_upload_with_conditional_retry_success(self):
@@ -3199,26 +3205,32 @@ def test_upload_from_file_success(self):
31993205
stream = self._upload_from_file_helper(predefined_acl="private")
32003206
assert stream.tell() == 2
32013207

3202-
@mock.patch("warnings.warn")
3203-
def test_upload_from_file_with_retries(self, mock_warn):
3208+
def test_upload_from_file_with_retry(self):
32043209
self._upload_from_file_helper(retry=DEFAULT_RETRY)
32053210

32063211
@mock.patch("warnings.warn")
3207-
def test_upload_from_file_with_num_retries(self, mock_warn):
3208-
from google.cloud.storage import blob as blob_module
3212+
def test_upload_from_file_w_num_retries(self, mock_warn):
3213+
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE
32093214

32103215
self._upload_from_file_helper(num_retries=2)
3216+
32113217
mock_warn.assert_called_once_with(
3212-
blob_module._NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2
3218+
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
32133219
)
32143220

32153221
@mock.patch("warnings.warn")
32163222
def test_upload_from_file_with_retry_conflict(self, mock_warn):
3223+
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE
3224+
32173225
# Special case here: in a conflict this method should NOT raise an error
32183226
# as that's handled further downstream. It should pass both options
32193227
# through.
32203228
self._upload_from_file_helper(retry=DEFAULT_RETRY, num_retries=2)
32213229

3230+
mock_warn.assert_called_once_with(
3231+
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
3232+
)
3233+
32223234
def test_upload_from_file_with_rewind(self):
32233235
stream = self._upload_from_file_helper(rewind=True)
32243236
assert stream.tell() == 0
@@ -3342,8 +3354,10 @@ def test_upload_from_filename_with_retry(self):
33423354
self.assertEqual(stream.mode, "rb")
33433355
self.assertEqual(stream.name, temp.name)
33443356

3345-
def test_upload_from_filename_with_num_retries(self):
3357+
@mock.patch("warnings.warn")
3358+
def test_upload_from_filename_w_num_retries(self, mock_warn):
33463359
from google.cloud._testing import _NamedTemporaryFile
3360+
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE
33473361

33483362
blob = self._make_one("blob-name", bucket=None)
33493363
# Mock low-level upload helper on blob (it is tested elsewhere).
@@ -3375,6 +3389,10 @@ def test_upload_from_filename_with_num_retries(self):
33753389
self.assertEqual(stream.mode, "rb")
33763390
self.assertEqual(stream.name, temp.name)
33773391

3392+
mock_warn.assert_called_once_with(
3393+
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
3394+
)
3395+
33783396
def test_upload_from_filename_w_custom_timeout(self):
33793397
from google.cloud._testing import _NamedTemporaryFile
33803398

@@ -3453,10 +3471,17 @@ def test_upload_from_string_w_text_w_retry(self):
34533471
data = u"\N{snowman} \N{sailboat}"
34543472
self._upload_from_string_helper(data, retry=DEFAULT_RETRY)
34553473

3456-
def test_upload_from_string_w_text_w_num_retries(self):
3474+
@mock.patch("warnings.warn")
3475+
def test_upload_from_string_with_num_retries(self, mock_warn):
3476+
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE
3477+
34573478
data = u"\N{snowman} \N{sailboat}"
34583479
self._upload_from_string_helper(data, num_retries=2)
34593480

3481+
mock_warn.assert_called_once_with(
3482+
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
3483+
)
3484+
34603485
def _create_resumable_upload_session_helper(
34613486
self,
34623487
origin=None,
@@ -4303,7 +4328,10 @@ def test_compose_w_if_generation_match_list_w_warning(self, mock_warn):
43034328
_COMPOSE_IF_GENERATION_LIST_DEPRECATED, DeprecationWarning, stacklevel=2,
43044329
)
43054330

4306-
def test_compose_w_if_generation_match_and_if_s_generation_match(self):
4331+
@mock.patch("warnings.warn")
4332+
def test_compose_w_if_generation_match_and_if_s_generation_match(self, mock_warn):
4333+
from google.cloud.storage.blob import _COMPOSE_IF_GENERATION_LIST_DEPRECATED
4334+
43074335
source_1_name = "source-1"
43084336
source_2_name = "source-2"
43094337
destination_name = "destination"
@@ -4324,6 +4352,10 @@ def test_compose_w_if_generation_match_and_if_s_generation_match(self):
43244352

43254353
client._post_resource.assert_not_called()
43264354

4355+
mock_warn.assert_called_with(
4356+
_COMPOSE_IF_GENERATION_LIST_DEPRECATED, DeprecationWarning, stacklevel=2,
4357+
)
4358+
43274359
@mock.patch("warnings.warn")
43284360
def test_compose_w_if_metageneration_match_list_w_warning(self, mock_warn):
43294361
from google.cloud.storage.blob import _COMPOSE_IF_METAGENERATION_LIST_DEPRECATED

0 commit comments

Comments
 (0)