2525
2626
2727def 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
5357def 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
7886def 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
106126if 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