def test_good_transaction(self): from google.cloud.firestore_v1beta1.transaction import Transaction transaction = Transaction(mock.sentinel.client) txn_id = b'doubt-it' transaction._id = txn_id self.assertTrue(transaction.in_progress) self.assertEqual(self._call_fut(transaction), txn_id)
def test_after_writes_allowed(self): from google.cloud.firestore_v1beta1.transaction import Transaction transaction = Transaction(mock.sentinel.client) txn_id = b'we-are-0fine' transaction._id = txn_id transaction._write_pbs.append(mock.sentinel.write) ret_val = self._call_fut(transaction, read_operation=False) self.assertEqual(ret_val, txn_id)
def test_after_writes_not_allowed(self): from google.cloud.firestore_v1beta1._helpers import ReadAfterWriteError from google.cloud.firestore_v1beta1.transaction import Transaction transaction = Transaction(mock.sentinel.client) transaction._id = b'under-hook' transaction._write_pbs.append(mock.sentinel.write) with self.assertRaises(ReadAfterWriteError): self._call_fut(transaction)
def _make_transaction(txn_id, **txn_kwargs): from google.protobuf import empty_pb2 from google.cloud.firestore_v1beta1.gapic import firestore_client from google.cloud.firestore_v1beta1.proto import firestore_pb2 from google.cloud.firestore_v1beta1.proto import write_pb2 from google.cloud.firestore_v1beta1.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_pb2.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_pb2.CommitResponse(write_results=[ write_pb2.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)
def test_invalid_transaction(self): from google.cloud.firestore_v1beta1.transaction import Transaction transaction = Transaction(mock.sentinel.client) self.assertFalse(transaction.in_progress) with self.assertRaises(ValueError): self._call_fut(transaction)
def test_get_with_transaction(self): from google.cloud.firestore_v1beta1.client import Client from google.cloud.firestore_v1beta1.transaction import Transaction # Create a minimal fake client with a dummy response. response_iterator = iter([mock.sentinel.snapshot]) client = mock.create_autospec(Client, instance=True) client.get_all.return_value = response_iterator # Actually make a document and call get(). document = self._make_one('yellow', 'mellow', client=client) transaction = Transaction(client) transaction._id = b'asking-me-2' snapshot = document.get(transaction=transaction) # Verify the response and the mocks. self.assertIs(snapshot, mock.sentinel.snapshot) client.get_all.assert_called_once_with( [document], field_paths=None, transaction=transaction)
def transaction(self, **kwargs): """Get a transaction that uses this client. See :class:`~.firestore_v1beta1.transaction.Transaction` for more information on transactions and the constructor arguments. Args: kwargs (Dict[str, Any]): The keyword arguments (other than ``client``) to pass along to the :class:`~.firestore_v1beta1.transaction.Transaction` constructor. Returns: ~.firestore_v1beta1.transaction.Transaction: A transaction attached to this client. """ return Transaction(self, **kwargs)
def _get_helper(self, field_paths=None, use_transaction=False, not_found=False): from google.api_core.exceptions import NotFound from google.cloud.firestore_v1beta1.proto import common_pb2 from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1.transaction import Transaction # Create a minimal fake GAPIC with a dummy response. create_time = 123 update_time = 234 firestore_api = mock.Mock(spec=["get_document"]) response = mock.create_autospec(document_pb2.Document) response.fields = {} response.create_time = create_time response.update_time = update_time if not_found: firestore_api.get_document.side_effect = NotFound("testing") else: firestore_api.get_document.return_value = response client = _make_client("donut-base") client._firestore_api_internal = firestore_api document = self._make_one("where", "we-are", client=client) if use_transaction: transaction = Transaction(client) transaction_id = transaction._id = b"asking-me-2" else: transaction = None snapshot = document.get(field_paths=field_paths, transaction=transaction) self.assertIs(snapshot.reference, document) if not_found: self.assertIsNone(snapshot._data) self.assertFalse(snapshot.exists) self.assertIsNone(snapshot.read_time) self.assertIsNone(snapshot.create_time) self.assertIsNone(snapshot.update_time) else: self.assertEqual(snapshot.to_dict(), {}) self.assertTrue(snapshot.exists) self.assertIsNone(snapshot.read_time) self.assertIs(snapshot.create_time, create_time) self.assertIs(snapshot.update_time, update_time) # Verify the request made to the API if field_paths is not None: mask = common_pb2.DocumentMask(field_paths=sorted(field_paths)) else: mask = None if use_transaction: expected_transaction_id = transaction_id else: expected_transaction_id = None firestore_api.get_document.assert_called_once_with( document._document_path, mask=mask, transaction=expected_transaction_id, metadata=client._rpc_metadata, )