def _delete_helper(self, **option_kwargs): from google.cloud.firestore_v1.types import write # Create a minimal fake GAPIC with a dummy response. firestore_api = mock.Mock(spec=["commit"]) firestore_api.commit.return_value = self._make_commit_repsonse() # Attach the fake GAPIC to a real client. client = _make_client("donut-base") client._firestore_api_internal = firestore_api # 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) else: option = None delete_time = document.delete() # Verify the response and the mocks. self.assertIs(delete_time, mock.sentinel.commit_time) write_pb = write.Write(delete=document._document_path) if option is not None: option.modify_write(write_pb) firestore_api.commit.assert_called_once_with( request={ "database": client._database_string, "writes": [write_pb], "transaction": None, }, metadata=client._rpc_metadata, )
def test_basewritebatch_update(): from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write client = _make_client() batch = _make_derived_write_batch(client) assert batch._write_pbs == [] reference = client.document("cats", "cradle") field_path = "head.foot" value = u"knees toes shoulders" field_updates = {field_path: value} ret_val = batch.update(reference, field_updates) assert ret_val is None map_pb = document.MapValue(fields={"foot": _value_pb(string_value=value)}) new_write_pb = write.Write( update=document.Document( name=reference._document_path, fields={"head": _value_pb(map_value=map_pb)}, ), update_mask=common.DocumentMask(field_paths=[field_path]), current_document=common.Precondition(exists=True), ) assert batch._write_pbs == [new_write_pb]
def _write_pb_for_create(document_path, document_data): from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1 import _helpers return write.Write( update=document.Document(name=document_path, fields=_helpers.encode_dict(document_data)), current_document=common.Precondition(exists=False), )
def get_transform_pb(self, document_path, exists=None) -> types.write.Write: field_transforms = self.get_field_transform_pbs(document_path) transform_pb = write.Write(transform=write.DocumentTransform( document=document_path, field_transforms=field_transforms)) if exists is not None: transform_pb._pb.current_document.CopyFrom( common.Precondition(exists=exists)._pb) return transform_pb
def _write_pb_for_update(document_path, update_values, field_paths): from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1 import _helpers return write.Write( update=document.Document(name=document_path, fields=_helpers.encode_dict(update_values)), update_mask=common.DocumentMask(field_paths=field_paths), current_document=common.Precondition(exists=True), )
def test_basewritebatch_delete(): from google.cloud.firestore_v1.types import write client = _make_client() batch = _make_derived_write_batch(client) assert batch._write_pbs == [] reference = client.document("early", "mornin", "dawn", "now") ret_val = batch.delete(reference) assert ret_val is None new_write_pb = write.Write(delete=reference._document_path) assert batch._write_pbs == [new_write_pb]
def test_delete(self): from google.cloud.firestore_v1.types import write client = _make_client() batch = self._make_one(client) self.assertEqual(batch._write_pbs, []) reference = client.document("early", "mornin", "dawn", "now") ret_val = batch.delete(reference) self.assertIsNone(ret_val) new_write_pb = write.Write(delete=reference._document_path) self.assertEqual(batch._write_pbs, [new_write_pb])
def get_transform_pb(self, document_path, exists=None) -> types.write.Write: def make_array_value(values): value_list = [encode_value(element) for element in values] return document.ArrayValue(values=value_list) path_field_transforms = ([( path, write.DocumentTransform.FieldTransform( field_path=path.to_api_repr(), set_to_server_value=REQUEST_TIME_ENUM, ), ) for path in self.server_timestamps] + [( path, write.DocumentTransform.FieldTransform( field_path=path.to_api_repr(), remove_all_from_array=make_array_value(values), ), ) for path, values in self.array_removes.items()] + [( path, write.DocumentTransform.FieldTransform( field_path=path.to_api_repr(), append_missing_elements=make_array_value(values), ), ) for path, values in self.array_unions.items()] + [( path, write.DocumentTransform.FieldTransform( field_path=path.to_api_repr(), increment=encode_value(value)), ) for path, value in self.increments.items()] + [( path, write.DocumentTransform.FieldTransform( field_path=path.to_api_repr(), maximum=encode_value(value)), ) for path, value in self.maximums.items()] + [( path, write.DocumentTransform.FieldTransform( field_path=path.to_api_repr(), minimum=encode_value(value)), ) for path, value in self.minimums.items()]) field_transforms = [ transform for path, transform in sorted(path_field_transforms) ] transform_pb = write.Write(transform=write.DocumentTransform( document=document_path, field_transforms=field_transforms)) if exists is not None: transform_pb._pb.current_document.CopyFrom( common.Precondition(exists=exists)._pb) return transform_pb
def pb_for_delete(document_path, option) -> types.write.Write: """Make a ``Write`` protobuf for ``delete()`` methods. Args: document_path (str): A fully-qualified document path. 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. Returns: google.cloud.firestore_v1.types.Write: A ``Write`` protobuf instance for the ``delete()``. """ write_pb = write.Write(delete=document_path) if option is not None: option.modify_write(write_pb) return write_pb
def get_update_pb( self, document_path, exists=None, allow_empty_mask=False ) -> types.write.Write: if exists is not None: current_document = common.Precondition(exists=exists) else: current_document = None update_pb = write.Write( update=document.Document( name=document_path, fields=encode_dict(self.set_fields) ), update_mask=self._get_update_mask(allow_empty_mask), current_document=current_document, ) return update_pb
def _write_pb_for_set(document_path, document_data, merge): from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write from google.cloud.firestore_v1 import _helpers write_pbs = write.Write(update=document.Document( name=document_path, fields=_helpers.encode_dict(document_data))) if merge: field_paths = [ field_path for field_path, value in _helpers.extract_fields( document_data, _helpers.FieldPath()) ] field_paths = [ field_path.to_api_repr() for field_path in sorted(field_paths) ] mask = common.DocumentMask(field_paths=sorted(field_paths)) write_pbs._pb.update_mask.CopyFrom(mask._pb) return write_pbs
def test_set(self): from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write client = _make_client() batch = self._make_one(client) self.assertEqual(batch._write_pbs, []) reference = client.document("another", "one") field = "zapzap" value = u"meadows and flowers" document_data = {field: value} ret_val = batch.set(reference, document_data) self.assertIsNone(ret_val) new_write_pb = write.Write(update=document.Document( name=reference._document_path, fields={field: _value_pb(string_value=value)}, )) self.assertEqual(batch._write_pbs, [new_write_pb])
def test_basewritebatch_set(): from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write client = _make_client() batch = _make_derived_write_batch(client) assert batch._write_pbs == [] reference = client.document("another", "one") field = "zapzap" value = u"meadows and flowers" document_data = {field: value} ret_val = batch.set(reference, document_data) assert ret_val is None new_write_pb = write.Write( update=document.Document( name=reference._document_path, fields={field: _value_pb(string_value=value)}, ) ) assert batch._write_pbs == [new_write_pb]
def test_basewritebatch_create(): from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write client = _make_client() batch = _make_derived_write_batch(client) assert batch._write_pbs == [] reference = client.document("this", "one") document_data = {"a": 10, "b": 2.5} ret_val = batch.create(reference, document_data) assert ret_val is None new_write_pb = write.Write( update=document.Document( name=reference._document_path, fields={ "a": _value_pb(integer_value=document_data["a"]), "b": _value_pb(double_value=document_data["b"]), }, ), current_document=common.Precondition(exists=False), ) assert batch._write_pbs == [new_write_pb]
def test_create(self): from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document from google.cloud.firestore_v1.types import write client = _make_client() batch = self._make_one(client) self.assertEqual(batch._write_pbs, []) reference = client.document("this", "one") document_data = {"a": 10, "b": 2.5} ret_val = batch.create(reference, document_data) self.assertIsNone(ret_val) new_write_pb = write.Write( update=document.Document( name=reference._document_path, fields={ "a": _value_pb(integer_value=document_data["a"]), "b": _value_pb(double_value=document_data["b"]), }, ), current_document=common.Precondition(exists=False), ) self.assertEqual(batch._write_pbs, [new_write_pb])