Skip to content

Commit

Permalink
feat: add retry/timeout to 'document.Document.delete'
Browse files Browse the repository at this point in the history
Toward #221
  • Loading branch information
tseaver committed Oct 13, 2020
1 parent 19ddd52 commit bf1d084
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
12 changes: 11 additions & 1 deletion google/cloud/firestore_v1/document.py
Expand Up @@ -269,13 +269,21 @@ def update(self, field_updates: dict, option: _helpers.WriteOption = None) -> An
write_results = batch.commit()
return _first_write_result(write_results)

def delete(self, option: _helpers.WriteOption = None) -> Any:
def delete(
self,
option: _helpers.WriteOption = None,
retry: retries.Retry = None,
timeout: float = None,
) -> Any:
"""Delete the current document in the Firestore database.
Args:
option (Optional[:class:`~google.cloud.firestore_v1.client.WriteOption`]):
A write option to make assertions / preconditions on the server
state of the document before applying changes.
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.protobuf.timestamp_pb2.Timestamp`:
Expand All @@ -285,13 +293,15 @@ def delete(self, option: _helpers.WriteOption = None) -> Any:
still return the time that the request was received by the server.
"""
write_pb = _helpers.pb_for_delete(self._document_path, option)
kwargs = self._make_retry_timeout_kwargs(retry, timeout)
commit_response = self._client._firestore_api.commit(
request={
"database": self._client._database_string,
"writes": [write_pb],
"transaction": None,
},
metadata=self._client._rpc_metadata,
**kwargs,
)

return commit_response.commit_time
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/v1/test_document.py
Expand Up @@ -270,7 +270,7 @@ def test_empty_update(self):
with self.assertRaises(ValueError):
document.update(field_updates)

def _delete_helper(self, **option_kwargs):
def _delete_helper(self, retry=None, timeout=None, **option_kwargs):
from google.cloud.firestore_v1.types import write

# Create a minimal fake GAPIC with a dummy response.
Expand All @@ -281,14 +281,22 @@ def _delete_helper(self, **option_kwargs):
client = _make_client("donut-base")
client._firestore_api_internal = firestore_api

kwargs = {}

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

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

# Actually make a document and call delete().
document = self._make_one("where", "we-are", client=client)
if option_kwargs:
option = client.write_option(**option_kwargs)
delete_time = document.delete(option=option)
delete_time = document.delete(option=option, **kwargs)
else:
option = None
delete_time = document.delete()
delete_time = document.delete(**kwargs)

# Verify the response and the mocks.
self.assertIs(delete_time, mock.sentinel.commit_time)
Expand All @@ -302,6 +310,7 @@ def _delete_helper(self, **option_kwargs):
"transaction": None,
},
metadata=client._rpc_metadata,
**kwargs,
)

def test_delete(self):
Expand All @@ -313,6 +322,13 @@ def test_delete_with_option(self):
timestamp_pb = timestamp_pb2.Timestamp(seconds=1058655101, nanos=100022244)
self._delete_helper(last_update_time=timestamp_pb)

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

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

def _get_helper(
self,
field_paths=None,
Expand Down

0 comments on commit bf1d084

Please sign in to comment.