Skip to content

Commit

Permalink
feat: add retry/timeout to 'collection.CollectionReference.get'
Browse files Browse the repository at this point in the history
Toward #221
  • Loading branch information
tseaver committed Oct 13, 2020
1 parent 2b80b91 commit 49d3b03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions google/cloud/firestore_v1/collection.py
Expand Up @@ -154,7 +154,12 @@ def list_documents(
)
return (_item_to_document_ref(self, i) for i in iterator)

def get(self, transaction: Transaction = None) -> list:
def get(
self,
transaction: Transaction = None,
retry: retries.Retry = None,
timeout: float = None,
) -> list:
"""Read the documents in this collection.
This sends a ``RunQuery`` RPC and returns a list of documents
Expand All @@ -164,6 +169,9 @@ def get(self, transaction: Transaction = None) -> list:
transaction
(Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
An existing transaction that this query will run in.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
If a ``transaction`` is used and it already has write operations
added, this method cannot be used (i.e. read-after-write is not
Expand All @@ -173,7 +181,8 @@ def get(self, transaction: Transaction = None) -> list:
list: The documents in this collection that match the query.
"""
query = query_mod.Query(self)
return query.get(transaction=transaction)
kwargs = self._make_retry_timeout_kwargs(retry, timeout)
return query.get(transaction=transaction, **kwargs)

def stream(
self, transaction: Transaction = None
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/v1/test_collection.py
Expand Up @@ -286,6 +286,23 @@ def test_get(self, query_class):
self.assertIs(get_response, query_instance.get.return_value)
query_instance.get.assert_called_once_with(transaction=None)

@mock.patch("google.cloud.firestore_v1.query.Query", autospec=True)
def test_get_w_retry_timeout(self, query_class):
from google.api_core.retry import Retry

retry = Retry(predicate=object())
timeout = 123.0
collection = self._make_one("collection")
get_response = collection.get(retry=retry, timeout=timeout)

query_class.assert_called_once_with(collection)
query_instance = query_class.return_value

self.assertIs(get_response, query_instance.get.return_value)
query_instance.get.assert_called_once_with(
transaction=None, retry=retry, timeout=timeout,
)

@mock.patch("google.cloud.firestore_v1.query.Query", autospec=True)
def test_get_with_transaction(self, query_class):

Expand Down

0 comments on commit 49d3b03

Please sign in to comment.