10000 Changes to get_if_changed function. · firebase/firebase-admin-python@6f9d71a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f9d71a

Browse files
Alexander Whatleyaewhatley
authored andcommitted
Changes to get_if_changed function.
1 parent aae28c2 commit 6f9d71a

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

firebase_admin/db.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,18 @@ def get_if_changed(self, etag):
169169
Raises:
170170
ValueError: If the ETag is not a string.
171171
"""
172-
# pylint: disable=missing-raises-doc
172+
#pylint: disable=protected-access
173173
if not isinstance(etag, six.string_types):
174174
raise ValueError('ETag must be a string.')
175175

176-
try:
177-
value, headers = self._client.request('get', self._add_suffix(),
178-
headers={'if-none-match': etag},
179-
resp_headers=True)
176+
resp = self._client._do_request('get', self._add_suffix(),
177+
headers={'if-none-match': etag})
178+
if resp.status_code == 200:
179+
value, headers = resp.json(), resp.headers
180180
new_etag = headers.get('ETag')
181181
return True, new_etag, value
182-
except ApiCallError as error:
183-
# what to do here?
184-
raise error
182+
elif resp.status_code == 304:
183+
return False, etag, None
185184

186185
def set(self, value):
187186
"""Sets the data at this location to the given value.

integration/test_db.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ def test_etag(self, testref):
155155
etag = python.etag()
156156
assert isinstance(etag, six.string_types)
157157

158+
def test_get_if_changed(self, testref):
159+
python = testref.parent
160+
push_data = {'name' : 'Edward Cope', 'since' : 1800}
161+
edward = python.child('users').push(push_data)
162+
changed_data = edward.get_if_changed('wrong_etag')
163+
assert changed_data[0]
164+
assert changed_data[2] == push_data
165+
158166
def test_get_and_set_with_etag(self, testref):
159167
python = testref.parent
160168
push_data = {'name' : 'Edward Cope', 'since' : 1800}

tests/test_db.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ def test_get_with_etag(self, data):
153153
assert recorder[0].headers['Authorization'] == 'Bearer mock-token'
154154
assert recorder[0].headers['User-Agent'] == db._USER_AGENT
155155

156+
@pytest.mark.parametrize('data', valid_values)
157+
def test_get_if_changed(self, data):
158+
ref = db.reference('/test')
159+
recorder = self.instrument(ref, json.dumps(data))
160+
161+
assert ref.get_if_changed('1') == (True, '0', data)
162+
assert len(recorder) == 1
163+
assert recorder[0].method == 'GET'
164+
assert recorder[0].url == 'https://test.firebaseio.com/test.json'
165+
156166
@pytest.mark.parametrize('data', valid_values)
157167
def test_etag(self, data):
158168
ref = db.reference('/test')

0 commit comments

Comments
 (0)
0