Skip to content

Commit 82ee82c

Browse files
committed
Remove confusion between 'connection' and '_batch_stack'.
1 parent 1d5ad10 commit 82ee82c

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

gcloud/datastore/client.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID."""
1515

16+
from gcloud._helpers import _LocalStack
1617
from gcloud.datastore import helpers
1718
from gcloud.datastore.batch import Batch
1819
from gcloud.datastore.entity import Entity
@@ -126,43 +127,42 @@ def __init__(self, dataset_id=None, namespace=None, connection=None):
126127
self.dataset_id = dataset_id
127128
if connection is None:
128129
connection = get_connection()
129-
self._connection_stack = [connection]
130+
self.connection = connection
131+
self._batch_stack = _LocalStack()
130132
self.namespace = namespace
131133

132-
def _push_connection(self, connection):
133-
"""Push a connection/batch/transaction onto our stack.
134+
def _push_batch(self, batch):
135+
"""Push a batch/transaction onto our stack.
134136
135137
"Protected", intended for use by batch / transaction context mgrs.
136138
137-
:type connection: :class:`gcloud.datastore.connection.Connection`,
138-
or an object implementing its API.
139-
:param connection: newly-active connection/batch/transaction to
140-
pass to proxied API methods
139+
:type batch: :class:`gcloud.datastore.batch.Batch`, or an object
140+
implementing its API.
141+
:param batch: newly-active batch/batch/transaction.
141142
"""
142-
self._connection_stack.append(connection)
143+
self._batch_stack.push(batch)
143144

144-
def _pop_connection(self):
145-
"""Pop a connection/batch/transaction from our stack.
145+
def _pop_batch(self):
146+
"""Pop a batch/transaction from our stack.
146147
147148
"Protected", intended for use by batch / transaction context mgrs.
148149
149150
:raises: IndexError if the stack is empty.
150-
:rtype: :class:`gcloud.datastore.connection.Connection`, or
151-
an object implementing its API.
152-
:returns: the top-most connection/batch/transaction, after removing it.
151+
:rtype: :class:`gcloud.datastore.batch.Batch`, or an object
152+
implementing its API.
153+
:returns: the top-most batch/transaction, after removing it.
153154
"""
154-
return self._connection_stack.pop()
155+
return self._batch_stack.pop()
155156

156157
@property
157-
def connection(self):
158-
"""Currently-active connection.
158+
def current_batch(self):
159+
"""Currently-active batch.
159160
160-
:rtype: :class:`gcloud.datastore.connection.Connection`, or
161-
an object implementing its API.
162-
:returns: The connection/batch/transaction at the toop of the
163-
connection stack.
161+
:rtype: :class:`gcloud.datastore.batch.Batch`, or an object
162+
implementing its API, or ``NoneType`` (if no batch is active).
163+
:returns: The batch/transaction at the toop of the batch stack.
164164
"""
165-
return self._connection_stack[-1]
165+
return self._batch_stack.top
166166

167167
def get(self, key, missing=None, deferred=None):
168168
"""Retrieve an entity from a single key (if it exists).

gcloud/datastore/test_client.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def test_ctor_w_implicit_inputs(self):
6363
self.assertEqual(client.dataset_id, OTHER)
6464
self.assertEqual(client.namespace, None)
6565
self.assertTrue(client.connection is conn)
66+
self.assertTrue(client.current_batch is None)
6667

6768
def test_ctor_w_explicit_inputs(self):
6869
OTHER = 'other'
@@ -74,18 +75,25 @@ def test_ctor_w_explicit_inputs(self):
7475
self.assertEqual(client.dataset_id, OTHER)
7576
self.assertEqual(client.namespace, NAMESPACE)
7677
self.assertTrue(client.connection is conn)
77-
self.assertEqual(list(client._connection_stack), [conn])
78+
self.assertTrue(client.current_batch is None)
79+
self.assertEqual(list(client._batch_stack), [])
7880

7981
def test__push_connection_and__pop_connection(self):
8082
conn = object()
81-
new_conn = object()
83+
batch1 = object()
84+
batch2 = object()
8285
client = self._makeOne(connection=conn)
83-
client._push_connection(new_conn)
84-
self.assertTrue(client.connection is new_conn)
85-
self.assertEqual(list(client._connection_stack), [conn, new_conn])
86-
self.assertTrue(client._pop_connection() is new_conn)
87-
self.assertTrue(client.connection is conn)
88-
self.assertEqual(list(client._connection_stack), [conn])
86+
client._push_batch(batch1)
87+
self.assertEqual(list(client._batch_stack), [batch1])
88+
self.assertTrue(client.current_batch is batch1)
89+
client._push_batch(batch2)
90+
self.assertTrue(client.current_batch is batch2)
91+
# list(_LocalStack) returns in reverse order.
92+
self.assertEqual(list(client._batch_stack), [batch2, batch1])
93+
self.assertTrue(client._pop_batch() is batch2)
94+
self.assertEqual(list(client._batch_stack), [batch1])
95+
self.assertTrue(client._pop_batch() is batch1)
96+
self.assertEqual(list(client._batch_stack), [])
8997

9098
def test_get_miss(self):
9199
_called_with = []

0 commit comments

Comments
 (0)