Skip to content

Commit f92e9fe

Browse files
committed
Making _PropertyMixin.patch() and reload() reset the changes.
Fixes #786.
1 parent 24ba02b commit f92e9fe

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

gcloud/storage/_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def reload(self):
5656
query_params = {'projection': 'noAcl'}
5757
self._properties = self.connection.api_request(
5858
method='GET', path=self.path, query_params=query_params)
59+
# If the api_request succeeded, we reset changes.
60+
self._changes = set()
5961

6062
def _patch_properties(self, properties):
6163
"""Update particular fields of this object's properties.
@@ -84,6 +86,8 @@ def patch(self):
8486
self._properties = self.connection.api_request(
8587
method='PATCH', path=self.path, data=update_properties,
8688
query_params={'projection': 'full'})
89+
# If the api_request succeeded, we reset changes.
90+
self._changes = set()
8791

8892

8993
def _scalar_property(fieldname):

gcloud/storage/test__helpers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ def test_path_is_abstract(self):
4949
def test_reload(self):
5050
connection = _Connection({'foo': 'Foo'})
5151
derived = self._derivedClass(connection, '/path')()
52+
# Make sure changes is not a set, so we can observe a change.
53+
derived._changes = object()
5254
derived.reload()
5355
self.assertEqual(derived._properties, {'foo': 'Foo'})
5456
kw = connection._requested
5557
self.assertEqual(len(kw), 1)
5658
self.assertEqual(kw[0]['method'], 'GET')
5759
self.assertEqual(kw[0]['path'], '/path')
5860
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
61+
# Make sure changes get reset by reload.
62+
self.assertEqual(derived._changes, set())
5963

6064
def test__patch_properties(self):
6165
connection = _Connection({'foo': 'Foo'})
@@ -69,6 +73,26 @@ def test__patch_properties(self):
6973
self.assertEqual(kw[0]['data'], {'foo': 'Foo'})
7074
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
7175

76+
def test_patch(self):
77+
connection = _Connection({'foo': 'Foo'})
78+
derived = self._derivedClass(connection, '/path')()
79+
# Make sure changes is non-empty, so we can observe a change.
80+
BAR = object()
81+
BAZ = object()
82+
derived._properties = {'bar': BAR, 'baz': BAZ}
83+
derived._changes = set(['bar']) # Ignore baz.
84+
derived.patch()
85+
self.assertEqual(derived._properties, {'foo': 'Foo'})
86+
kw = connection._requested
87+
self.assertEqual(len(kw), 1)
88+
self.assertEqual(kw[0]['method'], 'PATCH')
89+
self.assertEqual(kw[0]['path'], '/path')
90+
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
91+
# Since changes does not include `baz`, we don't see it sent.
92+
self.assertEqual(kw[0]['data'], {'bar': BAR})
93+
# Make sure changes get reset by patch().
94+
self.assertEqual(derived._changes, set())
95+
7296

7397
class Test__scalar_property(unittest2.TestCase):
7498

0 commit comments

Comments
 (0)