Skip to content

Commit b01f30d

Browse files
authored
Accepting None values in ref.update() (#162)
1 parent 351d624 commit b01f30d

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

3-
-
3+
- [fixed] The `db.Reference.update()` function now accepts dictionaries with
4+
`None` values. This can be used to delete child keys from a reference.
45

56
# v2.10.0
67

firebase_admin/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ def update(self, value):
269269
"""
270270
if not value or not isinstance(value, dict):
271271
raise ValueError('Value argument must be a non-empty dictionary.')
272-
if None in value.keys() or None in value.values():
273-
raise ValueError('Dictionary must not contain None keys or values.')
272+
if None in value.keys():
273+
raise ValueError('Dictionary must not contain None keys.')
274274
self._client.request('patch', self._add_suffix(), json=value, params='print=silent')
275275

276276
def delete(self):

integration/test_db.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ def test_update_children(self, testref):
155155

156156
def test_update_children_with_existing_values(self, testref):
157157
python = testref.parent
158-
ref = python.child('users').push({'name' : 'Edwin Colbert', 'since' : 1900})
158+
value = {'name' : 'Edwin Colbert', 'since' : 1900, 'temp': True}
159+
ref = python.child('users').push(value)
159160
ref.update({'since' : 1905})
160-
assert ref.get() == {'name' : 'Edwin Colbert', 'since' : 1905}
161+
value['since'] = 1905
162+
assert ref.get() == value
163+
ref.update({'temp': None})
164+
del value['temp']
165+
assert ref.get() == value
161166

162167
def test_update_nested_children(self, testref):
163168
python = testref.parent

tests/test_db.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ def test_set_non_json_value(self, value):
259259
with pytest.raises(TypeError):
260260
ref.set(value)
261261

262-
def test_update_children(self):
262+
@pytest.mark.parametrize('data', [{'foo': 'bar'}, {'foo': None}])
263+
def test_update_children(self, data):
263264
ref = db.reference('/test')
264-
data = {'foo' : 'bar'}
265265
recorder = self.instrument(ref, json.dumps(data))
266266
ref.update(data)
267267
assert len(recorder) == 1
@@ -317,21 +317,15 @@ def test_set_if_unchanged_non_json_value(self, value):
317317
with pytest.raises(TypeError):
318318
ref.set_if_unchanged(MockAdapter.ETAG, value)
319319

320-
def test_update_children_default(self):
321-
ref = db.reference('/test')
322-
recorder = self.instrument(ref, '')
323-
with pytest.raises(ValueError):
324-
ref.update({})
325-
assert len(recorder) is 0
326-
327320
@pytest.mark.parametrize('update', [
328-
None, {}, {None:'foo'}, {'foo': None}, '', 'foo', 0, 1, list(), tuple(), _Object()
321+
None, {}, {None:'foo'}, '', 'foo', 0, 1, list(), tuple(), _Object()
329322
])
330323
def test_set_invalid_update(self, update):
331324
ref = db.reference('/test')
332-
self.instrument(ref, '')
325+
recorder = self.instrument(ref, '')
333326
with pytest.raises(ValueError):
334327
ref.update(update)
328+
assert len(recorder) is 0
335329

336330
@pytest.mark.parametrize('data', valid_values)
337331
def test_push(self, data):

0 commit comments

Comments
 (0)