@@ -68,8 +68,20 @@ def test_put_entity_wo_key(self):
6868 client = _Client (_PROJECT , connection )
6969 batch = self ._makeOne (client )
7070
71+ batch .begin ()
7172 self .assertRaises (ValueError , batch .put , _Entity ())
7273
74+ def test_put_entity_wrong_status (self ):
75+ _PROJECT = 'PROJECT'
76+ connection = _Connection ()
77+ client = _Client (_PROJECT , connection )
78+ batch = self ._makeOne (client )
79+ entity = _Entity ()
80+ entity .key = _Key ('OTHER' )
81+
82+ self .assertEqual (batch ._status , batch ._INITIAL )
83+ self .assertRaises (ValueError , batch .put , entity )
84+
7385 def test_put_entity_w_key_wrong_project (self ):
7486 _PROJECT = 'PROJECT'
7587 connection = _Connection ()
@@ -78,6 +90,7 @@ def test_put_entity_w_key_wrong_project(self):
7890 entity = _Entity ()
7991 entity .key = _Key ('OTHER' )
8092
93+ batch .begin ()
8194 self .assertRaises (ValueError , batch .put , entity )
8295
8396 def test_put_entity_w_partial_key (self ):
@@ -90,6 +103,7 @@ def test_put_entity_w_partial_key(self):
90103 key = entity .key = _Key (_PROJECT )
91104 key ._id = None
92105
106+ batch .begin ()
93107 batch .put (entity )
94108
95109 mutated_entity = _mutated_pb (self , batch .mutations , 'insert' )
@@ -113,6 +127,7 @@ def test_put_entity_w_completed_key(self):
113127 entity .exclude_from_indexes = ('baz' , 'spam' )
114128 key = entity .key = _Key (_PROJECT )
115129
130+ batch .begin ()
116131 batch .put (entity )
117132
118133 mutated_entity = _mutated_pb (self , batch .mutations , 'upsert' )
@@ -129,6 +144,17 @@ def test_put_entity_w_completed_key(self):
129144 self .assertTrue (spam_values [2 ].exclude_from_indexes )
130145 self .assertFalse ('frotz' in prop_dict )
131146
147+ def test_delete_wrong_status (self ):
148+ _PROJECT = 'PROJECT'
149+ connection = _Connection ()
150+ client = _Client (_PROJECT , connection )
151+ batch = self ._makeOne (client )
152+ key = _Key (_PROJECT )
153+ key ._id = None
154+
155+ self .assertEqual (batch ._status , batch ._INITIAL )
156+ self .assertRaises (ValueError , batch .delete , key )
157+
132158 def test_delete_w_partial_key (self ):
133159 _PROJECT = 'PROJECT'
134160 connection = _Connection ()
@@ -137,6 +163,7 @@ def test_delete_w_partial_key(self):
137163 key = _Key (_PROJECT )
138164 key ._id = None
139165
166+ batch .begin ()
140167 self .assertRaises (ValueError , batch .delete , key )
141168
142169 def test_delete_w_key_wrong_project (self ):
@@ -146,6 +173,7 @@ def test_delete_w_key_wrong_project(self):
146173 batch = self ._makeOne (client )
147174 key = _Key ('OTHER' )
148175
176+ batch .begin ()
149177 self .assertRaises (ValueError , batch .delete , key )
150178
151179 def test_delete_w_completed_key (self ):
@@ -155,6 +183,7 @@ def test_delete_w_completed_key(self):
155183 batch = self ._makeOne (client )
156184 key = _Key (_PROJECT )
157185
186+ batch .begin ()
158187 batch .delete (key )
159188
160189 mutated_key = _mutated_pb (self , batch .mutations , 'delete' )
@@ -180,23 +209,43 @@ def test_rollback(self):
180209 _PROJECT = 'PROJECT'
181210 client = _Client (_PROJECT , None )
182211 batch = self ._makeOne (client )
183- self .assertEqual (batch ._status , batch ._INITIAL )
212+ batch .begin ()
213+ self .assertEqual (batch ._status , batch ._IN_PROGRESS )
184214 batch .rollback ()
185215 self .assertEqual (batch ._status , batch ._ABORTED )
186216
217+ def test_rollback_wrong_status (self ):
218+ _PROJECT = 'PROJECT'
219+ client = _Client (_PROJECT , None )
220+ batch = self ._makeOne (client )
221+
222+ self .assertEqual (batch ._status , batch ._INITIAL )
223+ self .assertRaises (ValueError , batch .rollback )
224+
187225 def test_commit (self ):
188226 _PROJECT = 'PROJECT'
189227 connection = _Connection ()
190228 client = _Client (_PROJECT , connection )
191229 batch = self ._makeOne (client )
192230
193231 self .assertEqual (batch ._status , batch ._INITIAL )
232+ batch .begin ()
233+ self .assertEqual (batch ._status , batch ._IN_PROGRESS )
194234 batch .commit ()
195235 self .assertEqual (batch ._status , batch ._FINISHED )
196236
197237 self .assertEqual (connection ._committed ,
198238 [(_PROJECT , batch ._commit_request , None )])
199239
240+ def test_commit_wrong_status (self ):
241+ _PROJECT = 'PROJECT'
242+ connection = _Connection ()
243+ client = _Client (_PROJECT , connection )
244+ batch = self ._makeOne (client )
245+
246+ self .assertEqual (batch ._status , batch ._INITIAL )
247+ self .assertRaises (ValueError , batch .commit )
248+
200249 def test_commit_w_partial_key_entities (self ):
201250 _PROJECT = 'PROJECT'
202251 _NEW_ID = 1234
@@ -209,6 +258,8 @@ def test_commit_w_partial_key_entities(self):
209258 batch ._partial_key_entities .append (entity )
210259
211260 self .assertEqual (batch ._status , batch ._INITIAL )
261+ batch .begin ()
262+ self .assertEqual (batch ._status , batch ._IN_PROGRESS )
212263 batch .commit ()
213264 self .assertEqual (batch ._status , batch ._FINISHED )
214265
@@ -295,6 +346,26 @@ def test_as_context_mgr_w_error(self):
295346 self .assertEqual (mutated_entity .key , key ._key )
296347 self .assertEqual (connection ._committed , [])
297348
349+ def test_as_context_mgr_enter_fails (self ):
350+ klass = self ._getTargetClass ()
351+
352+ class FailedBegin (klass ):
353+
354+ def begin (self ):
355+ raise RuntimeError
356+
357+ client = _Client (None , None )
358+ self .assertEqual (client ._batches , [])
359+
360+ batch = FailedBegin (client )
361+ with self .assertRaises (RuntimeError ):
362+ # The context manager will never be entered because
363+ # of the failure.
364+ with batch : # pragma: NO COVER
365+ pass
366+ # Make sure no batch was added.
367+ self .assertEqual (client ._batches , [])
368+
298369
299370class _PathElementPB (object ):
300371
0 commit comments