def _write_pb_for_update(document_path, update_values, field_paths): from google.cloud.firestore_v1beta1.proto import common_pb2 from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1.proto import write_pb2 from google.cloud.firestore_v1beta1 import _helpers return write_pb2.Write( update=document_pb2.Document( name=document_path, fields=_helpers.encode_dict(update_values), ), update_mask=common_pb2.DocumentMask(field_paths=field_paths), current_document=common_pb2.Precondition(exists=True), )
def get_update_pb(self, document_path, exists=None, allow_empty_mask=False): if exists is not None: current_document = common_pb2.Precondition(exists=exists) else: current_document = None update_pb = write_pb2.Write( update=document_pb2.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 test_modify_write_update_time(self): from google.protobuf import timestamp_pb2 from google.cloud.firestore_v1beta1.proto import common_pb2 from google.cloud.firestore_v1beta1.proto import write_pb2 timestamp_pb = timestamp_pb2.Timestamp( seconds=683893592, nanos=229362000, ) option = self._make_one(timestamp_pb) write_pb = write_pb2.Write() ret_val = option.modify_write(write_pb) self.assertIsNone(ret_val) expected_doc = common_pb2.Precondition(update_time=timestamp_pb) self.assertEqual(write_pb.current_document, expected_doc)
def pbs_for_set(document_path, document_data, merge=False, exists=None): """Make ``Write`` protobufs for ``set()`` methods. Args: document_path (str): A fully-qualified document path. document_data (dict): Property names and values to use for replacing a document. merge (bool): Whether to merge the fields or replace them exists (bool): If set, a precondition to indicate whether the document should exist or not. Used for create. Returns: List[google.cloud.firestore_v1beta1.types.Write]: One or two ``Write`` protobuf instances for ``set()``. """ transform_paths, actual_data, field_paths = process_server_timestamp( document_data, False) update_pb = write_pb2.Write(update=document_pb2.Document( name=document_path, fields=encode_dict(actual_data), ), ) if exists is not None: update_pb.current_document.CopyFrom( common_pb2.Precondition(exists=exists)) if merge: field_paths = canonicalize_field_paths(field_paths) mask = common_pb2.DocumentMask(field_paths=sorted(field_paths)) update_pb.update_mask.CopyFrom(mask) write_pbs = [update_pb] if transform_paths: # NOTE: We **explicitly** don't set any write option on # the ``transform_pb``. transform_pb = get_transform_pb(document_path, transform_paths) if not actual_data: write_pbs = [transform_pb] return write_pbs write_pbs.append(transform_pb) return write_pbs
def test_create(self): from google.cloud.firestore_v1beta1.proto import common_pb2 from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1.proto import write_pb2 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_pb2.Write( update=document_pb2.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_pb2.Precondition(exists=False), ) self.assertEqual(batch._write_pbs, [new_write_pb])
def test_update_and_transform(self): from google.cloud.firestore_v1beta1.proto import common_pb2 precondition = common_pb2.Precondition(exists=True) self._helper(current_document=precondition, do_transform=True)
def test_without_option(self): from google.cloud.firestore_v1beta1.proto import common_pb2 precondition = common_pb2.Precondition(exists=True) self._helper(current_document=precondition)