Skip to content

Commit a295c2f

Browse files
committed
Making _PropertyBatch call _PropertyMixin.patch() on exit.
1 parent e9e31c9 commit a295c2f

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

gcloud/storage/_helpers.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,21 @@ def patch(self):
138138

139139

140140
class _PropertyBatch(object):
141-
"""Context manager: Batch updates to object's ``_patch_properties``
141+
"""Context manager: Batch updates to object.
142142
143143
:type wrapped: class derived from :class:`_PropertyMixin`.
144-
:param wrapped: the instance whose property updates to defer/batch.
144+
:param wrapped: the instance whose property updates to batch.
145145
"""
146146
def __init__(self, wrapped):
147147
self._wrapped = wrapped
148-
self._deferred = {}
149148

150149
def __enter__(self):
151-
"""Intercept / defer property updates."""
152-
self._wrapped._patch_properties = self._deferred.update
150+
"""Do nothing method. Needed to be a context manager."""
153151

154-
def __exit__(self, type, value, traceback):
152+
def __exit__(self, exc_type, value, traceback):
155153
"""Patch deferred property updates if no error."""
156-
del self._wrapped._patch_properties
157-
if type is None:
158-
if self._deferred:
159-
self._wrapped._patch_properties(self._deferred)
154+
if exc_type is None:
155+
self._wrapped.patch()
160156

161157

162158
def _scalar_property(fieldname):

gcloud/storage/test__helpers.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def test_batch(self):
5353
derived._patch_properties({'foo': 'Foo'})
5454
derived._patch_properties({'bar': 'Baz'})
5555
derived._patch_properties({'foo': 'Qux'})
56-
derived.patch()
5756
kw = connection._requested
5857
self.assertEqual(len(kw), 1)
5958
self.assertEqual(kw[0]['method'], 'PATCH')
@@ -97,7 +96,7 @@ def test__patch_properties(self):
9796
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
9897

9998

100-
class TestPropertyBatch(unittest2.TestCase):
99+
class Test_PropertyBatch(unittest2.TestCase):
101100

102101
def _getTargetClass(self):
103102
from gcloud.storage._helpers import _PropertyBatch
@@ -129,21 +128,11 @@ def test_ctor_does_not_intercept__patch_properties(self):
129128
self.assertEqual(before, after)
130129
self.assertTrue(batch._wrapped is wrapped)
131130

132-
def test_cm_intercepts_restores__patch_properties(self):
133-
wrapped = self._makeWrapped()
134-
before = wrapped._patch_properties
135-
batch = self._makeOne(wrapped)
136-
with batch:
137-
# No deferred patching -> no call to the real '_patch_properties'
138-
during = wrapped._patch_properties
139-
after = wrapped._patch_properties
140-
self.assertNotEqual(before, during)
141-
self.assertEqual(before, after)
142-
143-
def test___exit___w_error_skips__patch_properties(self):
131+
def test___exit___w_error_skips_patch(self):
144132
class Testing(Exception):
145133
pass
146-
wrapped = self._makeWrapped()
134+
connection = _Connection()
135+
wrapped = self._makeWrapped(connection)
147136
batch = self._makeOne(wrapped)
148137
try:
149138
with batch:
@@ -154,7 +143,10 @@ class Testing(Exception):
154143
except Testing:
155144
pass
156145

157-
def test___exit___no_error_aggregates__patch_properties(self):
146+
kw = connection._requested
147+
self.assertEqual(len(kw), 0)
148+
149+
def test___exit___no_error_calls_patch(self):
158150
connection = _Connection({'foo': 'Foo'})
159151
wrapped = self._makeWrapped(connection, '/path')
160152
batch = self._makeOne(wrapped)
@@ -165,7 +157,6 @@ def test___exit___no_error_aggregates__patch_properties(self):
165157
wrapped._patch_properties({'bar': 'Baz'})
166158
wrapped._patch_properties({'foo': 'Qux'})
167159
self.assertEqual(len(kw), 0)
168-
wrapped.patch()
169160
# exited w/o error -> call to the real '_patch_properties'
170161
self.assertEqual(len(kw), 1)
171162
self.assertEqual(kw[0]['method'], 'PATCH')

0 commit comments

Comments
 (0)