Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix unit tests, add test for helper
  • Loading branch information
IlyaFaer committed May 1, 2020
commit 4cbad43a685c5c2ef4418752b3a5c4ea781f9e38
20 changes: 10 additions & 10 deletions google/cloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,24 +404,24 @@ def _convert_to_timestamp(value):
return mtime


def _add_generation_match_parameters(name_value_pairs, **parameters):
def _add_generation_match_parameters(parameters, **match_parameters):
"""Add generation match parameters into the given parameters list.

:type name_value_pairs: list or dict
:param name_value_pairs: Parameters list or dict.
:type parameters: list or dict
:param parameters: Parameters list or dict.

:type parameters: dict
:param parameters: if*generation*match parameters to add.
:type match_parameters: dict
:param match_parameters: if*generation*match parameters to add.
"""
for snakecase_name, camelcase_name in _GENERATION_MATCH_PARAMETERS:
value = parameters.get(snakecase_name)
value = match_parameters.get(snakecase_name)

if value is not None:
if isinstance(name_value_pairs, list):
name_value_pairs.append((camelcase_name, value))
if isinstance(parameters, list):
parameters.append((camelcase_name, value))

elif isinstance(name_value_pairs, dict):
name_value_pairs[camelcase_name] = value
elif isinstance(parameters, dict):
parameters[camelcase_name] = value
Comment thread
andrewsg marked this conversation as resolved.


def _raise_for_more_than_one_none(**kwargs):
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,48 @@ def read(self, block_size):
self.assertEqual(MD5.hash_obj._blocks, [BYTES_TO_SIGN])


class Test__add_generation_match_parameters(unittest.TestCase):
def _call_fut(self, params, **match_params):
from google.cloud.storage._helpers import _add_generation_match_parameters

return _add_generation_match_parameters(params, **match_params)

def test_add_generation_match_parameters_list(self):
GENERATION_NUMBER = 9
METAGENERATION_NUMBER = 6
EXPECTED_PARAMS = [
("param1", "value1"),
("param2", "value2"),
("ifGenerationMatch", GENERATION_NUMBER),
("ifMetagenerationMatch", METAGENERATION_NUMBER),
]
params = [("param1", "value1"), ("param2", "value2")]
self._call_fut(
params,
if_generation_match=GENERATION_NUMBER,
if_metageneration_match=METAGENERATION_NUMBER,
)
self.assertEqual(params, EXPECTED_PARAMS)

def test_add_generation_match_parameters_dict(self):
GENERATION_NUMBER = 9
METAGENERATION_NUMBER = 6
EXPECTED_PARAMS = {
"param1": "value1",
"param2": "value2",
"ifGenerationMatch": GENERATION_NUMBER,
"ifMetagenerationMatch": METAGENERATION_NUMBER,
}

params = {"param1": "value1", "param2": "value2"}
self._call_fut(
params,
if_generation_match=GENERATION_NUMBER,
if_metageneration_match=METAGENERATION_NUMBER,
)
self.assertEqual(params, EXPECTED_PARAMS)


class _Connection(object):
def __init__(self, *responses):
self._responses = responses
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def test_get_bucket_with_metageneration_match(self):
client = self._make_one(project=PROJECT, credentials=CREDENTIALS)

BUCKET_NAME = "bucket-name"
URI = "/".join(
URI1 = "/".join(
[
client._connection.API_BASE_URL,
"storage",
Expand All @@ -455,6 +455,16 @@ def test_get_bucket_with_metageneration_match(self):
% (BUCKET_NAME, METAGENERATION_NUMBER),
]
)
URI2 = "/".join(
[
client._connection.API_BASE_URL,
"storage",
client._connection.API_VERSION,
"b",
"%s?ifMetagenerationMatch=%s&projection=noAcl"
% (BUCKET_NAME, METAGENERATION_NUMBER),
]
)
Comment thread
IlyaFaer marked this conversation as resolved.
data = {"name": BUCKET_NAME}
http = _make_requests_session([_make_json_response(data)])
client._http_internal = http
Expand All @@ -466,11 +476,13 @@ def test_get_bucket_with_metageneration_match(self):
self.assertEqual(bucket.name, BUCKET_NAME)
http.request.assert_called_once_with(
method="GET",
url=URI,
url=mock.ANY,
data=mock.ANY,
headers=mock.ANY,
timeout=self._get_default_timeout(),
)
_, kwargs = http.request.call_args
self.assertIn(kwargs.get("url"), (URI1, URI2))

def test_get_bucket_with_object_miss(self):
from google.cloud.exceptions import NotFound
Expand Down