示例#1
0
    def test_list_collection_ids(self):
        # Setup Expected Response
        next_page_token = ""
        collection_ids_element = "collectionIdsElement1368994900"
        collection_ids = [collection_ids_element]
        expected_response = {
            "next_page_token": next_page_token,
            "collection_ids": collection_ids,
        }
        expected_response = firestore_pb2.ListCollectionIdsResponse(
            **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]")

        paged_list_response = client.list_collection_ids(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.collection_ids[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.ListCollectionIdsRequest(
            parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#2
0
    def test_list_collection_ids(self):
        # Setup Expected Response
        next_page_token = ''
        collection_ids_element = 'collectionIdsElement1368994900'
        collection_ids = [collection_ids_element]
        expected_response = {
            'next_page_token': next_page_token,
            'collection_ids': collection_ids
        }
        expected_response = firestore_pb2.ListCollectionIdsResponse(
            **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]')

        paged_list_response = client.list_collection_ids(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.collection_ids[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.ListCollectionIdsRequest(
            parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#3
0
    def list_collection_ids(self, parent, page_size=None, options=None):
        """
        Lists all the collection IDs underneath a document.

        Example:
            >>> from google.cloud import firestore_v1beta1
            >>> from google.gax import CallOptions, INITIAL_PAGE
            >>>
            >>> client = firestore_v1beta1.FirestoreClient()
            >>>
            >>> parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]')
            >>>
            >>>
            >>> # Iterate over all results
            >>> for element in client.list_collection_ids(parent):
            ...     # process element
            ...     pass
            >>>
            >>> # Or iterate over results one page at a time
            >>> for page in client.list_collection_ids(parent, options=CallOptions(page_token=INITIAL_PAGE)):
            ...     for element in page:
            ...         # process element
            ...         pass

        Args:
            parent (str): The parent document. In the format:
                ``projects/{project_id}/databases/{database_id}/documents/{document_path}``.
                For example:
                ``projects/my-project/databases/my-database/documents/chatrooms/my-chatroom``
            page_size (int): The maximum number of resources contained in the
                underlying API response. If page streaming is performed per-
                resource, this parameter does not affect the return value. If page
                streaming is performed per-page, this determines the maximum number
                of resources in a page.
            options (~google.gax.CallOptions): Overrides the default
                settings for this call, e.g, timeout, retries etc.

        Returns:
            A :class:`~google.gax.PageIterator` instance. By default, this
            is an iterable of :class:`str` instances.
            This object can also be configured to iterate over the pages
            of the response through the `options` parameter.

        Raises:
            :exc:`google.gax.errors.GaxError` if the RPC is aborted.
            :exc:`ValueError` if the parameters are invalid.
        """
        request = firestore_pb2.ListCollectionIdsRequest(parent=parent,
                                                         page_size=page_size)
        return self._list_collection_ids(request, options)
示例#4
0
    def test_list_collection_ids(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]')

        # Mock response
        next_page_token = ''
        collection_ids_element = 'collectionIdsElement1368994900'
        collection_ids = [collection_ids_element]
        expected_response = {
            'next_page_token': next_page_token,
            'collection_ids': collection_ids
        }
        expected_response = firestore_pb2.ListCollectionIdsResponse(
            **expected_response)
        grpc_stub.ListCollectionIds.return_value = expected_response

        paged_list_response = client.list_collection_ids(parent)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.collection_ids[0], resources[0])

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

        expected_request = firestore_pb2.ListCollectionIdsRequest(
            parent=parent)
        self.assertEqual(expected_request, actual_request)