def _make_query_response(**kwargs): # kwargs supported are ``skipped_results``, ``name`` and ``data`` from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1.proto import firestore_pb2 from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.firestore_v1beta1 import _helpers now = datetime.datetime.utcnow() read_time = _datetime_to_pb_timestamp(now) kwargs['read_time'] = read_time name = kwargs.pop('name', None) data = kwargs.pop('data', None) if name is not None and data is not None: document_pb = document_pb2.Document( name=name, fields=_helpers.encode_dict(data), ) delta = datetime.timedelta(seconds=100) update_time = _datetime_to_pb_timestamp(now - delta) create_time = _datetime_to_pb_timestamp(now - 2 * delta) document_pb.update_time.CopyFrom(update_time) document_pb.create_time.CopyFrom(create_time) kwargs['document'] = document_pb return firestore_pb2.RunQueryResponse(**kwargs)
def _write_pb_for_create(document_path, document_data): 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(document_data) ), current_document=common_pb2.Precondition(exists=False), )
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 _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 test_add_auto_assigned(self): from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1 import _helpers from google.cloud.firestore_v1beta1.document import DocumentReference # Create a minimal fake GAPIC add attach it to a real client. firestore_api = mock.Mock(spec=["create_document"]) create_doc_response = document_pb2.Document() firestore_api.create_document.return_value = create_doc_response client = _make_client() client._firestore_api_internal = firestore_api # Actually make a collection. collection = self._make_one("grand-parent", "parent", "child", client=client) # Add a dummy response for the fake GAPIC. parent_path = collection.parent._document_path auto_assigned_id = "cheezburger" name = "{}/{}/{}".format(parent_path, collection.id, auto_assigned_id) create_doc_response = document_pb2.Document(name=name) create_doc_response.update_time.FromDatetime( datetime.datetime.utcnow()) firestore_api.create_document.return_value = create_doc_response # Actually call add() on our collection. document_data = {"been": "here"} update_time, document_ref = collection.add(document_data) # Verify the response and the mocks. self.assertIs(update_time, create_doc_response.update_time) self.assertIsInstance(document_ref, DocumentReference) self.assertIs(document_ref._client, client) expected_path = collection._path + (auto_assigned_id, ) self.assertEqual(document_ref._path, expected_path) expected_document_pb = document_pb2.Document( fields=_helpers.encode_dict(document_data)) firestore_api.create_document.assert_called_once_with( parent_path, collection_id=collection.id, document_id=None, document=expected_document_pb, mask=None, metadata=client._rpc_metadata, )
def _write_pb_for_set(document_path, document_data, merge): 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 write_pbs = write_pb2.Write(update=document_pb2.Document( name=document_path, fields=_helpers.encode_dict(document_data), ), ) if merge: _, _, field_paths = _helpers.process_server_timestamp( document_data, split_on_dots=False) field_paths = _helpers.canonicalize_field_paths(field_paths) mask = common_pb2.DocumentMask(field_paths=sorted(field_paths)) write_pbs.update_mask.CopyFrom(mask) return write_pbs
def add(self, document_data, document_id=None): """Create a document in the Firestore database with the provided data. Args: document_data (dict): Property names and values to use for creating the document. document_id (Optional[str]): The document identifier within the current collection. If not provided, an ID will be automatically assigned by the server (the assigned ID will be a random 20 character string composed of digits, uppercase and lowercase letters). Returns: Tuple[google.protobuf.timestamp_pb2.Timestamp, \ ~.firestore_v1beta1.document.DocumentReference]: Pair of * The ``update_time`` when the document was created (or overwritten). * A document reference for the created document. Raises: ~google.cloud.exceptions.Conflict: If ``document_id`` is provided and the document already exists. """ if document_id is None: parent_path, expected_prefix = self._parent_info() document_pb = document_pb2.Document( fields=_helpers.encode_dict(document_data)) created_document_pb = self._client._firestore_api.create_document( parent_path, collection_id=self.id, document_id=None, document=document_pb, mask=None, metadata=self._client._rpc_metadata, ) new_document_id = _helpers.get_doc_id(created_document_pb, expected_prefix) document_ref = self.document(new_document_id) return created_document_pb.update_time, document_ref else: document_ref = self.document(document_id) write_result = document_ref.create(document_data) return write_result.update_time, document_ref
def test_add_auto_assigned(self): from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1 import _helpers from google.cloud.firestore_v1beta1.document import DocumentReference # Create a minimal fake GAPIC add attach it to a real client. firestore_api = mock.Mock(spec=["create_document"]) create_doc_response = document_pb2.Document() firestore_api.create_document.return_value = create_doc_response client = _make_client() client._firestore_api_internal = firestore_api # Actually make a collection. collection = self._make_one("grand-parent", "parent", "child", client=client) # Add a dummy response for the fake GAPIC. parent_path = collection.parent._document_path auto_assigned_id = "cheezburger" name = "{}/{}/{}".format(parent_path, collection.id, auto_assigned_id) create_doc_response = document_pb2.Document(name=name) create_doc_response.update_time.FromDatetime(datetime.datetime.utcnow()) firestore_api.create_document.return_value = create_doc_response # Actually call add() on our collection. document_data = {"been": "here"} update_time, document_ref = collection.add(document_data) # Verify the response and the mocks. self.assertIs(update_time, create_doc_response.update_time) self.assertIsInstance(document_ref, DocumentReference) self.assertIs(document_ref._client, client) expected_path = collection._path + (auto_assigned_id,) self.assertEqual(document_ref._path, expected_path) expected_document_pb = document_pb2.Document( fields=_helpers.encode_dict(document_data) ) firestore_api.create_document.assert_called_once_with( parent_path, collection_id=collection.id, document_id=None, document=expected_document_pb, mask=None, metadata=client._rpc_metadata, )
def _write_pb_for_set(document_path, document_data, merge): 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 write_pbs = write_pb2.Write( update=document_pb2.Document( name=document_path, fields=_helpers.encode_dict(document_data), ), ) if merge: _, _, field_paths = _helpers.process_server_timestamp( document_data) field_paths = _helpers.canonicalize_field_paths(field_paths) mask = common_pb2.DocumentMask(field_paths=sorted(field_paths)) write_pbs.update_mask.CopyFrom(mask) return write_pbs
def _doc_get_info(ref_string, values): from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.firestore_v1beta1 import _helpers now = datetime.datetime.utcnow() read_time = _datetime_to_pb_timestamp(now) delta = datetime.timedelta(seconds=100) update_time = _datetime_to_pb_timestamp(now - delta) create_time = _datetime_to_pb_timestamp(now - 2 * delta) document_pb = document_pb2.Document( name=ref_string, fields=_helpers.encode_dict(values), create_time=create_time, update_time=update_time, ) return document_pb, read_time
def _doc_get_info(ref_string, values): from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.firestore_v1beta1 import _helpers now = datetime.datetime.utcnow() read_time = _datetime_to_pb_timestamp(now) delta = datetime.timedelta(seconds=100) update_time = _datetime_to_pb_timestamp(now - delta) create_time = _datetime_to_pb_timestamp(now - 2 * delta) document_pb = document_pb2.Document( name=ref_string, fields=_helpers.encode_dict(values), create_time=create_time, update_time=update_time, ) return document_pb, read_time
def _write_pb_for_set(document_path, document_data, merge): 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 write_pbs = write_pb2.Write(update=document_pb2.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_pb2.DocumentMask(field_paths=sorted(field_paths)) write_pbs.update_mask.CopyFrom(mask) return write_pbs
def add(self, document_data, document_id=None): """Create a document in the Firestore database with the provided data. Args: document_data (dict): Property names and values to use for creating the document. document_id (Optional[str]): The document identifier within the current collection. If not provided, an ID will be automatically assigned by the server (the assigned ID will be a random 20 character string composed of digits, uppercase and lowercase letters). Returns: Tuple[google.protobuf.timestamp_pb2.Timestamp, \ ~.firestore_v1beta1.document.DocumentReference]: Pair of * The ``update_time`` when the document was created (or overwritten). * A document reference for the created document. Raises: ~google.cloud.exceptions.Conflict: If ``document_id`` is provided and the document already exists. """ if document_id is None: parent_path, expected_prefix = self._parent_info() document_pb = document_pb2.Document( fields=_helpers.encode_dict(document_data)) created_document_pb = self._client._firestore_api.create_document( parent_path, collection_id=self.id, document_id=None, document=document_pb, mask=None, metadata=self._client._rpc_metadata) new_document_id = _helpers.get_doc_id( created_document_pb, expected_prefix) document_ref = self.document(new_document_id) return created_document_pb.update_time, document_ref else: document_ref = self.document(document_id) write_result = document_ref.create(document_data) return write_result.update_time, document_ref
def _write_pb_for_set(document_path, document_data, merge): 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 write_pbs = write_pb2.Write( update=document_pb2.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_pb2.DocumentMask(field_paths=sorted(field_paths)) write_pbs.update_mask.CopyFrom(mask) return write_pbs
def _call_fut(values_dict): from google.cloud.firestore_v1beta1._helpers import encode_dict return encode_dict(values_dict)