@@ -106,6 +106,72 @@ 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 ):
113+ from google .cloud .datastore .connection import _grpc_catch_rendezvous
114+ return _grpc_catch_rendezvous ()
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+ with self ._callFUT ():
126+ result = self ._fake_method (None , expected )
127+ self .assertIs (result , expected )
128+
129+ def test_failure_aborted (self ):
130+ from grpc import StatusCode
131+ from grpc ._channel import _RPCState
132+ from google .cloud .exceptions import Conflict
133+ from google .cloud .exceptions import GrpcRendezvous
134+
135+ details = 'Bad things.'
136+ exc_state = _RPCState ((), None , None , StatusCode .ABORTED , details )
137+ exc = GrpcRendezvous (exc_state , None , None , None )
138+ with self .assertRaises (Conflict ):
139+ with self ._callFUT ():
140+ self ._fake_method (exc )
141+
142+ def test_failure_invalid_argument (self ):
143+ from grpc import StatusCode
144+ from grpc ._channel import _RPCState
145+ from google .cloud .exceptions import BadRequest
146+ from google .cloud .exceptions import GrpcRendezvous
147+
148+ details = ('Cannot have inequality filters on multiple '
149+ 'properties: [created, priority]' )
150+ exc_state = _RPCState ((), None , None ,
151+ StatusCode .INVALID_ARGUMENT , details )
152+ exc = GrpcRendezvous (exc_state , None , None , None )
153+ with self .assertRaises (BadRequest ):
154+ with self ._callFUT ():
155+ self ._fake_method (exc )
156+
157+ def test_failure_cancelled (self ):
158+ from grpc import StatusCode
159+ from grpc ._channel import _RPCState
160+ from google .cloud .exceptions import GrpcRendezvous
161+
162+ exc_state = _RPCState ((), None , None , StatusCode .CANCELLED , None )
163+ exc = GrpcRendezvous (exc_state , None , None , None )
164+ with self .assertRaises (GrpcRendezvous ):
165+ with self ._callFUT ():
166+ self ._fake_method (exc )
167+
168+ def test_commit_failure_non_grpc_err (self ):
169+ exc = RuntimeError ('Not a gRPC error' )
170+ with self .assertRaises (RuntimeError ):
171+ with self ._callFUT ():
172+ self ._fake_method (exc )
173+
174+
109175class Test_DatastoreAPIOverGRPC (unittest .TestCase ):
110176
111177 def _getTargetClass (self ):
@@ -227,16 +293,6 @@ def test_run_query_invalid_argument(self):
227293 exc = GrpcRendezvous (exc_state , None , None , None )
228294 self ._run_query_failure_helper (exc , BadRequest )
229295
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-
240296 def test_begin_transaction (self ):
241297 return_val = object ()
242298 stub = _GRPCStub (return_val )
@@ -264,59 +320,6 @@ def test_commit_success(self):
264320 self .assertEqual (stub .method_calls ,
265321 [(request_pb , 'Commit' )])
266322
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-
320323 def test_rollback (self ):
321324 return_val = object ()
322325 stub = _GRPCStub (return_val )
@@ -1161,27 +1164,22 @@ def __init__(self, return_val=None, side_effect=Exception):
11611164
11621165 def _method (self , request_pb , name ):
11631166 self .method_calls .append ((request_pb , name ))
1164- return self .return_val
1167+ if self .side_effect is Exception :
1168+ return self .return_val
1169+ else :
1170+ raise self .side_effect
11651171
11661172 def Lookup (self , request_pb ):
11671173 return self ._method (request_pb , 'Lookup' )
11681174
11691175 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
1176+ return self ._method (request_pb , 'RunQuery' )
11751177
11761178 def BeginTransaction (self , request_pb ):
11771179 return self ._method (request_pb , 'BeginTransaction' )
11781180
11791181 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
1182+ return self ._method (request_pb , 'Commit' )
11851183
11861184 def Rollback (self , request_pb ):
11871185 return self ._method (request_pb , 'Rollback' )
0 commit comments