def test___call__failure(self): from google.api_core import exceptions from google.cloud.firestore_v1beta1.transaction import _EXCEED_ATTEMPTS_TEMPLATE to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[]) wrapped = self._make_one(to_wrap) txn_id = b"only-one-shot" transaction = _make_transaction(txn_id, max_attempts=1) # Actually force the ``commit`` to fail. exc = exceptions.Aborted("Contention just once.") firestore_api = transaction._client._firestore_api firestore_api.commit.side_effect = exc # Call the __call__-able ``wrapped``. with self.assertRaises(ValueError) as exc_info: wrapped(transaction, "here", there=1.5) err_msg = _EXCEED_ATTEMPTS_TEMPLATE.format(transaction._max_attempts) self.assertEqual(exc_info.exception.args, (err_msg,)) self.assertIsNone(transaction._id) self.assertEqual(wrapped.current_id, txn_id) self.assertEqual(wrapped.retry_id, txn_id) # Verify mocks. to_wrap.assert_called_once_with(transaction, "here", there=1.5) firestore_api.begin_transaction.assert_called_once_with( transaction._client._database_string, options_=None, metadata=transaction._client._rpc_metadata, ) firestore_api.rollback.assert_called_once_with( transaction._client._database_string, txn_id, metadata=transaction._client._rpc_metadata, ) firestore_api.commit.assert_called_once_with( transaction._client._database_string, [], transaction=txn_id, metadata=transaction._client._rpc_metadata, )
def test___call__failure(self): from google.api_core import exceptions from google.cloud.firestore_v1beta1.transaction import ( _EXCEED_ATTEMPTS_TEMPLATE) to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[]) wrapped = self._make_one(to_wrap) txn_id = b'only-one-shot' transaction = _make_transaction(txn_id, max_attempts=1) # Actually force the ``commit`` to fail. exc = exceptions.Aborted('Contention just once.') firestore_api = transaction._client._firestore_api firestore_api.commit.side_effect = exc # Call the __call__-able ``wrapped``. with self.assertRaises(ValueError) as exc_info: wrapped(transaction, 'here', there=1.5) err_msg = _EXCEED_ATTEMPTS_TEMPLATE.format(transaction._max_attempts) self.assertEqual(exc_info.exception.args, (err_msg,)) self.assertIsNone(transaction._id) self.assertEqual(wrapped.current_id, txn_id) self.assertEqual(wrapped.retry_id, txn_id) # Verify mocks. to_wrap.assert_called_once_with(transaction, 'here', there=1.5) firestore_api.begin_transaction.assert_called_once_with( transaction._client._database_string, options_=None, metadata=transaction._client._rpc_metadata) firestore_api.rollback.assert_called_once_with( transaction._client._database_string, txn_id, metadata=transaction._client._rpc_metadata) firestore_api.commit.assert_called_once_with( transaction._client._database_string, [], transaction=txn_id, metadata=transaction._client._rpc_metadata)