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
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
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)
def _collections_helper(self, page_size=None): from google.api_core import grpc_helpers from google.cloud.firestore_v1beta1.collection import ( CollectionReference) from google.cloud.firestore_v1beta1.gapic.firestore_client import ( FirestoreClient) from google.cloud.firestore_v1beta1.proto import firestore_pb2 collection_ids = ['coll-1', 'coll-2'] list_coll_response = firestore_pb2.ListCollectionIdsResponse( collection_ids=collection_ids) channel = grpc_helpers.ChannelStub() api_client = FirestoreClient(channel=channel) channel.ListCollectionIds.response = list_coll_response client = _make_client() client._firestore_api_internal = api_client # Actually make a document and call delete(). document = self._make_one('where', 'we-are', client=client) if page_size is not None: collections = list(document.collections(page_size=page_size)) else: collections = list(document.collections()) # Verify the response and the mocks. self.assertEqual(len(collections), len(collection_ids)) for collection, collection_id in zip(collections, collection_ids): self.assertIsInstance(collection, CollectionReference) self.assertEqual(collection.parent, document) self.assertEqual(collection.id, collection_id) request, = channel.ListCollectionIds.requests self.assertEqual(request.parent, document._document_path) if page_size is None: self.assertEqual(request.page_size, 0) else: self.assertEqual(request.page_size, page_size)