Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing _PropertyBatch from storage. #782

Merged
merged 1 commit into from
Mar 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,6 @@ def __init__(self, name=None):
self._properties = {}
self._changes = set()

@property
def batch(self):
"""Return a context manager which defers/batches updates.

E.g., to batch multiple updates to a bucket::

>>> with bucket.batch:
... bucket.enable_versioning()
... bucket.disable_website()

or for a blob::

>>> with blob.batch:
... blob.content_type = 'image/jpeg'
... blob.content_encoding = 'gzip'

Updates will be aggregated and sent as a single call to
:meth:`_patch_properties` IFF the ``with`` block exits without
an exception.

:rtype: :class:`_PropertyBatch`
"""
return _PropertyBatch(self)

def reload(self):
"""Reload properties from Cloud Storage.

Expand Down Expand Up @@ -123,24 +99,6 @@ def patch(self):
return self


class _PropertyBatch(object):
"""Context manager: Batch updates to object.

:type wrapped: class derived from :class:`_PropertyMixin`.
:param wrapped: the instance whose property updates to batch.
"""
def __init__(self, wrapped):
self._wrapped = wrapped

def __enter__(self):
"""Do nothing method. Needed to be a context manager."""

def __exit__(self, exc_type, value, traceback):
"""Patch deferred property updates if no error."""
if exc_type is None:
self._wrapped.patch()


def _scalar_property(fieldname):
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
"""
Expand Down
83 changes: 0 additions & 83 deletions gcloud/storage/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,6 @@ def test_path_is_abstract(self):
mixin = self._makeOne()
self.assertRaises(NotImplementedError, lambda: mixin.path)

def test_batch(self):
connection = _Connection({'foo': 'Qux', 'bar': 'Baz'})
derived = self._derivedClass(connection, '/path')()
with derived.batch:
derived._patch_properties({'foo': 'Foo'})
derived._patch_properties({'bar': 'Baz'})
derived._patch_properties({'foo': 'Qux'})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['data'], {'foo': 'Qux', 'bar': 'Baz'})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})

def test_reload(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(connection, '/path')()
Expand All @@ -84,75 +70,6 @@ def test__patch_properties(self):
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})


class Test_PropertyBatch(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.storage._helpers import _PropertyBatch
return _PropertyBatch

def _makeOne(self, wrapped):
return self._getTargetClass()(wrapped)

def _makeWrapped(self, connection=None, path=None):
from gcloud.storage._helpers import _PropertyMixin

class Wrapped(_PropertyMixin):

@property
def connection(self):
return connection

@property
def path(self):
return path

return Wrapped()

def test_ctor_does_not_intercept__patch_properties(self):
wrapped = self._makeWrapped()
before = wrapped._patch_properties
batch = self._makeOne(wrapped)
after = wrapped._patch_properties
self.assertEqual(before, after)
self.assertTrue(batch._wrapped is wrapped)

def test___exit___w_error_skips_patch(self):
class Testing(Exception):
pass
connection = _Connection()
wrapped = self._makeWrapped(connection)
batch = self._makeOne(wrapped)
try:
with batch:
# deferred patching
wrapped._patch_properties({'foo': 'Foo'})
# but error -> no call to the real '_patch_properties'
raise Testing('testing')
except Testing:
pass

kw = connection._requested
self.assertEqual(len(kw), 0)

def test___exit___no_error_calls_patch(self):
connection = _Connection({'foo': 'Foo'})
wrapped = self._makeWrapped(connection, '/path')
batch = self._makeOne(wrapped)
kw = connection._requested
with batch:
# deferred patching
wrapped._patch_properties({'foo': 'Foo'})
wrapped._patch_properties({'bar': 'Baz'})
wrapped._patch_properties({'foo': 'Qux'})
self.assertEqual(len(kw), 0)
# exited w/o error -> call to the real '_patch_properties'
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['data'], {'foo': 'Qux', 'bar': 'Baz'})
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})


class Test__scalar_property(unittest2.TestCase):

def _callFUT(self, fieldName):
Expand Down