@@ -106,7 +106,7 @@ def test_put_entity_w_partial_key(self):
106106
107107 self .assertEqual (
108108 connection ._saved ,
109- (_DATASET , key ._key , _PROPERTIES , (), batch .mutation ))
109+ [ (_DATASET , key ._key , _PROPERTIES , (), batch .mutation )] )
110110 self .assertEqual (batch ._auto_id_entities , [entity ])
111111
112112 def test_put_entity_w_completed_key (self ):
@@ -121,7 +121,7 @@ def test_put_entity_w_completed_key(self):
121121
122122 self .assertEqual (
123123 connection ._saved ,
124- (_DATASET , key ._key , _PROPERTIES , (), batch .mutation ))
124+ [ (_DATASET , key ._key , _PROPERTIES , (), batch .mutation )] )
125125
126126 def test_delete_w_partial_key (self ):
127127 _DATASET = 'DATASET'
@@ -142,7 +142,7 @@ def test_delete_w_completed_key(self):
142142
143143 self .assertEqual (
144144 connection ._deleted ,
145- (_DATASET , [key ._key ], batch .mutation ))
145+ [ (_DATASET , [key ._key ], batch .mutation )] )
146146
147147 def test_commit (self ):
148148 _DATASET = 'DATASET'
@@ -151,7 +151,7 @@ def test_commit(self):
151151
152152 batch .commit ()
153153
154- self .assertEqual (connection ._committed , (_DATASET , batch .mutation ))
154+ self .assertEqual (connection ._committed , [ (_DATASET , batch .mutation )] )
155155
156156 def test_commit_w_auto_id_entities (self ):
157157 _DATASET = 'DATASET'
@@ -165,45 +165,91 @@ def test_commit_w_auto_id_entities(self):
165165
166166 batch .commit ()
167167
168- self .assertEqual (connection ._committed , (_DATASET , batch .mutation ))
168+ self .assertEqual (connection ._committed , [ (_DATASET , batch .mutation )] )
169169 self .assertFalse (key ._partial )
170170 self .assertEqual (key ._id , _NEW_ID )
171171
172172 def test_as_context_mgr_wo_error (self ):
173+ from gcloud .datastore .batch import _BATCHES
173174 _DATASET = 'DATASET'
174175 _PROPERTIES = {'foo' : 'bar' }
175176 connection = _Connection ()
176177 entity = _Entity (_PROPERTIES )
177178 key = entity .key = _Key (_DATASET )
178179
180+ self .assertEqual (_BATCHES .stack , [])
181+
179182 with self ._makeOne (dataset_id = _DATASET ,
180183 connection = connection ) as batch :
184+ self .assertEqual (_BATCHES .stack , [batch ])
181185 batch .put (entity )
182186
187+ self .assertEqual (_BATCHES .stack , [])
188+
183189 self .assertEqual (
184190 connection ._saved ,
185- (_DATASET , key ._key , _PROPERTIES , (), batch .mutation ))
186- self .assertEqual (connection ._committed , (_DATASET , batch .mutation ))
191+ [(_DATASET , key ._key , _PROPERTIES , (), batch .mutation )])
192+ self .assertEqual (connection ._committed , [(_DATASET , batch .mutation )])
193+
194+ def test_as_context_mgr_nested (self ):
195+ from gcloud .datastore .batch import _BATCHES
196+ _DATASET = 'DATASET'
197+ _PROPERTIES = {'foo' : 'bar' }
198+ connection = _Connection ()
199+ entity1 = _Entity (_PROPERTIES )
200+ key = entity1 .key = _Key (_DATASET )
201+ entity2 = _Entity (_PROPERTIES )
202+ key = entity2 .key = _Key (_DATASET )
203+
204+ self .assertEqual (_BATCHES .stack , [])
205+
206+ with self ._makeOne (dataset_id = _DATASET ,
207+ connection = connection ) as batch1 :
208+ self .assertEqual (_BATCHES .stack , [batch1 ])
209+ batch1 .put (entity1 )
210+ with self ._makeOne (dataset_id = _DATASET ,
211+ connection = connection ) as batch2 :
212+ self .assertEqual (_BATCHES .stack , [batch1 , batch2 ])
213+ batch2 .put (entity2 )
214+
215+ self .assertEqual (_BATCHES .stack , [batch1 ])
216+
217+ self .assertEqual (_BATCHES .stack , [])
218+
219+ self .assertEqual (
220+ connection ._saved ,
221+ [(_DATASET , key ._key , _PROPERTIES , (), batch1 .mutation ),
222+ (_DATASET , key ._key , _PROPERTIES , (), batch2 .mutation )]
223+ )
224+ self .assertEqual (connection ._committed ,
225+ [(_DATASET , batch2 .mutation ),
226+ (_DATASET , batch1 .mutation )])
187227
188228 def test_as_context_mgr_w_error (self ):
229+ from gcloud .datastore .batch import _BATCHES
189230 _DATASET = 'DATASET'
190231 _PROPERTIES = {'foo' : 'bar' }
191232 connection = _Connection ()
192233 entity = _Entity (_PROPERTIES )
193234 key = entity .key = _Key (_DATASET )
194235
236+ self .assertEqual (_BATCHES .stack , [])
237+
195238 try :
196239 with self ._makeOne (dataset_id = _DATASET ,
197240 connection = connection ) as batch :
241+ self .assertEqual (_BATCHES .stack , [batch ])
198242 batch .put (entity )
199243 raise ValueError ("testing" )
200244 except ValueError :
201245 pass
202246
247+ self .assertEqual (_BATCHES .stack , [])
248+
203249 self .assertEqual (
204250 connection ._saved ,
205- (_DATASET , key ._key , _PROPERTIES , (), batch .mutation ))
206- self .assertEqual (connection ._committed , None )
251+ [ (_DATASET , key ._key , _PROPERTIES , (), batch .mutation )] )
252+ self .assertEqual (connection ._committed , [] )
207253
208254
209255class _CommitResult (object ):
@@ -226,23 +272,25 @@ def __init__(self, id):
226272
227273class _Connection (object ):
228274 _marker = object ()
229- _committed = _saved = _deleted = None
230275 _save_result = (False , None )
231276
232277 def __init__ (self , * new_keys ):
233278 self ._commit_result = _CommitResult (* new_keys )
279+ self ._committed = []
280+ self ._saved = []
281+ self ._deleted = []
234282
235283 def save_entity (self , dataset_id , key_pb , properties ,
236284 exclude_from_indexes = (), mutation = None ):
237- self ._saved = (dataset_id , key_pb , properties ,
238- tuple (exclude_from_indexes ), mutation )
285+ self ._saved . append ( (dataset_id , key_pb , properties ,
286+ tuple (exclude_from_indexes ), mutation ) )
239287 return self ._save_result
240288
241289 def delete_entities (self , dataset_id , key_pbs , mutation = None ):
242- self ._deleted = ( dataset_id , key_pbs , mutation )
290+ self ._deleted . append (( dataset_id , key_pbs , mutation ) )
243291
244292 def commit (self , dataset_id , mutation ):
245- self ._committed = ( dataset_id , mutation )
293+ self ._committed . append (( dataset_id , mutation ) )
246294 return self ._commit_result
247295
248296
0 commit comments