Skip to content

Commit

Permalink
process: move assertion outside of pytest.raises context in showcas…
Browse files Browse the repository at this point in the history
…e tests (#2101)
  • Loading branch information
ohmayr authored and parthea committed Aug 27, 2024
1 parent fd75b1b commit 1fca983
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 74 deletions.
48 changes: 33 additions & 15 deletions tests/system/test_error_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
def create_status(error_details=None):
status = rpc_status.status_pb2.Status()
status.code = 3
status.message = (
"test"
)
status.message = "test"
status_detail = any_pb2.Any()
if error_details:
status_detail.Pack(error_details)
Expand All @@ -47,14 +45,19 @@ def create_bad_request_details():
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]
_ = echo.echo(
showcase.EchoRequest(
error=status,
)
)

# Note: error details are exposed as e.value.details.
assert e.value.details == [bad_request_details]


def test_precondition_failure_details(echo):
Expand All @@ -77,16 +80,31 @@ def 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]
_ = echo.echo(
showcase.EchoRequest(
error=status,
)
)

# Note: error details are exposed as e.value.details.
assert e.value.details == [pf_details]


def test_unknown_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

status = create_status()
with pytest.raises(exceptions.GoogleAPICallError) as e:
_ = echo.echo(showcase.EchoRequest(
error=status,
))
assert e.details == status.details
_ = echo.echo(
showcase.EchoRequest(
error=status,
)
)

# Note: error details are exposed as e.value.details.
assert e.value.details == list(status.details)
144 changes: 85 additions & 59 deletions tests/system/test_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,27 @@


def test_unary_with_request_object(echo):
response = echo.echo(showcase.EchoRequest(
content='The hail in Wales falls mainly on the snails.',
request_id='some_value',
other_request_id='',
))
assert response.content == 'The hail in Wales falls mainly on the snails.'
assert response.request_id == 'some_value'
assert response.other_request_id == ''
response = echo.echo(
showcase.EchoRequest(
content="The hail in Wales falls mainly on the snails.",
request_id="some_value",
other_request_id="",
)
)
assert response.content == "The hail in Wales falls mainly on the snails."
assert response.request_id == "some_value"
assert response.other_request_id == ""

# Repeat the same test but this time without `request_id`` set
# The `request_id` field should be automatically populated with
# a UUID4 value if it is not set.
# See https://google.aip.dev/client-libraries/4235
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.'
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."
# Ensure that the uuid4 field is set according to AIP 4235
assert re.match(UUID4_RE, response.request_id)
assert len(response.request_id) == 36
Expand All @@ -51,23 +55,27 @@ def test_unary_with_request_object(echo):


def test_unary_with_dict(echo):
response = echo.echo({
'content': 'The hail in Wales falls mainly on the snails.',
'request_id': 'some_value',
'other_request_id': '',
})
assert response.content == 'The hail in Wales falls mainly on the snails.'
assert response.request_id == 'some_value'
assert response.other_request_id == ''
response = echo.echo(
{
"content": "The hail in Wales falls mainly on the snails.",
"request_id": "some_value",
"other_request_id": "",
}
)
assert response.content == "The hail in Wales falls mainly on the snails."
assert response.request_id == "some_value"
assert response.other_request_id == ""

# Repeat the same test but this time without `request_id`` set
# The `request_id` field should be automatically populated with
# a UUID4 value if it is not set.
# See https://google.aip.dev/client-libraries/4235
response = echo.echo({
'content': 'The hail in Wales falls mainly on the snails.',
})
assert response.content == 'The hail in Wales falls mainly on the snails.'
response = echo.echo(
{
"content": "The hail in Wales falls mainly on the snails.",
}
)
assert response.content == "The hail in Wales falls mainly on the snails."
assert re.match(UUID4_RE, response.request_id)
assert len(response.request_id) == 36
# Ensure that the uuid4 field is set according to AIP 4235
Expand All @@ -76,59 +84,77 @@ def test_unary_with_dict(echo):


def test_unary_error(echo):
message = 'Bad things! Bad things!'
message = "Bad things! Bad things!"
http_message = f"POST http://localhost:7469/v1beta1/echo:echo: {message}"
# Note: InvalidArgument is from gRPC, BadRequest from http (no MTLS), InternalServerError from http (MTLS)
# TODO: Reduce number of different exception types here.
with pytest.raises((exceptions.InvalidArgument, exceptions.BadRequest, exceptions.InternalServerError)) as exc:
echo.echo({
'error': {
'code': code_pb2.Code.Value('INVALID_ARGUMENT'),
'message': message,
},
})
assert exc.value.code == 400
assert exc.value.message == message
with pytest.raises(
(
exceptions.InvalidArgument,
exceptions.BadRequest,
exceptions.InternalServerError,
)
) as exc:
echo.echo(
{
"error": {
"code": code_pb2.Code.Value("INVALID_ARGUMENT"),
"message": message,
},
}
)
err_message = message if "grpc" in str(echo.transport) else http_message
assert exc.value.code == 400
assert exc.value.message == err_message

if isinstance(echo.transport, type(echo).get_transport_class("grpc")):
# Under gRPC, we raise exceptions.InvalidArgument, which is a
# sub-class of exceptions.BadRequest.
with pytest.raises(exceptions.InvalidArgument) as exc:
echo.echo({
'error': {
'code': code_pb2.Code.Value('INVALID_ARGUMENT'),
'message': message,
},
})
assert exc.value.code == 400
assert exc.value.message == message
echo.echo(
{
"error": {
"code": code_pb2.Code.Value("INVALID_ARGUMENT"),
"message": message,
},
}
)
assert exc.value.code == 400
assert exc.value.message == message


if os.environ.get("GAPIC_PYTHON_ASYNC", "true") == "true":
import asyncio

@pytest.mark.asyncio
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.'
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."

@pytest.mark.asyncio
async def test_async_unary_with_dict(async_echo):
response = await async_echo.echo({
'content': 'The hail in Wales falls mainly on the snails.',
})
assert response.content == 'The hail in Wales falls mainly on the snails.'
response = await async_echo.echo(
{
"content": "The hail in Wales falls mainly on the snails.",
}
)
assert response.content == "The hail in Wales falls mainly on the snails."

@pytest.mark.asyncio
async def test_async_unary_error(async_echo):
message = 'Bad things! Bad things!'
message = "Bad things! Bad things!"
with pytest.raises(exceptions.InvalidArgument) as exc:
await async_echo.echo({
'error': {
'code': code_pb2.Code.Value('INVALID_ARGUMENT'),
'message': message,
},
})
assert exc.value.code == 400
assert exc.value.message == message
await async_echo.echo(
{
"error": {
"code": code_pb2.Code.Value("INVALID_ARGUMENT"),
"message": message,
},
}
)
assert exc.value.message == message

0 comments on commit 1fca983

Please sign in to comment.