Skip to content

Commit

Permalink
feat: add retry/timeout to 'document.Document.create'
Browse files Browse the repository at this point in the history
Toward #221
  • Loading branch information
tseaver committed Oct 13, 2020
1 parent bf1d084 commit 58ca931
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
10 changes: 8 additions & 2 deletions google/cloud/firestore_v1/document.py
Expand Up @@ -69,12 +69,17 @@ def _make_retry_timeout_kwargs(retry, timeout):

return kwargs

def create(self, document_data) -> Any:
def create(
self, document_data, retry: retries.Retry = None, timeout: float = None,
) -> Any:
"""Create the current document in the Firestore database.
Args:
document_data (dict): Property names and values to use for
creating a document.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Returns:
:class:`~google.cloud.firestore_v1.types.WriteResult`:
Expand All @@ -87,7 +92,8 @@ def create(self, document_data) -> Any:
"""
batch = self._client.batch()
batch.create(self, document_data)
write_results = batch.commit()
kwargs = self._make_retry_timeout_kwargs(retry, timeout)
write_results = batch.commit(**kwargs)
return _first_write_result(write_results)

def set(self, document_data: dict, merge: bool = False) -> Any:
Expand Down
24 changes: 22 additions & 2 deletions tests/unit/v1/test_document.py
Expand Up @@ -69,7 +69,7 @@ def _write_pb_for_create(document_path, document_data):
current_document=common.Precondition(exists=False),
)

def test_create(self):
def _create_helper(self, retry=None, timeout=None):
# Create a minimal fake GAPIC with a dummy response.
firestore_api = mock.Mock()
firestore_api.commit.mock_add_spec(spec=["commit"])
Expand All @@ -82,7 +82,16 @@ def test_create(self):
# Actually make a document and call create().
document = self._make_one("foo", "twelve", client=client)
document_data = {"hello": "goodbye", "count": 99}
write_result = document.create(document_data)

kwargs = {}

if retry is not None:
kwargs["retry"] = retry

if timeout is not None:
kwargs["timeout"] = timeout

write_result = document.create(document_data, **kwargs)

# Verify the response and the mocks.
self.assertIs(write_result, mock.sentinel.write_result)
Expand All @@ -94,8 +103,19 @@ def test_create(self):
"transaction": None,
},
metadata=client._rpc_metadata,
**kwargs,
)

def test_create(self):
self._create_helper()

def test_create_w_retry_timeout(self):
from google.api_core.retry import Retry

retry = Retry(predicate=object())
timeout = 123.0
self._create_helper(retry=retry, timeout=timeout)

def test_create_empty(self):
# Create a minimal fake GAPIC with a dummy response.
from google.cloud.firestore_v1.document import DocumentReference
Expand Down

0 comments on commit 58ca931

Please sign in to comment.