@@ -832,9 +832,7 @@ def test_make_public_w_future(self):
832832
833833 def test_make_public_recursive (self ):
834834 from gcloud .storage .acl import _ACLEntity
835- from gcloud ._testing import _Monkey
836- from gcloud .storage import key
837- from gcloud .storage import bucket as MUT
835+ from gcloud .storage .bucket import _KeyIterator
838836 _saved = []
839837
840838 class _Key (object ):
@@ -856,7 +854,7 @@ def grant_read(self):
856854 def save_acl (self ):
857855 _saved .append ((self ._bucket , self ._name , self ._granted ))
858856
859- class _KeyIterator ( key . _KeyIterator ):
857+ class _Iterator ( _KeyIterator ):
860858 def get_items_from_response (self , response ):
861859 for item in response .get ('items' , []):
862860 yield _Key (self .bucket , item ['name' ])
@@ -868,8 +866,8 @@ def get_items_from_response(self, response):
868866 connection = _Connection (after , {'items' : [{'name' : KEY }]})
869867 bucket = self ._makeOne (connection , NAME )
870868 bucket .acl .loaded = True
871- with _Monkey ( MUT , _KeyIterator = _KeyIterator ):
872- bucket .make_public (recursive = True )
869+ bucket . _iterator_class = _Iterator
870+ bucket .make_public (recursive = True )
873871 self .assertEqual (list (bucket .acl ), permissive )
874872 self .assertEqual (list (bucket .default_object_acl ), [])
875873 self .assertEqual (_saved , [(bucket , KEY , True )])
@@ -884,6 +882,46 @@ def get_items_from_response(self, response):
884882 self .assertEqual (kw [1 ]['query_params' ], {})
885883
886884
885+ class Test__KeyIterator (unittest2 .TestCase ):
886+
887+ def _getTargetClass (self ):
888+ from gcloud .storage .bucket import _KeyIterator
889+ return _KeyIterator
890+
891+ def _makeOne (self , * args , ** kw ):
892+ return self ._getTargetClass ()(* args , ** kw )
893+
894+ def test_ctor (self ):
895+ connection = _Connection ()
896+ bucket = _Bucket (connection )
897+ iterator = self ._makeOne (bucket )
898+ self .assertTrue (iterator .bucket is bucket )
899+ self .assertTrue (iterator .connection is connection )
900+ self .assertEqual (iterator .path , '%s/o' % bucket .path )
901+ self .assertEqual (iterator .page_number , 0 )
902+ self .assertEqual (iterator .next_page_token , None )
903+
904+ def test_get_items_from_response_empty (self ):
905+ connection = _Connection ()
906+ bucket = _Bucket (connection )
907+ iterator = self ._makeOne (bucket )
908+ self .assertEqual (list (iterator .get_items_from_response ({})), [])
909+
910+ def test_get_items_from_response_non_empty (self ):
911+ from gcloud .storage .key import Key
912+ KEY = 'key'
913+ response = {'items' : [{'name' : KEY }]}
914+ connection = _Connection ()
915+ bucket = _Bucket (connection )
916+ iterator = self ._makeOne (bucket )
917+ keys = list (iterator .get_items_from_response (response ))
918+ self .assertEqual (len (keys ), 1 )
919+ key = keys [0 ]
920+ self .assertTrue (isinstance (key , Key ))
921+ self .assertTrue (key .connection is connection )
922+ self .assertEqual (key .name , KEY )
923+
924+
887925class _Connection (object ):
888926 _delete_ok = False
889927
@@ -911,6 +949,14 @@ def delete_bucket(self, bucket, force=False):
911949 return True
912950
913951
952+ class _Bucket (object ):
953+ path = '/b/name'
954+ name = 'name'
955+
956+ def __init__ (self , connection ):
957+ self .connection = connection
958+
959+
914960class MockFile (io .StringIO ):
915961 name = None
916962
0 commit comments