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_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_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)