@@ -106,6 +106,67 @@ def test__request_not_200(self):
106106 [{'method' : METHOD , 'project' : PROJECT }])
107107
108108
109+ @unittest .skipUnless (_HAVE_GRPC , 'No gRPC' )
110+ class Test__grpc_catch_rendezvous (unittest .TestCase ):
111+
112+ def _callFUT (self , to_call , * args , ** kwargs ):
113+ from google .cloud .datastore .connection import _grpc_catch_rendezvous
114+ return _grpc_catch_rendezvous (to_call , * args , ** kwargs )
115+
116+ @staticmethod
117+ def _fake_method (exc , result = None ):
118+ if exc is None :
119+ return result
120+ else :
121+ raise exc
122+
123+ def test_success (self ):
124+ expected = object ()
125+ result = self ._callFUT (self ._fake_method , None , expected )
126+ self .assertIs (result , expected )
127+
128+ def test_failure_aborted (self ):
129+ from grpc import StatusCode
130+ from grpc ._channel import _RPCState
131+ from google .cloud .exceptions import Conflict
132+ from google .cloud .exceptions import GrpcRendezvous
133+
134+ details = 'Bad things.'
135+ exc_state = _RPCState ((), None , None , StatusCode .ABORTED , details )
136+ exc = GrpcRendezvous (exc_state , None , None , None )
137+ with self .assertRaises (Conflict ):
138+ self ._callFUT (self ._fake_method , exc )
139+
140+ def test_failure_invalid_argument (self ):
141+ from grpc import StatusCode
142+ from grpc ._channel import _RPCState
143+ from google .cloud .exceptions import BadRequest
144+ from google .cloud .exceptions import GrpcRendezvous
145+
146+ details = ('Cannot have inequality filters on multiple '
147+ 'properties: [created, priority]' )
148+ exc_state = _RPCState ((), None , None ,
149+ StatusCode .INVALID_ARGUMENT , details )
150+ exc = GrpcRendezvous (exc_state , None , None , None )
151+ with self .assertRaises (BadRequest ):
152+ self ._callFUT (self ._fake_method , exc )
153+
154+ def test_failure_cancelled (self ):
155+ from grpc import StatusCode
156+ from grpc ._channel import _RPCState
157+ from google .cloud .exceptions import GrpcRendezvous
158+
159+ exc_state = _RPCState ((), None , None , StatusCode .CANCELLED , None )
160+ exc = GrpcRendezvous (exc_state , None , None , None )
161+ with self .assertRaises (GrpcRendezvous ):
162+ self ._callFUT (self ._fake_method , exc )
163+
164+ def test_commit_failure_non_grpc_err (self ):
165+ exc = RuntimeError ('Not a gRPC error' )
166+ with self .assertRaises (RuntimeError ):
167+ self ._callFUT (self ._fake_method , exc )
168+
169+
109170class Test_DatastoreAPIOverGRPC (unittest .TestCase ):
110171
111172 def _getTargetClass (self ):
@@ -227,16 +288,6 @@ def test_run_query_invalid_argument(self):
227288 exc = GrpcRendezvous (exc_state , None , None , None )
228289 self ._run_query_failure_helper (exc , BadRequest )
229290
230- @unittest .skipUnless (_HAVE_GRPC , 'No gRPC' )
231- def test_run_query_cancelled (self ):
232- from grpc import StatusCode
233- from grpc ._channel import _RPCState
234- from google .cloud .exceptions import GrpcRendezvous
235-
236- exc_state = _RPCState ((), None , None , StatusCode .CANCELLED , None )
237- exc = GrpcRendezvous (exc_state , None , None , None )
238- self ._run_query_failure_helper (exc , GrpcRendezvous )
239-
240291 def test_begin_transaction (self ):
241292 return_val = object ()
242293 stub = _GRPCStub (return_val )
@@ -264,59 +315,6 @@ def test_commit_success(self):
264315 self .assertEqual (stub .method_calls ,
265316 [(request_pb , 'Commit' )])
266317
267- def _commit_failure_helper (self , exc , err_class ):
268- stub = _GRPCStub (side_effect = exc )
269- datastore_api = self ._makeOne (stub = stub )
270-
271- request_pb = _RequestPB ()
272- project = 'PROJECT'
273- with self .assertRaises (err_class ):
274- datastore_api .commit (project , request_pb )
275-
276- self .assertEqual (request_pb .project_id , project )
277- self .assertEqual (stub .method_calls ,
278- [(request_pb , 'Commit' )])
279-
280- @unittest .skipUnless (_HAVE_GRPC , 'No gRPC' )
281- def test_commit_failure_aborted (self ):
282- from grpc import StatusCode
283- from grpc ._channel import _RPCState
284- from google .cloud .exceptions import Conflict
285- from google .cloud .exceptions import GrpcRendezvous
286-
287- details = 'Bad things.'
288- exc_state = _RPCState ((), None , None , StatusCode .ABORTED , details )
289- exc = GrpcRendezvous (exc_state , None , None , None )
290- self ._commit_failure_helper (exc , Conflict )
291-
292- @unittest .skipUnless (_HAVE_GRPC , 'No gRPC' )
293- def test_commit_failure_invalid_argument (self ):
294- from grpc import StatusCode
295- from grpc ._channel import _RPCState
296- from google .cloud .exceptions import BadRequest
297- from google .cloud .exceptions import GrpcRendezvous
298-
299- details = 'Too long content.'
300- exc_state = _RPCState ((), None , None ,
301- StatusCode .INVALID_ARGUMENT , details )
302- exc = GrpcRendezvous (exc_state , None , None , None )
303- self ._commit_failure_helper (exc , BadRequest )
304-
305- @unittest .skipUnless (_HAVE_GRPC , 'No gRPC' )
306- def test_commit_failure_cancelled (self ):
307- from grpc import StatusCode
308- from grpc ._channel import _RPCState
309- from google .cloud .exceptions import GrpcRendezvous
310-
311- exc_state = _RPCState ((), None , None , StatusCode .CANCELLED , None )
312- exc = GrpcRendezvous (exc_state , None , None , None )
313- self ._commit_failure_helper (exc , GrpcRendezvous )
314-
315- @unittest .skipUnless (_HAVE_GRPC , 'No gRPC' )
316- def test_commit_failure_non_grpc_err (self ):
317- exc = RuntimeError ('Not a gRPC error' )
318- self ._commit_failure_helper (exc , RuntimeError )
319-
320318 def test_rollback (self ):
321319 return_val = object ()
322320 stub = _GRPCStub (return_val )
@@ -1161,27 +1159,22 @@ def __init__(self, return_val=None, side_effect=Exception):
11611159
11621160 def _method (self , request_pb , name ):
11631161 self .method_calls .append ((request_pb , name ))
1164- return self .return_val
1162+ if self .side_effect is Exception :
1163+ return self .return_val
1164+ else :
1165+ raise self .side_effect
11651166
11661167 def Lookup (self , request_pb ):
11671168 return self ._method (request_pb , 'Lookup' )
11681169
11691170 def RunQuery (self , request_pb ):
1170- result = self ._method (request_pb , 'RunQuery' )
1171- if self .side_effect is Exception :
1172- return result
1173- else :
1174- raise self .side_effect
1171+ return self ._method (request_pb , 'RunQuery' )
11751172
11761173 def BeginTransaction (self , request_pb ):
11771174 return self ._method (request_pb , 'BeginTransaction' )
11781175
11791176 def Commit (self , request_pb ):
1180- result = self ._method (request_pb , 'Commit' )
1181- if self .side_effect is Exception :
1182- return result
1183- else :
1184- raise self .side_effect
1177+ return self ._method (request_pb , 'Commit' )
11851178
11861179 def Rollback (self , request_pb ):
11871180 return self ._method (request_pb , 'Rollback' )
0 commit comments