Exemple #1
0
    def test_list_documents(self):
        # Setup Expected Response
        next_page_token = ""
        documents_element = {}
        documents = [documents_element]
        expected_response = {
            "next_page_token": next_page_token,
            "documents": documents
        }
        expected_response = firestore_pb2.ListDocumentsResponse(
            **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"

        paged_list_response = client.list_documents(parent, collection_id)
        resources = list(paged_list_response)
        assert len(resources) == 1

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

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.ListDocumentsRequest(
            parent=parent, collection_id=collection_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #2
0
    def test_list_documents(self):
        # Setup Expected Response
        next_page_token = ''
        documents_element = {}
        documents = [documents_element]
        expected_response = {
            'next_page_token': next_page_token,
            'documents': documents
        }
        expected_response = firestore_pb2.ListDocumentsResponse(
            **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'

        paged_list_response = client.list_documents(parent, collection_id)
        resources = list(paged_list_response)
        assert len(resources) == 1

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

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.ListDocumentsRequest(
            parent=parent, collection_id=collection_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemple #3
0
    def test_list_documents(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'

        # Mock response
        next_page_token = ''
        documents_element = {}
        documents = [documents_element]
        expected_response = {
            'next_page_token': next_page_token,
            'documents': documents
        }
        expected_response = firestore_pb2.ListDocumentsResponse(
            **expected_response)
        grpc_stub.ListDocuments.return_value = expected_response

        paged_list_response = client.list_documents(parent, collection_id)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.documents[0], resources[0])

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

        expected_request = firestore_pb2.ListDocumentsRequest(
            parent=parent, collection_id=collection_id)
        self.assertEqual(expected_request, actual_request)