Skip to content

Commit

Permalink
Allow passing explicit connection to '_PropertyMixin.{reload,patch}'.
Browse files Browse the repository at this point in the history
See #825.
  • Loading branch information
tseaver committed May 1, 2015
1 parent fb59514 commit 843a50e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
25 changes: 20 additions & 5 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ def __init__(self, name=None):
self._properties = {}
self._changes = set()

def reload(self):
"""Reload properties from Cloud Storage."""
def reload(self, connection=None):
"""Reload properties from Cloud Storage.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: An explicit connection to use for the API request.
If not passed, use the connection assigned to
the object in its constructor.
"""
if connection is None:
connection = self.connection
# Pass only '?projection=noAcl' here because 'acl' and related
# are handled via custom endpoints.
query_params = {'projection': 'noAcl'}
api_response = self.connection.api_request(
api_response = connection.api_request(
method='GET', path=self.path, query_params=query_params)
self._set_properties(api_response)

Expand Down Expand Up @@ -89,16 +97,23 @@ def _set_properties(self, value):
# If the values are reset, the changes must as well.
self._changes = set()

def patch(self):
def patch(self, connection=None):
"""Sends all changed properties in a PATCH request.
Updates the ``_properties`` with the response from the backend.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: An explicit connection to use for the API request.
If not passed, use the connection assigned to
the object in its constructor.
"""
if connection is None:
connection = self.connection
# Pass '?projection=full' here because 'PATCH' documented not
# to work properly w/ 'noAcl'.
update_properties = dict((key, self._properties[key])
for key in self._changes)
api_response = self.connection.api_request(
api_response = connection.api_request(
method='PATCH', path=self.path, data=update_properties,
query_params={'projection': 'full'})
self._set_properties(api_response)
Expand Down
35 changes: 35 additions & 0 deletions gcloud/storage/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ def test_reload(self):
# Make sure changes get reset by reload.
self.assertEqual(derived._changes, set())

def test_reload_w_explicit_connection(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(None, '/path')()
# Make sure changes is not a set, so we can observe a change.
derived._changes = object()
derived.reload(connection)
self.assertEqual(derived._properties, {'foo': 'Foo'})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'GET')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
# Make sure changes get reset by reload.
self.assertEqual(derived._changes, set())

def test__patch_property(self):
derived = self._derivedClass()()
derived._patch_property('foo', 'Foo')
Expand All @@ -86,6 +101,26 @@ def test_patch(self):
# Make sure changes get reset by patch().
self.assertEqual(derived._changes, set())

def test_patch_w_explicit_connection(self):
connection = _Connection({'foo': 'Foo'})
derived = self._derivedClass(None, '/path')()
# Make sure changes is non-empty, so we can observe a change.
BAR = object()
BAZ = object()
derived._properties = {'bar': BAR, 'baz': BAZ}
derived._changes = set(['bar']) # Ignore baz.
derived.patch(connection)
self.assertEqual(derived._properties, {'foo': 'Foo'})
kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0]['method'], 'PATCH')
self.assertEqual(kw[0]['path'], '/path')
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
# Since changes does not include `baz`, we don't see it sent.
self.assertEqual(kw[0]['data'], {'bar': BAR})
# Make sure changes get reset by patch().
self.assertEqual(derived._changes, set())


class Test__scalar_property(unittest2.TestCase):

Expand Down

0 comments on commit 843a50e

Please sign in to comment.