diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py index f25b928c3..d465039ea 100644 --- a/google/cloud/storage/blob.py +++ b/google/cloud/storage/blob.py @@ -3719,9 +3719,6 @@ def update_storage_class( :param retry: (Optional) How to retry the RPC. See: :ref:`configuring_retries` """ - if new_class not in self.STORAGE_CLASSES: - raise ValueError(f"Invalid storage class: {new_class}") - # Update current blob's storage class prior to rewrite self._patch_property("storageClass", new_class) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index 6f133b923..98cbf892b 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -2654,8 +2654,6 @@ def storage_class(self, value): or :attr:`~google.cloud.storage.constants.DURABLE_REDUCED_AVAILABILITY_LEGACY_STORAGE_CLASS`, """ - if value not in self.STORAGE_CLASSES: - raise ValueError(f"Invalid storage class: {value}") self._patch_property("storageClass", value) @property diff --git a/google/cloud/storage/hmac_key.py b/google/cloud/storage/hmac_key.py index 944bc7f87..7f6de7eee 100644 --- a/google/cloud/storage/hmac_key.py +++ b/google/cloud/storage/hmac_key.py @@ -131,11 +131,6 @@ def state(self): @state.setter def state(self, value): - if value not in self._SETTABLE_STATES: - raise ValueError( - f"State may only be set to one of: {', '.join(self._SETTABLE_STATES)}" - ) - self._properties["state"] = value @property @@ -289,9 +284,6 @@ def delete(self, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY): :raises :class:`~google.api_core.exceptions.NotFound`: if the key does not exist on the back-end. """ - if self.state != self.INACTIVE_STATE: - raise ValueError("Cannot delete key if not in 'INACTIVE' state.") - qs_params = {} if self.user_project is not None: qs_params["userProject"] = self.user_project diff --git a/google/cloud/storage/notification.py b/google/cloud/storage/notification.py index f7e72e710..d9fa79ac6 100644 --- a/google/cloud/storage/notification.py +++ b/google/cloud/storage/notification.py @@ -306,7 +306,7 @@ def exists(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY): :raises ValueError: if the notification has no ID. """ if self.notification_id is None: - raise ValueError("Notification not intialized by server") + raise ValueError("Notification ID not set: set an explicit notification_id") client = self._require_client(client) @@ -352,7 +352,7 @@ def reload(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY): :raises ValueError: if the notification has no ID. """ if self.notification_id is None: - raise ValueError("Notification not intialized by server") + raise ValueError("Notification ID not set: set an explicit notification_id") client = self._require_client(client) @@ -395,7 +395,7 @@ def delete(self, client=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY): :raises ValueError: if the notification has no ID. """ if self.notification_id is None: - raise ValueError("Notification not intialized by server") + raise ValueError("Notification ID not set: set an explicit notification_id") client = self._require_client(client) diff --git a/tests/unit/test_blob.py b/tests/unit/test_blob.py index d25f0eb39..638db9f4e 100644 --- a/tests/unit/test_blob.py +++ b/tests/unit/test_blob.py @@ -5002,17 +5002,6 @@ def test_rewrite_same_name_w_kms_key_w_version(self): _target_object=dest, ) - def test_update_storage_class_invalid(self): - blob_name = "blob-name" - bucket = _Bucket() - blob = self._make_one(blob_name, bucket=bucket) - blob.rewrite = mock.Mock(spec=[]) - - with self.assertRaises(ValueError): - blob.update_storage_class("BOGUS") - - blob.rewrite.assert_not_called() - def _update_storage_class_multi_pass_helper(self, **kw): blob_name = "blob-name" storage_class = "NEARLINE" @@ -5223,6 +5212,38 @@ def test_update_storage_class_single_pass_w_retry(self): retry = mock.Mock(spec=[]) self._update_storage_class_single_pass_helper(retry=retry) + def test_update_storage_class_invalid(self): + from google.cloud.exceptions import BadRequest + + storage_class = "BOGUS" + blob_name = "blob-name" + client = mock.Mock(spec=[]) + bucket = _Bucket(client=client) + blob = self._make_one(blob_name, bucket=bucket) + blob.rewrite = mock.Mock(spec=[]) + blob.rewrite.side_effect = BadRequest("Invalid storage class") + + with self.assertRaises(BadRequest): + blob.update_storage_class(storage_class) + + # Test that invalid classes are allowed without client side validation. + # Fall back to server side validation and errors. + self.assertEqual(blob.storage_class, storage_class) + + blob.rewrite.assert_called_once_with( + blob, + if_generation_match=None, + if_generation_not_match=None, + if_metageneration_match=None, + if_metageneration_not_match=None, + if_source_generation_match=None, + if_source_generation_not_match=None, + if_source_metageneration_match=None, + if_source_metageneration_not_match=None, + timeout=self._get_default_timeout(), + retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED, + ) + def test_cache_control_getter(self): BLOB_NAME = "blob-name" bucket = _Bucket() diff --git a/tests/unit/test_bucket.py b/tests/unit/test_bucket.py index 5ff758758..163d31fd6 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -2813,11 +2813,15 @@ def test_storage_class_getter(self): self.assertEqual(bucket.storage_class, NEARLINE_STORAGE_CLASS) def test_storage_class_setter_invalid(self): + invalid_class = "BOGUS" NAME = "name" bucket = self._make_one(name=NAME) - with self.assertRaises(ValueError): - bucket.storage_class = "BOGUS" - self.assertFalse("storageClass" in bucket._changes) + bucket.storage_class = invalid_class + + # Test that invalid classes are allowed without client side validation. + # Fall back to server side validation and errors. + self.assertEqual(bucket.storage_class, invalid_class) + self.assertTrue("storageClass" in bucket._changes) def test_storage_class_setter_STANDARD(self): from google.cloud.storage.constants import STANDARD_STORAGE_CLASS diff --git a/tests/unit/test_hmac_key.py b/tests/unit/test_hmac_key.py index 917006b96..b74bc1e7e 100644 --- a/tests/unit/test_hmac_key.py +++ b/tests/unit/test_hmac_key.py @@ -149,11 +149,12 @@ def test_state_getter(self): def test_state_setter_invalid_state(self): metadata = self._make_one() expected = "INVALID" + metadata.state = expected - with self.assertRaises(ValueError): - metadata.state = expected - - self.assertIsNone(metadata.state) + # Test that invalid states are allowed without client side validation. + # Fall back to server side validation and errors. + self.assertEqual(metadata.state, expected) + self.assertEqual(metadata._properties["state"], expected) def test_state_setter_inactive(self): metadata = self._make_one()