@@ -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
@@ -181,3 +204,22 @@ def test_get_entities(self):
181204 with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ):
182205 result = gcloud .datastore .get_entities (DUMMY_KEYS )
183206 self .assertTrue (result == DUMMY_VALS )
207+
208+ def test_allocate_ids (self ):
209+ import gcloud .datastore
210+ from gcloud .datastore import _implicit_environ
211+ from gcloud .datastore .key import Key
212+ from gcloud .datastore .test_dataset import _Connection
213+ from gcloud .datastore .test_entity import _Dataset
214+ from gcloud ._testing import _Monkey
215+
216+ CUSTOM_DATASET = _Dataset ()
217+ CUSTOM_CONNECTION = _Connection ()
218+ NUM_IDS = 2
219+ with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ,
220+ CONNECTION = CUSTOM_CONNECTION ):
221+ INCOMPLETE_KEY = Key ('KIND' )
222+ result = gcloud .datastore .allocate_ids (INCOMPLETE_KEY , NUM_IDS )
223+
224+ # Check the IDs returned.
225+ self .assertEqual ([key .id for key in result ], range (NUM_IDS ))
0 commit comments