Skip to content

Commit

Permalink
Move '_scalar_property' to '_helpers'.
Browse files Browse the repository at this point in the history
Also, add explicit coverage for it.
  • Loading branch information
tseaver committed Nov 7, 2014
1 parent 8cd4c5e commit 37046e2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
14 changes: 14 additions & 0 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,17 @@ def __exit__(self, type, value, traceback):
if type is None:
if self._deferred:
self._wrapped._patch_properties(self._deferred)


def _scalar_property(fieldname):
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
"""
def _getter(self):
"""Scalar property getter."""
return self.properties[fieldname]

def _setter(self, value):
"""Scalar property setter."""
self._patch_properties({fieldname: value})

return property(_getter, _setter)
15 changes: 1 addition & 14 deletions gcloud/storage/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,12 @@
from StringIO import StringIO

from gcloud.storage._helpers import _PropertyMixin
from gcloud.storage._helpers import _scalar_property
from gcloud.storage.acl import ObjectACL
from gcloud.storage.exceptions import StorageError
from gcloud.storage.iterator import Iterator


def _scalar_property(fieldname):
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
"""
def _getter(self):
"""Scalar property getter."""
return self.properties[fieldname]

def _setter(self, value):
"""Scalar property setter."""
self._patch_properties({fieldname: value})

return property(_getter, _setter)


class Key(_PropertyMixin):
"""A wrapper around Cloud Storage's concept of an ``Object``."""

Expand Down
28 changes: 28 additions & 0 deletions gcloud/storage/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,34 @@ def test___exit___no_error_aggregates__patch_properties(self):
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})


class Test__scalar_property(unittest2.TestCase):

def _callFUT(self, fieldName):
from gcloud.storage._helpers import _scalar_property
return _scalar_property(fieldName)

def test_getter(self):

class Test(object):
def __init__(self, **kw):
self.properties = kw.copy()
do_re_mi = self._callFUT('solfege')

test = Test(solfege='Latido')
self.assertEqual(test.do_re_mi, 'Latido')

def test_setter(self):

class Test(object):
def _patch_properties(self, mapping):
self._patched = mapping.copy()
do_re_mi = self._callFUT('solfege')

test = Test()
test.do_re_mi = 'Latido'
self.assertEqual(test._patched, {'solfege': 'Latido'})


class _Connection(object):

def __init__(self, *responses):
Expand Down

0 comments on commit 37046e2

Please sign in to comment.