def test_more_than_one(self): from google.cloud.firestore_v1.types import write result1 = write.WriteResult() result2 = write.WriteResult() write_results = [result1, result2] result = self._call_fut(write_results) self.assertIs(result, result1)
def test__first_write_result_more_than_one(): from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1.base_document import _first_write_result result1 = write.WriteResult() result2 = write.WriteResult() write_results = [result1, result2] result = _first_write_result(write_results) assert result is result1
def _make_transaction_pb(txn_id, **txn_kwargs): from google.protobuf import empty_pb2 from google.cloud.firestore_v1.services.firestore import client as firestore_client from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1.transaction import Transaction # Create a fake GAPIC ... firestore_api = mock.create_autospec(firestore_client.FirestoreClient, instance=True) # ... with a dummy ``BeginTransactionResponse`` result ... begin_response = firestore.BeginTransactionResponse(transaction=txn_id) firestore_api.begin_transaction.return_value = begin_response # ... and a dummy ``Rollback`` result ... firestore_api.rollback.return_value = empty_pb2.Empty() # ... and a dummy ``Commit`` result. commit_response = firestore.CommitResponse( write_results=[write.WriteResult()]) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api return Transaction(client, **txn_kwargs)
async def _commit_helper(self, retry=None, timeout=None): from google.protobuf import timestamp_pb2 from google.cloud.firestore_v1 import _helpers from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write # Create a minimal fake GAPIC with a dummy result. firestore_api = AsyncMock(spec=["commit"]) timestamp = timestamp_pb2.Timestamp(seconds=1234567, nanos=123456798) commit_response = firestore.CommitResponse( write_results=[write.WriteResult(), write.WriteResult()], commit_time=timestamp, ) firestore_api.commit.return_value = commit_response kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) # Attach the fake GAPIC to a real client. client = _make_client("grand") client._firestore_api_internal = firestore_api # Actually make a batch with some mutations and call commit(). batch = self._make_one(client) document1 = client.document("a", "b") batch.create(document1, {"ten": 10, "buck": "ets"}) document2 = client.document("c", "d", "e", "f") batch.delete(document2) write_pbs = batch._write_pbs[::] write_results = await batch.commit(**kwargs) self.assertEqual(write_results, list(commit_response.write_results)) self.assertEqual(batch.write_results, write_results) self.assertEqual(batch.commit_time.timestamp_pb(), timestamp) # Make sure batch has no more "changes". self.assertEqual(batch._write_pbs, []) # Verify the mocks. firestore_api.commit.assert_called_once_with( request={ "database": client._database_string, "writes": write_pbs, "transaction": None, }, metadata=client._rpc_metadata, **kwargs, )
def test_success(self): from google.protobuf import timestamp_pb2 from google.cloud.firestore_v1.types import write single_result = write.WriteResult(update_time=timestamp_pb2.Timestamp( seconds=1368767504, nanos=458000123)) write_results = [single_result] result = self._call_fut(write_results) self.assertIs(result, single_result)
def _write_helper(self, retry=None, timeout=None): from google.cloud.firestore_v1 import _helpers from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write # Create a minimal fake GAPIC with a dummy result. firestore_api = mock.Mock(spec=["batch_write"]) write_response = firestore.BatchWriteResponse( write_results=[write.WriteResult(), write.WriteResult()], ) firestore_api.batch_write.return_value = write_response kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) # Attach the fake GAPIC to a real client. client = _make_client("grand") client._firestore_api_internal = firestore_api # Actually make a batch with some mutations and call commit(). batch = self._make_one(client) document1 = client.document("a", "b") self.assertFalse(document1 in batch) batch.create(document1, {"ten": 10, "buck": "ets"}) self.assertTrue(document1 in batch) document2 = client.document("c", "d", "e", "f") batch.delete(document2) write_pbs = batch._write_pbs[::] resp = batch.commit(**kwargs) self.assertEqual(resp.write_results, list(write_response.write_results)) self.assertEqual(batch.write_results, resp.write_results) # Make sure batch has no more "changes". self.assertEqual(batch._write_pbs, []) # Verify the mocks. firestore_api.batch_write.assert_called_once_with( request={ "database": client._database_string, "writes": write_pbs, "labels": None, }, metadata=client._rpc_metadata, **kwargs, )
def test__first_write_result_success(): from google.protobuf import timestamp_pb2 from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1.base_document import _first_write_result single_result = write.WriteResult(update_time=timestamp_pb2.Timestamp( seconds=1368767504, nanos=458000123)) write_results = [single_result] result = _first_write_result(write_results) assert result is single_result
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])
def test_as_context_mgr_wo_error(self): from google.protobuf import timestamp_pb2 from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write firestore_api = mock.Mock(spec=["commit"]) timestamp = timestamp_pb2.Timestamp(seconds=1234567, nanos=123456798) commit_response = firestore.CommitResponse( write_results=[write.WriteResult(), write.WriteResult()], commit_time=timestamp, ) firestore_api.commit.return_value = commit_response client = _make_client() client._firestore_api_internal = firestore_api batch = self._make_one(client) document1 = client.document("a", "b") document2 = client.document("c", "d", "e", "f") with batch as ctx_mgr: self.assertIs(ctx_mgr, batch) ctx_mgr.create(document1, {"ten": 10, "buck": "ets"}) ctx_mgr.delete(document2) write_pbs = batch._write_pbs[::] self.assertEqual(batch.write_results, list(commit_response.write_results)) self.assertEqual(batch.commit_time.timestamp_pb(), timestamp) # Make sure batch has no more "changes". self.assertEqual(batch._write_pbs, []) # Verify the mocks. firestore_api.commit.assert_called_once_with( request={ "database": client._database_string, "writes": write_pbs, "transaction": None, }, metadata=client._rpc_metadata, )
def test__commit(self): from google.cloud.firestore_v1.services.firestore import ( client as firestore_client, ) from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write # Create a minimal fake GAPIC with a dummy result. firestore_api = mock.create_autospec(firestore_client.FirestoreClient, instance=True) commit_response = firestore.CommitResponse( write_results=[write.WriteResult()]) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. client = _make_client("phone-joe") client._firestore_api_internal = firestore_api # Actually make a transaction with some mutations and call _commit(). transaction = self._make_one(client) txn_id = b"under-over-thru-woods" transaction._id = txn_id document = client.document("zap", "galaxy", "ship", "space") transaction.set(document, {"apple": 4.5}) write_pbs = transaction._write_pbs[::] write_results = transaction._commit() self.assertEqual(write_results, list(commit_response.write_results)) # Make sure transaction has no more "changes". self.assertIsNone(transaction._id) self.assertEqual(transaction._write_pbs, []) # Verify the mocks. firestore_api.commit.assert_called_once_with( request={ "database": client._database_string, "writes": write_pbs, "transaction": txn_id, }, metadata=client._rpc_metadata, )
def _make_transaction(txn_id, **txn_kwargs): from google.protobuf import empty_pb2 from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1.async_transaction import AsyncTransaction # Create a fake GAPIC ... firestore_api = AsyncMock() # ... with a dummy ``BeginTransactionResponse`` result ... begin_response = firestore.BeginTransactionResponse(transaction=txn_id) firestore_api.begin_transaction.return_value = begin_response # ... and a dummy ``Rollback`` result ... firestore_api.rollback.return_value = empty_pb2.Empty() # ... and a dummy ``Commit`` result. commit_response = firestore.CommitResponse(write_results=[write.WriteResult()]) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api return AsyncTransaction(client, **txn_kwargs)
async def test_asynctransaction__commit(): from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write # Create a minimal fake GAPIC with a dummy result. firestore_api = AsyncMock() commit_response = firestore.CommitResponse(write_results=[write.WriteResult()]) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. client = _make_client("phone-joe") client._firestore_api_internal = firestore_api # Actually make a transaction with some mutations and call _commit(). transaction = _make_async_transaction(client) txn_id = b"under-over-thru-woods" transaction._id = txn_id document = client.document("zap", "galaxy", "ship", "space") transaction.set(document, {"apple": 4.5}) write_pbs = transaction._write_pbs[::] write_results = await transaction._commit() assert write_results == list(commit_response.write_results) # Make sure transaction has no more "changes". assert transaction._id is None assert transaction._write_pbs == [] # Verify the mocks. firestore_api.commit.assert_called_once_with( request={ "database": client._database_string, "writes": write_pbs, "transaction": txn_id, }, metadata=client._rpc_metadata, )
def _mock_firestore_api(): firestore_api = mock.Mock(spec=["commit"]) commit_response = firestore.CommitResponse( write_results=[write.WriteResult()]) firestore_api.commit.return_value = commit_response return firestore_api