def test_list_operations(): channel = grpc_helpers.ChannelStub() client = operations_v1.OperationsClient(channel) operations = [ operations_pb2.Operation(name="1"), operations_pb2.Operation(name="2"), ] list_response = operations_pb2.ListOperationsResponse( operations=operations) channel.ListOperations.response = list_response response = client.list_operations("name", "filter", metadata=[("header", "foo")]) assert isinstance(response, page_iterator.Iterator) assert list(response) == operations assert ("header", "foo") in channel.ListOperations.calls[0].metadata assert ("x-goog-request-params", "name=name") in channel.ListOperations.calls[0].metadata assert len(channel.ListOperations.requests) == 1 request = channel.ListOperations.requests[0] assert isinstance(request, operations_pb2.ListOperationsRequest) assert request.name == "name" assert request.filter == "filter"
def test_constructor(self): from google.cloud.trace_v1.gapic import trace_service_client channel = grpc_helpers.ChannelStub() gapic_client = trace_service_client.TraceServiceClient(channel=channel) _, api = self._make_one(gapic_client, mock.sentinel.client) self.assertIs(api._gapic_api, gapic_client) self.assertIs(api.client, mock.sentinel.client)
def test_multiple_responses_and_single_response_error(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) channel.GetOperation.responses = [] channel.GetOperation.response = mock.sentinel.response with pytest.raises(ValueError): stub.GetOperation(operations_pb2.GetOperationRequest())
def test_no_response(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") with pytest.raises(ValueError) as exc_info: stub.GetOperation(expected_request) assert exc_info.match("GetOperation")
def test_exception_response(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") channel.GetOperation.response = RuntimeError() with pytest.raises(RuntimeError): stub.GetOperation(expected_request)
def test_cancel_operation(): channel = grpc_helpers.ChannelStub() client = operations_v1.OperationsClient(channel) channel.CancelOperation.response = empty_pb2.Empty() client.cancel_operation('name') assert len(channel.CancelOperation.requests) == 1 assert channel.CancelOperation.requests[0].name == 'name'
def _make_one(self, gapic_client=None, handwritten_client=None): from google.cloud.trace_v1.gapic import trace_service_client channel = grpc_helpers.ChannelStub() if gapic_client is None: gapic_client = trace_service_client.TraceServiceClient( channel=channel) if handwritten_client is None: handwritten_client = mock.Mock() api = self._get_target_class()(gapic_client, handwritten_client) return channel, api
def test_get_operation(): channel = grpc_helpers.ChannelStub() client = operations_v1.OperationsClient(channel) channel.GetOperation.response = operations_pb2.Operation(name='meep') response = client.get_operation('name') assert len(channel.GetOperation.requests) == 1 assert channel.GetOperation.requests[0].name == 'name' assert response == channel.GetOperation.response
def test_cancel_operation(): channel = grpc_helpers.ChannelStub() client = operations_v1.OperationsClient(channel) channel.CancelOperation.response = empty_pb2.Empty() client.cancel_operation("name", metadata=[("header", "foo")]) assert ("header", "foo") in channel.CancelOperation.calls[0].metadata assert ("x-goog-request-params", "name=name") in channel.CancelOperation.calls[0].metadata assert len(channel.CancelOperation.requests) == 1 assert channel.CancelOperation.requests[0].name == "name"
def test_get_operation(): channel = grpc_helpers.ChannelStub() client = operations_v1.OperationsClient(channel) channel.GetOperation.response = operations_pb2.Operation(name="meep") response = client.get_operation("name", metadata=[("header", "foo")]) assert ("header", "foo") in channel.GetOperation.calls[0].metadata assert ("x-goog-request-params", "name=name") in channel.GetOperation.calls[0].metadata assert len(channel.GetOperation.requests) == 1 assert channel.GetOperation.requests[0].name == "name" assert response == channel.GetOperation.response
def test_single_response(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") expected_response = operations_pb2.Operation(name="moop") channel.GetOperation.response = expected_response response = stub.GetOperation(expected_request) assert response == expected_response assert channel.requests == [("GetOperation", expected_request)] assert channel.GetOperation.requests == [expected_request]
def test_callable_response(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") expected_response = operations_pb2.Operation(name="moop") on_get_operation = mock.Mock(spec=("__call__",), return_value=expected_response) channel.GetOperation.response = on_get_operation response = stub.GetOperation(expected_request) assert response == expected_response on_get_operation.assert_called_once_with(expected_request)
def test_call_info(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name='meep') expected_response = operations_pb2.Operation(name='moop') expected_metadata = [('red', 'blue'), ('two', 'shoe')] expected_credentials = mock.sentinel.credentials channel.GetOperation.response = expected_response response = stub.GetOperation( expected_request, timeout=42, metadata=expected_metadata, credentials=expected_credentials) assert response == expected_response assert channel.requests == [('GetOperation', expected_request)] assert channel.GetOperation.calls == [ (expected_request, 42, expected_metadata, expected_credentials)]
def test_list_operations(): channel = grpc_helpers.ChannelStub() client = operations_v1.OperationsClient(channel) operations = [ operations_pb2.Operation(name='1'), operations_pb2.Operation(name='2') ] list_response = operations_pb2.ListOperationsResponse( operations=operations) channel.ListOperations.response = list_response response = client.list_operations('name', 'filter') assert isinstance(response, page_iterator.Iterator) assert list(response) == operations assert len(channel.ListOperations.requests) == 1 request = channel.ListOperations.requests[0] assert isinstance(request, operations_pb2.ListOperationsRequest) assert request.name == 'name' assert request.filter == 'filter'
def test_call_info(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") expected_response = operations_pb2.Operation(name="moop") expected_metadata = [("red", "blue"), ("two", "shoe")] expected_credentials = mock.sentinel.credentials channel.GetOperation.response = expected_response response = stub.GetOperation( expected_request, timeout=42, metadata=expected_metadata, credentials=expected_credentials, ) assert response == expected_response assert channel.requests == [("GetOperation", expected_request)] assert channel.GetOperation.calls == [ (expected_request, 42, expected_metadata, expected_credentials) ]
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)
def test_multiple_responses(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") expected_responses = [ operations_pb2.Operation(name="foo"), operations_pb2.Operation(name="bar"), operations_pb2.Operation(name="baz"), ] channel.GetOperation.responses = iter(expected_responses) response1 = stub.GetOperation(expected_request) response2 = stub.GetOperation(expected_request) response3 = stub.GetOperation(expected_request) assert response1 == expected_responses[0] assert response2 == expected_responses[1] assert response3 == expected_responses[2] assert channel.requests == [("GetOperation", expected_request)] * 3 assert channel.GetOperation.requests == [expected_request] * 3 with pytest.raises(StopIteration): stub.GetOperation(expected_request)
def test_close(self): channel = grpc_helpers.ChannelStub() assert channel.close() is None
def test_subscribe_unsubscribe(self): channel = grpc_helpers.ChannelStub() assert channel.subscribe(None) is None assert channel.unsubscribe(None) is None
def test_stream_stream(self): channel = grpc_helpers.ChannelStub() method_name = "GetOperation" callable_stub = channel.stream_stream(method_name) assert callable_stub._method == method_name assert callable_stub._channel == channel
def make_sinks_api(): channel = grpc_helpers.ChannelStub() gapic_client = config_service_v2_client.ConfigServiceV2Client(channel=channel) handwritten_client = mock.Mock() api = _gapic._SinksAPI(gapic_client, handwritten_client) return channel, api
def make_metrics_api(): channel = grpc_helpers.ChannelStub() gapic_client = metrics_service_v2_client.MetricsServiceV2Client(channel=channel) handwritten_client = mock.Mock() api = _gapic._MetricsAPI(gapic_client, handwritten_client) return channel, api
def test_ctor(self): channel = grpc_helpers.ChannelStub() gapic_client = metrics_service_v2_client.MetricsServiceV2Client(channel=channel) api = _gapic._MetricsAPI(gapic_client, mock.sentinel.client) assert api._gapic_api is gapic_client assert api._client is mock.sentinel.client
def make_logging_api(): channel = grpc_helpers.ChannelStub() gapic_client = logging_service_v2_client.LoggingServiceV2Client(channel=channel) handwritten_client = mock.Mock() api = _gapic._LoggingAPI(gapic_client, handwritten_client) return channel, api
def test_missing_method(self): channel = grpc_helpers.ChannelStub() with pytest.raises(AttributeError): channel.DoesNotExist.response
def test_ctor(self): channel = grpc_helpers.ChannelStub() gapic_client = logging_service_v2_client.LoggingServiceV2Client(channel=channel) api = _gapic._LoggingAPI(gapic_client, mock.sentinel.client) assert api._gapic_api is gapic_client assert api._client is mock.sentinel.client
def test_unary_unary(self): channel = grpc_helpers.ChannelStub() method_name = 'GetOperation' callable_stub = channel.unary_unary(method_name) assert callable_stub._method == method_name assert callable_stub._channel == channel