async def test_async_stream_unary_writer(async_echo): call = await async_echo.collect() await call.write(showcase.EchoRequest(content="hello")) await call.write(showcase.EchoRequest(content="world!")) await call.done_writing() response = await call assert response.content == 'hello world!'
async def test_async_stream_unary_iterable(async_echo): requests = [] requests.append(showcase.EchoRequest(content="hello")) requests.append(showcase.EchoRequest(content="world!")) call = await async_echo.collect(requests) response = await call assert response.content == 'hello world!'
def test_stream_stream(intercepted_echo): requests = [] requests.append(showcase.EchoRequest(content="hello")) requests.append(showcase.EchoRequest(content="world!")) responses = intercepted_echo.chat(iter(requests)) contents = [response.content for response in responses] assert contents == ['hello', 'world!'] assert responses.trailing_metadata() == intercepted_metadata
def test_stream_unary(echo): if isinstance(echo.transport, type(echo).get_transport_class("rest")): # (TODO: dovs) Temporarily disabling rest return requests = [] requests.append(showcase.EchoRequest(content="hello")) requests.append(showcase.EchoRequest(content="world!")) response = echo.collect(iter(requests)) assert response.content == 'hello world!'
async def test_async_stream_stream_reader_writier(async_echo): call = await async_echo.chat(metadata=metadata) await call.write(showcase.EchoRequest(content="hello")) await call.write(showcase.EchoRequest(content="world!")) await call.done_writing() contents = [(await call.read()).content, (await call.read()).content] assert contents == ['hello', 'world!'] trailing_metadata = await call.trailing_metadata() assert trailing_metadata == metadata
def test_stream_stream(echo): requests = [] requests.append(showcase.EchoRequest(content="hello")) requests.append(showcase.EchoRequest(content="world!")) responses = echo.chat(iter(requests), metadata=metadata) contents = [] for response in responses: contents.append(response.content) assert contents == ['hello', 'world!'] assert responses.trailing_metadata() == metadata
def test_stream_stream(echo): if isinstance(echo.transport, type(echo).get_transport_class("rest")): # (TODO: dovs) Temporarily disabling rest return requests = [] requests.append(showcase.EchoRequest(content="hello")) requests.append(showcase.EchoRequest(content="world!")) responses = echo.chat(iter(requests), metadata=metadata) contents = [] for response in responses: contents.append(response.content) assert contents == ['hello', 'world!'] assert responses.trailing_metadata() == metadata
def test_sample(): ssl_credentials = grpc.ssl_channel_credentials(root_certificates=cert, certificate_chain=cert, private_key=key) with mock.patch("grpc.ssl_channel_credentials", autospec=True) as mock_ssl_cred: mock_ssl_cred.return_value = ssl_credentials client = EchoClient( credentials=credentials.AnonymousCredentials(), client_options=client_options, ) mock_ssl_cred.assert_called_once_with(certificate_chain=cert, private_key=key) response = client.echo( showcase.EchoRequest( content="The hail in Wales falls mainly on the snails.")) assert response.content == "The hail in Wales falls mainly on the snails."
def test_bad_request_details(echo): # TODO(dovs): reenable when transcoding requests with an "Any" # field is properly handled # See https://github.com/googleapis/proto-plus-python/issues/285 # for background and tracking. if "rest" in str(echo.transport).lower(): return def create_bad_request_details(): bad_request_details = error_details_pb2.BadRequest() field_violation = bad_request_details.field_violations.add() field_violation.field = "test field" field_violation.description = "test description" return bad_request_details bad_request_details = create_bad_request_details() status = create_status(bad_request_details) with pytest.raises(exceptions.GoogleAPICallError) as e: _ = echo.echo(showcase.EchoRequest(error=status, )) assert e.details == [bad_request_details]
def test_precondition_failure_details(echo): # TODO(dovs): reenable when transcoding requests with an "Any" # field is properly handled # See https://github.com/googleapis/proto-plus-python/issues/285 # for background and tracking. if "rest" in str(echo.transport).lower(): return def create_precondition_failure_details(): pf_details = error_details_pb2.PreconditionFailure() violation = pf_details.violations.add() violation.type = "test type" violation.subject = "test subject" violation.description = "test description" return pf_details pf_details = create_precondition_failure_details() status = create_status(pf_details) with pytest.raises(exceptions.GoogleAPICallError) as e: _ = echo.echo(showcase.EchoRequest(error=status, )) assert e.details == [pf_details]
def test_unary_with_request_object(echo): response = echo.echo( showcase.EchoRequest( content='The hail in Wales falls mainly on the snails.', )) assert response.content == 'The hail in Wales falls mainly on the snails.'
async def test_async_unary_with_request_object(async_echo): response = await async_echo.echo(showcase.EchoRequest( content='The hail in Wales falls mainly on the snails.', ), timeout=1) assert response.content == 'The hail in Wales falls mainly on the snails.'
def test_stream_unary(echo): requests = [] requests.append(showcase.EchoRequest(content="hello")) requests.append(showcase.EchoRequest(content="world!")) response = echo.collect(iter(requests)) assert response.content == 'hello world!'
async def async_generator(): yield showcase.EchoRequest(content="hello") yield showcase.EchoRequest(content="world!")
def test_unknown_details(echo): status = create_status() with pytest.raises(exceptions.GoogleAPICallError) as e: _ = echo.echo(showcase.EchoRequest(error=status, )) assert e.details == status.details