25
25
26
26
27
27
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 == ""
36
38
37
39
# Repeat the same test but this time without `request_id`` set
38
40
# The `request_id` field should be automatically populated with
39
41
# a UUID4 value if it is not set.
40
42
# 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."
45
49
# Ensure that the uuid4 field is set according to AIP 4235
46
50
assert re .match (UUID4_RE , response .request_id )
47
51
assert len (response .request_id ) == 36
@@ -51,23 +55,27 @@ def test_unary_with_request_object(echo):
51
55
52
56
53
57
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 == ""
62
68
63
69
# Repeat the same test but this time without `request_id`` set
64
70
# The `request_id` field should be automatically populated with
65
71
# a UUID4 value if it is not set.
66
72
# 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."
71
79
assert re .match (UUID4_RE , response .request_id )
72
80
assert len (response .request_id ) == 36
73
81
# Ensure that the uuid4 field is set according to AIP 4235
@@ -76,59 +84,77 @@ def test_unary_with_dict(echo):
76
84
77
85
78
86
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 } "
80
89
# Note: InvalidArgument is from gRPC, BadRequest from http (no MTLS), InternalServerError from http (MTLS)
81
90
# 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
91
109
92
110
if isinstance (echo .transport , type (echo ).get_transport_class ("grpc" )):
93
111
# Under gRPC, we raise exceptions.InvalidArgument, which is a
94
112
# sub-class of exceptions.BadRequest.
95
113
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
104
124
105
125
106
126
if os .environ .get ("GAPIC_PYTHON_ASYNC" , "true" ) == "true" :
107
127
import asyncio
108
128
109
129
@pytest .mark .asyncio
110
130
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."
115
138
116
139
@pytest .mark .asyncio
117
140
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."
122
147
123
148
@pytest .mark .asyncio
124
149
async def test_async_unary_error (async_echo ):
125
- message = ' Bad things! Bad things!'
150
+ message = " Bad things! Bad things!"
126
151
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