示例#1
0
    def test_create_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
        parent = client.any_path_path("[PROJECT]", "[DATABASE]", "[DOCUMENT]",
                                      "[ANY_PATH]")
        collection_id = "collectionId-821242276"
        document_id = "documentId506676927"
        document = {}

        response = client.create_document(parent, collection_id, document_id,
                                          document)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.CreateDocumentRequest(
            parent=parent,
            collection_id=collection_id,
            document_id=document_id,
            document=document,
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_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
        parent = "parent-995424086"
        collection_id = "collectionId-821242276"
        document = {}

        response = client.create_document(parent, collection_id, "documentid",
                                          document)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.CreateDocumentRequest(
            parent=parent,
            collection_id=collection_id,
            document_id="documentid",
            document=document,
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#3
0
    def test_create_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
        parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]',
                                      '[ANY_PATH]')
        collection_id = 'collectionId-821242276'
        document_id = 'documentId506676927'
        document = {}

        response = client.create_document(parent, collection_id, document_id,
                                          document)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.CreateDocumentRequest(
            parent=parent,
            collection_id=collection_id,
            document_id=document_id,
            document=document)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#4
0
    def create_document(self,
                        parent,
                        collection_id,
                        document_id,
                        document,
                        mask=None,
                        options=None):
        """
        Creates a new document.

        Example:
            >>> from google.cloud import firestore_v1beta1
            >>>
            >>> client = firestore_v1beta1.FirestoreClient()
            >>>
            >>> parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]')
            >>> collection_id = ''
            >>> document_id = ''
            >>> document = {}
            >>>
            >>> response = client.create_document(parent, collection_id, document_id, document)

        Args:
            parent (str): The parent resource. For example:
                ``projects/{project_id}/databases/{database_id}/documents`` or
                ``projects/{project_id}/databases/{database_id}/documents/chatrooms/{chatroom_id}``
            collection_id (str): The collection ID, relative to ``parent``, to list. For example: ``chatrooms``.
            document_id (str): The client-assigned document ID to use for this document.

                Optional. If not specified, an ID will be assigned by the service.
            document (Union[dict, ~google.cloud.firestore_v1beta1.types.Document]): The document to create. ``name`` must not be set.
                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.firestore_v1beta1.types.Document`
            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`
            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.CreateDocumentRequest(
            parent=parent,
            collection_id=collection_id,
            document_id=document_id,
            document=document,
            mask=mask)
        return self._create_document(request, options)
示例#5
0
    def test_create_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
        parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]',
                                      '[ANY_PATH]')
        collection_id = 'collectionId-821242276'
        document_id = 'documentId506676927'
        document = {}

        # Mock response
        name = 'name3373707'
        expected_response = {'name': name}
        expected_response = document_pb2.Document(**expected_response)
        grpc_stub.CreateDocument.return_value = expected_response

        response = client.create_document(parent, collection_id, document_id,
                                          document)
        self.assertEqual(expected_response, response)

        grpc_stub.CreateDocument.assert_called_once()
        args, kwargs = grpc_stub.CreateDocument.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = firestore_pb2.CreateDocumentRequest(
            parent=parent,
            collection_id=collection_id,
            document_id=document_id,
            document=document)
        self.assertEqual(expected_request, actual_request)