def test_update_document(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = firestore_client.FirestoreClient() # Mock request document = {} update_mask = {} # Mock response name = 'name3373707' expected_response = {'name': name} expected_response = document_pb2.Document(**expected_response) grpc_stub.UpdateDocument.return_value = expected_response response = client.update_document(document, update_mask) self.assertEqual(expected_response, response) grpc_stub.UpdateDocument.assert_called_once() args, kwargs = grpc_stub.UpdateDocument.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.UpdateDocumentRequest( document=document, update_mask=update_mask) self.assertEqual(expected_request, actual_request)
def test_update_document(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = document_pb2.Document(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_client.FirestoreClient() # Setup Request document = {} update_mask = {} response = client.update_document(document, update_mask) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.UpdateDocumentRequest( document=document, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def update_document(self, document, update_mask, mask=None, current_document=None, options=None): """ Updates or inserts a document. Example: >>> from google.cloud import firestore_v1beta1 >>> >>> client = firestore_v1beta1.FirestoreClient() >>> >>> document = {} >>> update_mask = {} >>> >>> response = client.update_document(document, update_mask) Args: document (Union[dict, ~google.cloud.firestore_v1beta1.types.Document]): The updated document. Creates the document if it does not already exist. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.Document` update_mask (Union[dict, ~google.cloud.firestore_v1beta1.types.DocumentMask]): The fields to update. None of the field paths in the mask may contain a reserved name. If the document exists on the server and has fields not referenced in the mask, they are left unchanged. Fields referenced in the mask, but not present in the input document, are deleted from the document on the server. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.DocumentMask` mask (Union[dict, ~google.cloud.firestore_v1beta1.types.DocumentMask]): The fields to return. If not set, returns all fields. If the document has a field that is not present in this mask, that field will not be returned in the response. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.DocumentMask` current_document (Union[dict, ~google.cloud.firestore_v1beta1.types.Precondition]): An optional precondition on the document. The request will fail if this is set and not met by the target document. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.Precondition` options (~google.gax.CallOptions): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`~google.cloud.firestore_v1beta1.types.Document` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ request = firestore_pb2.UpdateDocumentRequest( document=document, update_mask=update_mask, mask=mask, current_document=current_document) return self._update_document(request, options)
def test_update_document(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = document_pb2.Document(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request document = {} update_mask = {} response = client.update_document(document, update_mask) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.UpdateDocumentRequest( document=document, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request