Skip to content

Commit 28d3ed5

Browse files
authored
process: move assertion outside of pytest.raises context in showcase tests (#2101)
1 parent b3d7e99 commit 28d3ed5

File tree

2 files changed

+118
-74
lines changed

2 files changed

+118
-74
lines changed

tests/system/test_error_details.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
def create_status(error_details=None):
2424
status = rpc_status.status_pb2.Status()
2525
status.code = 3
26-
status.message = (
27-
"test"
28-
)
26+
status.message = "test"
2927
status_detail = any_pb2.Any()
3028
if error_details:
3129
status_detail.Pack(error_details)
@@ -47,14 +45,19 @@ def create_bad_request_details():
4745
field_violation.field = "test field"
4846
field_violation.description = "test description"
4947
return bad_request_details
48+
5049
bad_request_details = create_bad_request_details()
5150
status = create_status(bad_request_details)
5251

5352
with pytest.raises(exceptions.GoogleAPICallError) as e:
54-
_ = echo.echo(showcase.EchoRequest(
55-
error=status,
56-
))
57-
assert e.details == [bad_request_details]
53+
_ = echo.echo(
54+
showcase.EchoRequest(
55+
error=status,
56+
)
57+
)
58+
59+
# Note: error details are exposed as e.value.details.
60+
assert e.value.details == [bad_request_details]
5861

5962

6063
def test_precondition_failure_details(echo):
@@ -77,16 +80,31 @@ def create_precondition_failure_details():
7780
status = create_status(pf_details)
7881

7982
with pytest.raises(exceptions.GoogleAPICallError) as e:
80-
_ = echo.echo(showcase.EchoRequest(
81-
error=status,
82-
))
83-
assert e.details == [pf_details]
83+
_ = echo.echo(
84+
showcase.EchoRequest(
85+
error=status,
86+
)
87+
)
88+
89+
# Note: error details are exposed as e.value.details.
90+
assert e.value.details == [pf_details]
8491

8592

8693
def test_unknown_details(echo):
94+
# TODO(dovs): reenable when transcoding requests with an "Any"
95+
# field is properly handled
96+
# See https://github.com/googleapis/proto-plus-python/issues/285
97+
# for background and tracking.
98+
if "rest" in str(echo.transport).lower():
99+
return
100+
87101
status = create_status()
88102
with pytest.raises(exceptions.GoogleAPICallError) as e:
89-
_ = echo.echo(showcase.EchoRequest(
90-
error=status,
91-
))
92-
assert e.details == status.details
103+
_ = echo.echo(
104+
showcase.EchoRequest(
105+
error=status,
106+
)
107+
)
108+
109+
# Note: error details are exposed as e.value.details.
110+
assert e.value.details == list(status.details)

tests/system/test_unary.py

Lines changed: 85 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,27 @@
2525

2626

2727
def test_unary_with_request_object(echo):
28-
response = echo.echo(showcase.EchoRequest(
29-
content='The hail in Wales falls mainly on the snails.',
30-
request_id='some_value',
31-
other_request_id='',
32-
))
33-
assert response.content == 'The hail in Wales falls mainly on the snails.'
34-
assert response.request_id == 'some_value'
35-
assert response.other_request_id == ''
28+
response = echo.echo(
29+
showcase.EchoRequest(
30+
content="The hail in Wales falls mainly on the snails.",
31+
request_id="some_value",
32+
other_request_id="",
33+
)
34+
)
35+
assert response.content == "The hail in Wales falls mainly on the snails."
36+
assert response.request_id == "some_value"
37+
assert response.other_request_id == ""
3638

3739
# Repeat the same test but this time without `request_id`` set
3840
# The `request_id` field should be automatically populated with
3941
# a UUID4 value if it is not set.
4042
# See https://google.aip.dev/client-libraries/4235
41-
response = echo.echo(showcase.EchoRequest(
42-
content='The hail in Wales falls mainly on the snails.',
43-
))
44-
assert response.content == 'The hail in Wales falls mainly on the snails.'
43+
response = echo.echo(
44+
showcase.EchoRequest(
45+
content="The hail in Wales falls mainly on the snails.",
46+
)
47+
)
48+
assert response.content == "The hail in Wales falls mainly on the snails."
4549
# Ensure that the uuid4 field is set according to AIP 4235
4650
assert re.match(UUID4_RE, response.request_id)
4751
assert len(response.request_id) == 36
@@ -51,23 +55,27 @@ def test_unary_with_request_object(echo):
5155

5256

5357
def test_unary_with_dict(echo):
54-
response = echo.echo({
55-
'content': 'The hail in Wales falls mainly on the snails.',
56-
'request_id': 'some_value',
57-
'other_request_id': '',
58-
})
59-
assert response.content == 'The hail in Wales falls mainly on the snails.'
60-
assert response.request_id == 'some_value'
61-
assert response.other_request_id == ''
58+
response = echo.echo(
59+
{
60+
"content": "The hail in Wales falls mainly on the snails.",
61+
"request_id": "some_value",
62+
"other_request_id": "",
63+
}
64+
)
65+
assert response.content == "The hail in Wales falls mainly on the snails."
66+
assert response.request_id == "some_value"
67+
assert response.other_request_id == ""
6268

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

7785

7886
def test_unary_error(echo):
79-
message = 'Bad things! Bad things!'
87+
message = "Bad things! Bad things!"
88+
http_message = f"POST http://localhost:7469/v1beta1/echo:echo: {message}"
8089
# Note: InvalidArgument is from gRPC, BadRequest from http (no MTLS), InternalServerError from http (MTLS)
8190
# TODO: Reduce number of different exception types here.
82-
with pytest.raises((exceptions.InvalidArgument, exceptions.BadRequest, exceptions.InternalServerError)) as exc:
83-
echo.echo({
84-
'error': {
85-
'code': code_pb2.Code.Value('INVALID_ARGUMENT'),
86-
'message': message,
87-
},
88-
})
89-
assert exc.value.code == 400
90-
assert exc.value.message == message
91+
with pytest.raises(
92+
(
93+
exceptions.InvalidArgument,
94+
exceptions.BadRequest,
95+
exceptions.InternalServerError,
96+
)
97+
) as exc:
98+
echo.echo(
99+
{
100+
"error": {
101+
"code": code_pb2.Code.Value("INVALID_ARGUMENT"),
102+
"message": message,
103+
},
104+
}
105+
)
106+
err_message = message if "grpc" in str(echo.transport) else http_message
107+
assert exc.value.code == 400
108+
assert exc.value.message == err_message
91109

92110
if isinstance(echo.transport, type(echo).get_transport_class("grpc")):
93111
# Under gRPC, we raise exceptions.InvalidArgument, which is a
94112
# sub-class of exceptions.BadRequest.
95113
with pytest.raises(exceptions.InvalidArgument) as exc:
96-
echo.echo({
97-
'error': {
98-
'code': code_pb2.Code.Value('INVALID_ARGUMENT'),
99-
'message': message,
100-
},
101-
})
102-
assert exc.value.code == 400
103-
assert exc.value.message == message
114+
echo.echo(
115+
{
116+
"error": {
117+
"code": code_pb2.Code.Value("INVALID_ARGUMENT"),
118+
"message": message,
119+
},
120+
}
121+
)
122+
assert exc.value.code == 400
123+
assert exc.value.message == message
104124

105125

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

109129
@pytest.mark.asyncio
110130
async def test_async_unary_with_request_object(async_echo):
111-
response = await async_echo.echo(showcase.EchoRequest(
112-
content='The hail in Wales falls mainly on the snails.',
113-
), timeout=1)
114-
assert response.content == 'The hail in Wales falls mainly on the snails.'
131+
response = await async_echo.echo(
132+
showcase.EchoRequest(
133+
content="The hail in Wales falls mainly on the snails.",
134+
),
135+
timeout=1,
136+
)
137+
assert response.content == "The hail in Wales falls mainly on the snails."
115138

116139
@pytest.mark.asyncio
117140
async def test_async_unary_with_dict(async_echo):
118-
response = await async_echo.echo({
119-
'content': 'The hail in Wales falls mainly on the snails.',
120-
})
121-
assert response.content == 'The hail in Wales falls mainly on the snails.'
141+
response = await async_echo.echo(
142+
{
143+
"content": "The hail in Wales falls mainly on the snails.",
144+
}
145+
)
146+
assert response.content == "The hail in Wales falls mainly on the snails."
122147

123148
@pytest.mark.asyncio
124149
async def test_async_unary_error(async_echo):
125-
message = 'Bad things! Bad things!'
150+
message = "Bad things! Bad things!"
126151
with pytest.raises(exceptions.InvalidArgument) as exc:
127-
await async_echo.echo({
128-
'error': {
129-
'code': code_pb2.Code.Value('INVALID_ARGUMENT'),
130-
'message': message,
131-
},
132-
})
133-
assert exc.value.code == 400
134-
assert exc.value.message == message
152+
await async_echo.echo(
153+
{
154+
"error": {
155+
"code": code_pb2.Code.Value("INVALID_ARGUMENT"),
156+
"message": message,
157+
},
158+
}
159+
)
160+
assert exc.value.message == message

0 commit comments

Comments
 (0)