@@ -151,20 +151,43 @@ def test_it(self):
151151
152152class Test_implicit_behavior (unittest2 .TestCase ):
153153
154- def test__require_dataset (self ):
154+ def test__require_dataset_value_unset (self ):
155155 import gcloud .datastore
156156 from gcloud .datastore import _implicit_environ
157- original_dataset = _implicit_environ .DATASET
158-
159- try :
160- _implicit_environ .DATASET = None
161- self .assertRaises (EnvironmentError ,
162- gcloud .datastore ._require_dataset )
163- NEW_DATASET = object ()
164- _implicit_environ .DATASET = NEW_DATASET
165- self .assertEqual (gcloud .datastore ._require_dataset (), NEW_DATASET )
166- finally :
167- _implicit_environ .DATASET = original_dataset
157+ from gcloud ._testing import _Monkey
158+
159+ with _Monkey (_implicit_environ , DATASET = None ):
160+ with self .assertRaises (EnvironmentError ):
161+ gcloud .datastore ._require_dataset ()
162+
163+ def test__require_dataset_value_set (self ):
164+ import gcloud .datastore
165+ from gcloud .datastore import _implicit_environ
166+ from gcloud ._testing import _Monkey
167+
168+ FAKE_DATASET = object ()
169+ with _Monkey (_implicit_environ , DATASET = FAKE_DATASET ):
170+ stored_dataset = gcloud .datastore ._require_dataset ()
171+ self .assertTrue (stored_dataset is FAKE_DATASET )
172+
173+ def test__require_connection_value_unset (self ):
174+ import gcloud .datastore
175+ from gcloud .datastore import _implicit_environ
176+ from gcloud ._testing import _Monkey
177+
178+ with _Monkey (_implicit_environ , CONNECTION = None ):
179+ with self .assertRaises (EnvironmentError ):
180+ gcloud .datastore ._require_connection ()
181+
182+ def test__require_connection_value_set (self ):
183+ import gcloud .datastore
184+ from gcloud .datastore import _implicit_environ
185+ from gcloud ._testing import _Monkey
186+
187+ FAKE_CONNECTION = object ()
188+ with _Monkey (_implicit_environ , CONNECTION = FAKE_CONNECTION ):
189+ stored_connection = gcloud .datastore ._require_connection ()
190+ self .assertTrue (stored_connection is FAKE_CONNECTION )
168191
169192 def test_get_entities (self ):
170193 import gcloud .datastore
@@ -182,18 +205,62 @@ def test_get_entities(self):
182205 result = gcloud .datastore .get_entities (DUMMY_KEYS )
183206 self .assertTrue (result == DUMMY_VALS )
184207
208+
209+ class Test_allocate_ids_function (unittest2 .TestCase ):
210+
211+ def _callFUT (self , incomplete_key , num_ids ,
212+ connection = None , dataset_id = None ):
213+ from gcloud .datastore import allocate_ids
214+ return allocate_ids (incomplete_key , num_ids , connection = connection ,
215+ dataset_id = dataset_id )
216+
185217 def test_allocate_ids (self ):
186- import gcloud .datastore
218+ from gcloud .datastore .key import Key
219+ from gcloud .datastore .test_dataset import _Connection
220+
221+ DATASET_ID = 'DATASET'
222+ INCOMPLETE_KEY = Key ('KIND' , dataset_id = DATASET_ID )
223+ CONNECTION = _Connection ()
224+ NUM_IDS = 2
225+ result = self ._callFUT (INCOMPLETE_KEY , NUM_IDS ,
226+ connection = CONNECTION , dataset_id = DATASET_ID )
227+
228+ # Check the IDs returned match.
229+ self .assertEqual ([key .id for key in result ], range (NUM_IDS ))
230+
231+ # Check connection is called correctly.
232+ self .assertEqual (CONNECTION ._called_dataset_id , DATASET_ID )
233+ self .assertEqual (len (CONNECTION ._called_key_pbs ), NUM_IDS )
234+
235+ def test_allocate_ids_implicit (self ):
187236 from gcloud .datastore import _implicit_environ
188237 from gcloud .datastore .key import Key
238+ from gcloud .datastore .test_dataset import _Connection
189239 from gcloud .datastore .test_entity import _Dataset
190240 from gcloud ._testing import _Monkey
191241
192242 CUSTOM_DATASET = _Dataset ()
243+ CUSTOM_CONNECTION = _Connection ()
193244 NUM_IDS = 2
194- with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ):
245+ with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ,
246+ CONNECTION = CUSTOM_CONNECTION ):
195247 INCOMPLETE_KEY = Key ('KIND' )
196- result = gcloud . datastore . allocate_ids (INCOMPLETE_KEY , NUM_IDS )
248+ result = self . _callFUT (INCOMPLETE_KEY , NUM_IDS )
197249
198250 # Check the IDs returned.
199- self .assertEqual ([key .id for key in result ], range (1 , NUM_IDS + 1 ))
251+ self .assertEqual ([key .id for key in result ], range (NUM_IDS ))
252+
253+ def test_allocate_ids_with_complete (self ):
254+ from gcloud .datastore import _implicit_environ
255+ from gcloud .datastore .key import Key
256+ from gcloud .datastore .test_dataset import _Connection
257+ from gcloud .datastore .test_entity import _Dataset
258+ from gcloud ._testing import _Monkey
259+
260+ CUSTOM_DATASET = _Dataset ()
261+ CUSTOM_CONNECTION = _Connection ()
262+ with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ,
263+ CONNECTION = CUSTOM_CONNECTION ):
264+ COMPLETE_KEY = Key ('KIND' , 1234 )
265+ self .assertRaises (ValueError , self ._callFUT ,
266+ COMPLETE_KEY , 2 )
0 commit comments