@@ -101,23 +101,15 @@ def test_get_no_keys(self):
101101 results = self ._callFUT ([])
102102 self .assertEqual (results , [])
103103
104- def _miss_helper (self , expected_results , use_list = True ):
104+ def test_get_miss (self ):
105105 from gcloud .datastore .key import Key
106106 from gcloud .datastore .test_connection import _Connection
107107
108108 DATASET_ID = 'DATASET'
109109 connection = _Connection ()
110110 key = Key ('Kind' , 1234 , dataset_id = DATASET_ID )
111- if use_list :
112- key = [key ]
113- results = self ._callFUT (key , connection = connection )
114- self .assertEqual (results , expected_results )
115-
116- def test_get_miss (self ):
117- self ._miss_helper ([], use_list = True )
118-
119- def test_get_miss_single_key (self ):
120- self ._miss_helper (None , use_list = False )
111+ results = self ._callFUT ([key ], connection = connection )
112+ self .assertEqual (results , [])
121113
122114 def test_get_miss_w_missing (self ):
123115 from gcloud .datastore import datastore_v1_pb2 as datastore_pb
@@ -248,33 +240,6 @@ def test_get_hit_multiple_keys_different_dataset(self):
248240 with self .assertRaises (ValueError ):
249241 self ._callFUT ([key1 , key2 ], connection = object ())
250242
251- def test_get_hit_single_key (self ):
252- from gcloud .datastore .key import Key
253- from gcloud .datastore .test_connection import _Connection
254-
255- DATASET_ID = 'DATASET'
256- KIND = 'Kind'
257- ID = 1234
258- PATH = [{'kind' : KIND , 'id' : ID }]
259-
260- # Make a found entity pb to be returned from mock backend.
261- entity_pb = self ._make_entity_pb (DATASET_ID , KIND , ID ,
262- 'foo' , 'Foo' )
263-
264- # Make a connection to return the entity pb.
265- connection = _Connection (entity_pb )
266-
267- key = Key (KIND , ID , dataset_id = DATASET_ID )
268- result = self ._callFUT (key , connection = connection )
269- new_key = result .key
270-
271- # Check the returned value is as expected.
272- self .assertFalse (new_key is key )
273- self .assertEqual (new_key .dataset_id , DATASET_ID )
274- self .assertEqual (new_key .path , PATH )
275- self .assertEqual (list (result ), ['foo' ])
276- self .assertEqual (result ['foo' ], 'Foo' )
277-
278243 def test_get_implicit (self ):
279244 from gcloud .datastore import _implicit_environ
280245 from gcloud .datastore .key import Key
@@ -313,6 +278,102 @@ def test_get_implicit(self):
313278 self .assertEqual (result ['foo' ], 'Foo' )
314279
315280
281+ class Test_delete_function (unittest2 .TestCase ):
282+
283+ def _callFUT (self , keys , connection = None ):
284+ from gcloud .datastore .api import delete
285+ return delete (keys , connection = connection )
286+
287+ def test_delete_no_batch (self ):
288+ from gcloud .datastore .test_batch import _Connection
289+ from gcloud .datastore .test_batch import _Key
290+
291+ # Build basic mocks needed to delete.
292+ _DATASET = 'DATASET'
293+ connection = _Connection ()
294+ key = _Key (_DATASET )
295+
296+ result = self ._callFUT ([key ], connection = connection )
297+ self .assertEqual (result , None )
298+
299+ def test_delete_existing_batch (self ):
300+ from gcloud ._testing import _Monkey
301+ from gcloud .datastore import api
302+ from gcloud .datastore .batch import _Batches
303+ from gcloud .datastore .batch import Batch
304+ from gcloud .datastore .test_batch import _Connection
305+ from gcloud .datastore .test_batch import _Key
306+
307+ # Build basic mocks needed to delete.
308+ _DATASET = 'DATASET'
309+ connection = _Connection ()
310+ key = _Key (_DATASET )
311+
312+ # Set up mock Batch on stack so we can check it is used.
313+ _BATCHES = _Batches ()
314+ CURR_BATCH = Batch (dataset_id = _DATASET , connection = connection )
315+ _BATCHES .push (CURR_BATCH )
316+
317+ with _Monkey (api , _BATCHES = _BATCHES ):
318+ result = self ._callFUT ([key ], connection = connection )
319+
320+ self .assertEqual (result , None )
321+ self .assertEqual (len (CURR_BATCH .mutation .insert_auto_id ), 0 )
322+ self .assertEqual (len (CURR_BATCH .mutation .upsert ), 0 )
323+ deletes = list (CURR_BATCH .mutation .delete )
324+ self .assertEqual (len (deletes ), 1 )
325+ self .assertEqual (deletes [0 ], key ._key )
326+
327+ def test_delete_implicit_connection (self ):
328+ from gcloud ._testing import _Monkey
329+ from gcloud .datastore import _implicit_environ
330+ from gcloud .datastore import api
331+ from gcloud .datastore .batch import _Batches
332+ from gcloud .datastore .batch import Batch
333+ from gcloud .datastore .test_batch import _Connection
334+ from gcloud .datastore .test_batch import _Key
335+
336+ # Build basic mocks needed to delete.
337+ _DATASET = 'DATASET'
338+ connection = _Connection ()
339+ key = _Key (_DATASET )
340+
341+ # Set up mock Batch on stack so we can check it is used.
342+ _BATCHES = _Batches ()
343+
344+ with _Monkey (_implicit_environ , CONNECTION = connection ):
345+ CURR_BATCH = Batch (dataset_id = _DATASET )
346+ _BATCHES .push (CURR_BATCH )
347+ with _Monkey (api , _BATCHES = _BATCHES ):
348+ result = self ._callFUT ([key ])
349+
350+ self .assertEqual (result , None )
351+ self .assertEqual (len (CURR_BATCH .mutation .insert_auto_id ), 0 )
352+ self .assertEqual (len (CURR_BATCH .mutation .upsert ), 0 )
353+ deletes = list (CURR_BATCH .mutation .delete )
354+ self .assertEqual (len (deletes ), 1 )
355+ self .assertEqual (deletes [0 ], key ._key )
356+
357+ def test_delete_no_keys (self ):
358+ from gcloud .datastore import _implicit_environ
359+
360+ self .assertEqual (_implicit_environ .CONNECTION , None )
361+ result = self ._callFUT ([])
362+ self .assertEqual (result , None )
363+
364+ def test_delete_no_connection (self ):
365+ from gcloud .datastore import _implicit_environ
366+ from gcloud .datastore .test_batch import _Key
367+
368+ # Build basic mocks needed to delete.
369+ _DATASET = 'DATASET'
370+ key = _Key (_DATASET )
371+
372+ self .assertEqual (_implicit_environ .CONNECTION , None )
373+ with self .assertRaises (ValueError ):
374+ self ._callFUT ([key ])
375+
376+
316377class Test_allocate_ids_function (unittest2 .TestCase ):
317378
318379 def _callFUT (self , incomplete_key , num_ids , connection = None ):
0 commit comments