def test__transactional__pre_commit_retry_id_already_set_success(): from google.cloud.firestore_v1.types import common to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[]) wrapped = _make__transactional(to_wrap) txn_id1 = b"already-set" wrapped.retry_id = txn_id1 txn_id2 = b"ok-here-too" transaction = _make_transaction_pb(txn_id2) result = wrapped._pre_commit(transaction) assert result is mock.sentinel.result assert transaction._id == txn_id2 assert wrapped.current_id == txn_id2 assert wrapped.retry_id == txn_id1 # Verify mocks. to_wrap.assert_called_once_with(transaction) firestore_api = transaction._client._firestore_api options_ = common.TransactionOptions( read_write=common.TransactionOptions.ReadWrite( retry_transaction=txn_id1)) firestore_api.begin_transaction.assert_called_once_with( request={ "database": transaction._client._database_string, "options": options_, }, metadata=transaction._client._rpc_metadata, ) firestore_api.rollback.assert_not_called() firestore_api.commit.assert_not_called()
def test_basetransaction__options_protobuf_read_only(): from google.cloud.firestore_v1.types import common transaction = _make_base_transaction(read_only=True) options_pb = transaction._options_protobuf(None) expected_pb = common.TransactionOptions( read_only=common.TransactionOptions.ReadOnly()) assert options_pb == expected_pb
def test__options_protobuf_read_only(self): from google.cloud.firestore_v1.types import common transaction = self._make_one(read_only=True) options_pb = transaction._options_protobuf(None) expected_pb = common.TransactionOptions( read_only=common.TransactionOptions.ReadOnly()) self.assertEqual(options_pb, expected_pb)
def test_basetransaction__options_protobuf_on_retry(): from google.cloud.firestore_v1.types import common transaction = _make_base_transaction() retry_id = b"hocus-pocus" options_pb = transaction._options_protobuf(retry_id) expected_pb = common.TransactionOptions( read_write=common.TransactionOptions.ReadWrite( retry_transaction=retry_id)) assert options_pb == expected_pb
def test__options_protobuf_on_retry(self): from google.cloud.firestore_v1.types import common transaction = self._make_one() retry_id = b"hocus-pocus" options_pb = transaction._options_protobuf(retry_id) expected_pb = common.TransactionOptions( read_write=common.TransactionOptions.ReadWrite( retry_transaction=retry_id)) self.assertEqual(options_pb, expected_pb)
async def test___call__success_second_attempt(self): from google.api_core import exceptions from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write to_wrap = AsyncMock(return_value=mock.sentinel.result, spec=[]) wrapped = self._make_one(to_wrap) txn_id = b"whole-enchilada" transaction = _make_transaction(txn_id) # Actually force the ``commit`` to fail on first / succeed on second. exc = exceptions.Aborted("Contention junction.") firestore_api = transaction._client._firestore_api firestore_api.commit.side_effect = [ exc, firestore.CommitResponse(write_results=[write.WriteResult()]), ] # Call the __call__-able ``wrapped``. result = await wrapped(transaction, "a", b="c") self.assertIs(result, mock.sentinel.result) self.assertIsNone(transaction._id) self.assertEqual(wrapped.current_id, txn_id) self.assertEqual(wrapped.retry_id, txn_id) # Verify mocks. wrapped_call = mock.call(transaction, "a", b="c") self.assertEqual(to_wrap.mock_calls, [wrapped_call, wrapped_call]) firestore_api = transaction._client._firestore_api db_str = transaction._client._database_string options_ = common.TransactionOptions( read_write=common.TransactionOptions.ReadWrite(retry_transaction=txn_id) ) self.assertEqual( firestore_api.begin_transaction.mock_calls, [ mock.call( request={"database": db_str, "options": None}, metadata=transaction._client._rpc_metadata, ), mock.call( request={"database": db_str, "options": options_}, metadata=transaction._client._rpc_metadata, ), ], ) firestore_api.rollback.assert_not_called() commit_call = mock.call( request={"database": db_str, "writes": [], "transaction": txn_id}, metadata=transaction._client._rpc_metadata, ) self.assertEqual(firestore_api.commit.mock_calls, [commit_call, commit_call])