@@ -71,7 +71,7 @@ def _makeOne(self, *args, **kw):
7171
7272 def test_ctor_defaults (self ):
7373 bucket = self ._makeOne ()
74- self .assertEqual (bucket .connection , None )
74+ self .assertEqual (bucket ._connection , None )
7575 self .assertEqual (bucket .name , None )
7676 self .assertEqual (bucket ._properties , {})
7777 self .assertTrue (bucket ._acl is None )
@@ -1068,6 +1068,44 @@ def get_items_from_response(self, response):
10681068 self .assertEqual (kw [1 ]['query_params' ], {})
10691069
10701070
1071+ class Test__require_connection (unittest2 .TestCase ):
1072+
1073+ def _callFUT (self , connection = None ):
1074+ from gcloud .storage .bucket import _require_connection
1075+ return _require_connection (connection = connection )
1076+
1077+ def _monkey (self , connection ):
1078+ from gcloud .storage ._testing import _monkey_defaults
1079+ return _monkey_defaults (connection = connection )
1080+
1081+ def test_implicit_unset (self ):
1082+ with self ._monkey (None ):
1083+ with self .assertRaises (EnvironmentError ):
1084+ self ._callFUT ()
1085+
1086+ def test_implicit_unset_w_existing_batch (self ):
1087+ CONNECTION = object ()
1088+ with self ._monkey (None ):
1089+ with _NoCommitBatch (connection = CONNECTION ):
1090+ self .assertEqual (self ._callFUT (), CONNECTION )
1091+
1092+ def test_implicit_unset_passed_explicitly (self ):
1093+ CONNECTION = object ()
1094+ with self ._monkey (None ):
1095+ self .assertTrue (self ._callFUT (CONNECTION ) is CONNECTION )
1096+
1097+ def test_implicit_set (self ):
1098+ IMPLICIT_CONNECTION = object ()
1099+ with self ._monkey (IMPLICIT_CONNECTION ):
1100+ self .assertTrue (self ._callFUT () is IMPLICIT_CONNECTION )
1101+
1102+ def test_implicit_set_passed_explicitly (self ):
1103+ IMPLICIT_CONNECTION = object ()
1104+ CONNECTION = object ()
1105+ with self ._monkey (IMPLICIT_CONNECTION ):
1106+ self .assertTrue (self ._callFUT (CONNECTION ) is CONNECTION )
1107+
1108+
10711109class _Connection (object ):
10721110 _delete_bucket = False
10731111
@@ -1118,3 +1156,18 @@ class MockFile(io.StringIO):
11181156 def __init__ (self , name , buffer_ = None ):
11191157 super (MockFile , self ).__init__ (buffer_ )
11201158 self .name = name
1159+
1160+
1161+ class _NoCommitBatch (object ):
1162+
1163+ def __init__ (self , connection ):
1164+ self ._connection = connection
1165+
1166+ def __enter__ (self ):
1167+ from gcloud .storage .batch import _BATCHES
1168+ _BATCHES .push (self ._connection )
1169+ return self ._connection
1170+
1171+ def __exit__ (self , * args ):
1172+ from gcloud .storage .batch import _BATCHES
1173+ _BATCHES .pop ()
0 commit comments