@@ -195,6 +195,63 @@ def test_to_protobuf_w_no_kind(self):
195195 pb = key .to_protobuf ()
196196 self .assertFalse (pb .path_element [0 ].HasField ('kind' ))
197197
198+ def test_get_explicit_connection_miss (self ):
199+ from gcloud .datastore .test_dataset import _Connection
200+
201+ cnxn_lookup_result = []
202+ cnxn = _Connection (* cnxn_lookup_result )
203+ key = self ._makeOne ('KIND' , 1234 )
204+ entity = key .get (connection = cnxn )
205+ self .assertEqual (entity , None )
206+
207+ def test_get_implicit_connection_miss (self ):
208+ from gcloud ._testing import _Monkey
209+ from gcloud .datastore import _implicit_environ
210+ from gcloud .datastore .test_dataset import _Connection
211+
212+ cnxn_lookup_result = []
213+ cnxn = _Connection (* cnxn_lookup_result )
214+ key = self ._makeOne ('KIND' , 1234 )
215+ with _Monkey (_implicit_environ , CONNECTION = cnxn ):
216+ entity = key .get ()
217+ self .assertEqual (entity , None )
218+
219+ def test_get_explicit_connection_hit (self ):
220+ from gcloud .datastore import datastore_v1_pb2
221+ from gcloud .datastore .test_dataset import _Connection
222+
223+ KIND = 'KIND'
224+ ID = 1234
225+
226+ # Make a bogus entity PB to be returned from fake Connection.
227+ entity_pb = datastore_v1_pb2 .Entity ()
228+ entity_pb .key .partition_id .dataset_id = self ._DEFAULT_DATASET
229+ path_element = entity_pb .key .path_element .add ()
230+ path_element .kind = KIND
231+ path_element .id = ID
232+ prop = entity_pb .property .add ()
233+ prop .name = 'foo'
234+ prop .value .string_value = 'Foo'
235+
236+ # Make fake connection.
237+ cnxn_lookup_result = [entity_pb ]
238+ cnxn = _Connection (* cnxn_lookup_result )
239+
240+ # Create key and look-up.
241+ key = self ._makeOne (KIND , ID )
242+ entity = key .get (connection = cnxn )
243+ self .assertEqual (entity .items (), [('foo' , 'Foo' )])
244+ self .assertTrue (entity .key () is key )
245+
246+ def test_get_explicit_connection_partial_key (self ):
247+ from gcloud .datastore .test_dataset import _Connection
248+
249+ cnxn_lookup_result = []
250+ cnxn = _Connection (* cnxn_lookup_result )
251+ key = self ._makeOne ('KIND' )
252+ with self .assertRaises (ValueError ):
253+ key .get (connection = cnxn )
254+
198255 def test_is_partial_no_name_or_id (self ):
199256 key = self ._makeOne ('KIND' )
200257 self .assertTrue (key .is_partial )
0 commit comments