Skip to content

Commit 8190f4c

Browse files
committed
Give '_Batches' methods more natural names.
Add explicit test of using the stack.
1 parent e5e4518 commit 8190f4c

2 files changed

Lines changed: 60 additions & 21 deletions

File tree

gcloud/datastore/batch.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def __init__(self):
3232
super(_Batches, self).__init__()
3333
self._stack = []
3434

35-
@property
36-
def stack(self):
37-
"""Return a copy of our stack."""
38-
return self._stack[:]
35+
def __iter__(self):
36+
"""Iterate the stack in LIFO order.
37+
"""
38+
return iter(reversed(self._stack))
3939

40-
def _push_batch(self, batch):
41-
"""Push a batch onto our stack.
40+
def push(self, batch):
41+
"""Push a batch / transaction onto our stack.
4242
4343
Intended for use only in :meth:`gcloud.datastore.batch.Batch.__enter__`
4444
@@ -47,8 +47,8 @@ def _push_batch(self, batch):
4747
"""
4848
self._stack.append(batch)
4949

50-
def _pop_batch(self):
51-
"""Pop a batch onto our stack.
50+
def pop(self):
51+
"""Pop a batch / transaction from our stack.
5252
5353
Intended for use only in :meth:`gcloud.datastore.batch.Batch.__enter__`
5454
@@ -58,6 +58,17 @@ def _pop_batch(self):
5858
"""
5959
return self._stack.pop()
6060

61+
@property
62+
def top(self):
63+
"""Get the top-most batch / transaction
64+
65+
:rtype: :class:`gcloud.datastore.batch.Batch` or
66+
:class:`gcloud.datastore.transaction.Transaction` or None
67+
:returns: the top-most item, or None if the stack is empty.
68+
"""
69+
if len(self._stack) > 0:
70+
return self._stack[-1]
71+
6172

6273
_BATCHES = _Batches()
6374

@@ -256,7 +267,7 @@ def rollback(self):
256267
pass
257268

258269
def __enter__(self):
259-
_BATCHES._push_batch(self)
270+
_BATCHES.push(self)
260271
self.begin()
261272
return self
262273

@@ -267,4 +278,4 @@ def __exit__(self, exc_type, exc_val, exc_tb):
267278
else:
268279
self.rollback()
269280
finally:
270-
_BATCHES._pop_batch()
281+
_BATCHES.pop()

gcloud/datastore/test_batch.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@
1515
import unittest2
1616

1717

18+
class Test_Batches(unittest2.TestCase):
19+
20+
def _getTargetClass(self):
21+
from gcloud.datastore.batch import _Batches
22+
23+
return _Batches
24+
25+
def _makeOne(self):
26+
return self._getTargetClass()()
27+
28+
def test_it(self):
29+
batch1, batch2 = object(), object()
30+
batches = self._makeOne()
31+
self.assertEqual(list(batches), [])
32+
self.assertTrue(batches.top is None)
33+
batches.push(batch1)
34+
self.assertTrue(batches.top is batch1)
35+
batches.push(batch2)
36+
self.assertTrue(batches.top is batch2)
37+
popped = batches.pop()
38+
self.assertTrue(popped is batch2)
39+
self.assertTrue(batches.top is batch1)
40+
self.assertEqual(list(batches), [batch1])
41+
popped = batches.pop()
42+
self.assertTrue(batches.top is None)
43+
self.assertEqual(list(batches), [])
44+
45+
1846
class TestBatch(unittest2.TestCase):
1947

2048
def _getTargetClass(self):
@@ -177,14 +205,14 @@ def test_as_context_mgr_wo_error(self):
177205
entity = _Entity(_PROPERTIES)
178206
key = entity.key = _Key(_DATASET)
179207

180-
self.assertEqual(_BATCHES.stack, [])
208+
self.assertEqual(list(_BATCHES), [])
181209

182210
with self._makeOne(dataset_id=_DATASET,
183211
connection=connection) as batch:
184-
self.assertEqual(_BATCHES.stack, [batch])
212+
self.assertEqual(list(_BATCHES), [batch])
185213
batch.put(entity)
186214

187-
self.assertEqual(_BATCHES.stack, [])
215+
self.assertEqual(list(_BATCHES), [])
188216

189217
self.assertEqual(
190218
connection._saved,
@@ -201,20 +229,20 @@ def test_as_context_mgr_nested(self):
201229
entity2 = _Entity(_PROPERTIES)
202230
key = entity2.key = _Key(_DATASET)
203231

204-
self.assertEqual(_BATCHES.stack, [])
232+
self.assertEqual(list(_BATCHES), [])
205233

206234
with self._makeOne(dataset_id=_DATASET,
207235
connection=connection) as batch1:
208-
self.assertEqual(_BATCHES.stack, [batch1])
236+
self.assertEqual(list(_BATCHES), [batch1])
209237
batch1.put(entity1)
210238
with self._makeOne(dataset_id=_DATASET,
211239
connection=connection) as batch2:
212-
self.assertEqual(_BATCHES.stack, [batch1, batch2])
240+
self.assertEqual(list(_BATCHES), [batch2, batch1])
213241
batch2.put(entity2)
214242

215-
self.assertEqual(_BATCHES.stack, [batch1])
243+
self.assertEqual(list(_BATCHES), [batch1])
216244

217-
self.assertEqual(_BATCHES.stack, [])
245+
self.assertEqual(list(_BATCHES), [])
218246

219247
self.assertEqual(
220248
connection._saved,
@@ -233,18 +261,18 @@ def test_as_context_mgr_w_error(self):
233261
entity = _Entity(_PROPERTIES)
234262
key = entity.key = _Key(_DATASET)
235263

236-
self.assertEqual(_BATCHES.stack, [])
264+
self.assertEqual(list(_BATCHES), [])
237265

238266
try:
239267
with self._makeOne(dataset_id=_DATASET,
240268
connection=connection) as batch:
241-
self.assertEqual(_BATCHES.stack, [batch])
269+
self.assertEqual(list(_BATCHES), [batch])
242270
batch.put(entity)
243271
raise ValueError("testing")
244272
except ValueError:
245273
pass
246274

247-
self.assertEqual(_BATCHES.stack, [])
275+
self.assertEqual(list(_BATCHES), [])
248276

249277
self.assertEqual(
250278
connection._saved,

0 commit comments

Comments
 (0)