Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 2103462

Browse files
committed
refactor: use 'Client._get_path' in 'BucketNotification.reload'
Toward #38.
1 parent 7e55b29 commit 2103462

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

google/cloud/storage/notification.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,8 @@ def reload(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY):
377377
if self.bucket.user_project is not None:
378378
query_params["userProject"] = self.bucket.user_project
379379

380-
response = client._connection.api_request(
381-
method="GET",
382-
path=self.path,
383-
query_params=query_params,
384-
timeout=timeout,
385-
retry=retry,
380+
response = client._get_path(
381+
self.path, query_params=query_params, timeout=timeout, retry=retry,
386382
)
387383
self._set_properties(response)
388384

tests/unit/test_notification.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -387,52 +387,58 @@ def test_exists_hit_w_explicit_w_user_project(self):
387387
)
388388

389389
def test_reload_wo_notification_id(self):
390-
client = self._make_client()
390+
client = mock.Mock(spec=["_get_path", "project"])
391+
client.project = self.BUCKET_PROJECT
391392
bucket = self._make_bucket(client)
392393
notification = self._make_one(bucket, self.TOPIC_NAME)
393394

394395
with self.assertRaises(ValueError):
395396
notification.reload()
396397

397-
def test_reload_miss(self):
398+
client._get_path.assert_not_called()
399+
400+
def test_reload_miss_w_defaults(self):
398401
from google.cloud.exceptions import NotFound
399402

400-
client = self._make_client()
403+
client = mock.Mock(spec=["_get_path", "project"])
404+
client._get_path.side_effect = NotFound("testing")
405+
client.project = self.BUCKET_PROJECT
401406
bucket = self._make_bucket(client)
402407
notification = self._make_one(bucket, self.TOPIC_NAME)
403408
notification._properties["id"] = self.NOTIFICATION_ID
404-
api_request = client._connection.api_request
405-
api_request.side_effect = NotFound("testing")
406409

407410
with self.assertRaises(NotFound):
408-
notification.reload(timeout=42)
411+
notification.reload()
409412

410-
api_request.assert_called_once_with(
411-
method="GET",
412-
path=self.NOTIFICATION_PATH,
413-
query_params={},
414-
timeout=42,
413+
expected_query_params = {}
414+
client._get_path.assert_called_once_with(
415+
self.NOTIFICATION_PATH,
416+
query_params=expected_query_params,
417+
timeout=self._get_default_timeout(),
415418
retry=DEFAULT_RETRY,
416419
)
417420

418-
def test_reload_hit(self):
421+
def test_reload_hit_w_explicit_w_user_project(self):
419422
from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT
420423

421-
USER_PROJECT = "user-project-123"
422-
client = self._make_client()
423-
bucket = self._make_bucket(client, user_project=USER_PROJECT)
424-
notification = self._make_one(bucket, self.TOPIC_NAME)
425-
notification._properties["id"] = self.NOTIFICATION_ID
426-
api_request = client._connection.api_request
427-
api_request.return_value = {
424+
user_project = "user-project-123"
425+
api_response = {
428426
"topic": self.TOPIC_REF,
429427
"id": self.NOTIFICATION_ID,
430428
"etag": self.ETAG,
431429
"selfLink": self.SELF_LINK,
432430
"payload_format": NONE_PAYLOAD_FORMAT,
433431
}
432+
client = mock.Mock(spec=["_get_path", "project"])
433+
client._get_path.return_value = api_response
434+
client.project = self.BUCKET_PROJECT
435+
bucket = self._make_bucket(client, user_project=user_project)
436+
notification = self._make_one(bucket, self.TOPIC_NAME)
437+
notification._properties["id"] = self.NOTIFICATION_ID
438+
timeout = 42
439+
retry = mock.Mock(spec=[])
434440

435-
notification.reload(client=client)
441+
notification.reload(client=client, timeout=timeout, retry=retry)
436442

437443
self.assertEqual(notification.etag, self.ETAG)
438444
self.assertEqual(notification.self_link, self.SELF_LINK)
@@ -441,12 +447,12 @@ def test_reload_hit(self):
441447
self.assertIsNone(notification.blob_name_prefix)
442448
self.assertEqual(notification.payload_format, NONE_PAYLOAD_FORMAT)
443449

444-
api_request.assert_called_once_with(
445-
method="GET",
446-
path=self.NOTIFICATION_PATH,
447-
query_params={"userProject": USER_PROJECT},
448-
timeout=self._get_default_timeout(),
449-
retry=DEFAULT_RETRY,
450+
expected_query_params = {"userProject": user_project}
451+
client._get_path.assert_called_once_with(
452+
self.NOTIFICATION_PATH,
453+
query_params=expected_query_params,
454+
timeout=timeout,
455+
retry=retry,
450456
)
451457

452458
def test_delete_wo_notification_id(self):

0 commit comments

Comments
 (0)