Skip to content

Commit

Permalink
tests: suppress deprecation warnings in unit test output (#499)
Browse files Browse the repository at this point in the history
Test hygeine: avoid importing module-under-test at module scope

Closes #498.
  • Loading branch information
tseaver committed Jul 13, 2021
1 parent a8aaaf6 commit c787d12
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 70 deletions.
11 changes: 6 additions & 5 deletions google/cloud/storage/blob.py
Expand Up @@ -36,8 +36,8 @@
import os
import re
import warnings
import six

import six
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import quote
from six.moves.urllib.parse import urlencode
Expand Down Expand Up @@ -135,6 +135,10 @@
_COMPOSE_IF_SOURCE_GENERATION_MISMATCH_ERROR = (
"'if_source_generation_match' length must be the same as 'sources' length"
)
_DOWNLOAD_AS_STRING_DEPRECATED = (
"Blob.download_as_string() is deprecated and will be removed in future. "
"Use Blob.download_as_bytes() instead.",
)


_DEFAULT_CHUNKSIZE = 104857600 # 1024 * 1024 B * 100 = 100 MB
Expand Down Expand Up @@ -1514,10 +1518,7 @@ def download_as_string(
:raises: :class:`google.cloud.exceptions.NotFound`
"""
warnings.warn(
"Blob.download_as_string() is deprecated and will be removed in future."
"Use Blob.download_as_bytes() instead.",
PendingDeprecationWarning,
stacklevel=1,
_DOWNLOAD_AS_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2
)
return self.download_as_bytes(
client=client,
Expand Down
66 changes: 49 additions & 17 deletions tests/unit/test_blob.py
Expand Up @@ -2068,6 +2068,8 @@ def test_download_as_text_w_non_ascii_wo_explicit_encoding_w_charset(self):

@mock.patch("warnings.warn")
def test_download_as_string(self, mock_warn):
from google.cloud.storage.blob import _DOWNLOAD_AS_STRING_DEPRECATED

MEDIA_LINK = "http://example.com/media/"

client = self._make_client()
Expand Down Expand Up @@ -2096,14 +2098,14 @@ def test_download_as_string(self, mock_warn):
retry=DEFAULT_RETRY,
)

mock_warn.assert_called_with(
"Blob.download_as_string() is deprecated and will be removed in future."
"Use Blob.download_as_bytes() instead.",
PendingDeprecationWarning,
stacklevel=1,
mock_warn.assert_called_once_with(
_DOWNLOAD_AS_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2,
)

def test_download_as_string_no_retry(self):
@mock.patch("warnings.warn")
def test_download_as_string_no_retry(self, mock_warn):
from google.cloud.storage.blob import _DOWNLOAD_AS_STRING_DEPRECATED

MEDIA_LINK = "http://example.com/media/"

client = self._make_client()
Expand Down Expand Up @@ -2132,6 +2134,10 @@ def test_download_as_string_no_retry(self):
retry=None,
)

mock_warn.assert_called_once_with(
_DOWNLOAD_AS_STRING_DEPRECATED, PendingDeprecationWarning, stacklevel=2,
)

def test__get_content_type_explicit(self):
blob = self._make_one(u"blob-name", bucket=None)

Expand Down Expand Up @@ -2718,7 +2724,7 @@ def test__initiate_resumable_upload_with_extra_headers(self):
def test__initiate_resumable_upload_with_retry(self):
self._initiate_resumable_helper(retry=DEFAULT_RETRY)

def test__initiate_resumable_upload_with_num_retries(self):
def test__initiate_resumable_upload_w_num_retries(self):
self._initiate_resumable_helper(num_retries=11)

def test__initiate_resumable_upload_with_retry_conflict(self):
Expand Down Expand Up @@ -2983,7 +2989,7 @@ def test__do_resumable_upload_with_size(self):
def test__do_resumable_upload_with_retry(self):
self._do_resumable_helper(retry=DEFAULT_RETRY)

def test__do_resumable_upload_with_num_retries(self):
def test__do_resumable_upload_w_num_retries(self):
self._do_resumable_helper(num_retries=8)

def test__do_resumable_upload_with_retry_conflict(self):
Expand Down Expand Up @@ -3129,7 +3135,7 @@ def test__do_upload_uses_resumable_w_custom_timeout(self):
def test__do_upload_with_retry(self):
self._do_upload_helper(retry=DEFAULT_RETRY)

def test__do_upload_with_num_retries(self):
def test__do_upload_w_num_retries(self):
self._do_upload_helper(num_retries=2)

def test__do_upload_with_conditional_retry_success(self):
Expand Down Expand Up @@ -3199,26 +3205,32 @@ def test_upload_from_file_success(self):
stream = self._upload_from_file_helper(predefined_acl="private")
assert stream.tell() == 2

@mock.patch("warnings.warn")
def test_upload_from_file_with_retries(self, mock_warn):
def test_upload_from_file_with_retry(self):
self._upload_from_file_helper(retry=DEFAULT_RETRY)

@mock.patch("warnings.warn")
def test_upload_from_file_with_num_retries(self, mock_warn):
from google.cloud.storage import blob as blob_module
def test_upload_from_file_w_num_retries(self, mock_warn):
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE

self._upload_from_file_helper(num_retries=2)

mock_warn.assert_called_once_with(
blob_module._NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
)

@mock.patch("warnings.warn")
def test_upload_from_file_with_retry_conflict(self, mock_warn):
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE

# Special case here: in a conflict this method should NOT raise an error
# as that's handled further downstream. It should pass both options
# through.
self._upload_from_file_helper(retry=DEFAULT_RETRY, num_retries=2)

mock_warn.assert_called_once_with(
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
)

def test_upload_from_file_with_rewind(self):
stream = self._upload_from_file_helper(rewind=True)
assert stream.tell() == 0
Expand Down Expand Up @@ -3342,8 +3354,10 @@ def test_upload_from_filename_with_retry(self):
self.assertEqual(stream.mode, "rb")
self.assertEqual(stream.name, temp.name)

def test_upload_from_filename_with_num_retries(self):
@mock.patch("warnings.warn")
def test_upload_from_filename_w_num_retries(self, mock_warn):
from google.cloud._testing import _NamedTemporaryFile
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE

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

mock_warn.assert_called_once_with(
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
)

def test_upload_from_filename_w_custom_timeout(self):
from google.cloud._testing import _NamedTemporaryFile

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

def test_upload_from_string_w_text_w_num_retries(self):
@mock.patch("warnings.warn")
def test_upload_from_string_with_num_retries(self, mock_warn):
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE

data = u"\N{snowman} \N{sailboat}"
self._upload_from_string_helper(data, num_retries=2)

mock_warn.assert_called_once_with(
_NUM_RETRIES_MESSAGE, DeprecationWarning, stacklevel=2,
)

def _create_resumable_upload_session_helper(
self,
origin=None,
Expand Down Expand Up @@ -4303,7 +4328,10 @@ def test_compose_w_if_generation_match_list_w_warning(self, mock_warn):
_COMPOSE_IF_GENERATION_LIST_DEPRECATED, DeprecationWarning, stacklevel=2,
)

def test_compose_w_if_generation_match_and_if_s_generation_match(self):
@mock.patch("warnings.warn")
def test_compose_w_if_generation_match_and_if_s_generation_match(self, mock_warn):
from google.cloud.storage.blob import _COMPOSE_IF_GENERATION_LIST_DEPRECATED

source_1_name = "source-1"
source_2_name = "source-2"
destination_name = "destination"
Expand All @@ -4324,6 +4352,10 @@ def test_compose_w_if_generation_match_and_if_s_generation_match(self):

client._post_resource.assert_not_called()

mock_warn.assert_called_with(
_COMPOSE_IF_GENERATION_LIST_DEPRECATED, DeprecationWarning, stacklevel=2,
)

@mock.patch("warnings.warn")
def test_compose_w_if_metageneration_match_list_w_warning(self, mock_warn):
from google.cloud.storage.blob import _COMPOSE_IF_METAGENERATION_LIST_DEPRECATED
Expand Down

0 comments on commit c787d12

Please sign in to comment.