mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
kvstore: Optionally, don't throw exception when deleting key
Tests: - Unit tests pass. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
e017e55a7b
commit
97b579c4fc
@ -27,7 +27,11 @@ def set(key, value): # pylint: disable-msg=W0622
|
|||||||
store.save()
|
store.save()
|
||||||
|
|
||||||
|
|
||||||
def delete(key):
|
def delete(key, ignore_missing=False):
|
||||||
"""Delete a key"""
|
"""Delete a key"""
|
||||||
from plinth.models import KVStore
|
from plinth.models import KVStore
|
||||||
return KVStore.objects.get(key=key).delete()
|
try:
|
||||||
|
return KVStore.objects.get(key=key).delete()
|
||||||
|
except KVStore.DoesNotExist:
|
||||||
|
if not ignore_missing:
|
||||||
|
raise
|
||||||
|
|||||||
@ -6,6 +6,7 @@ Test module for key/value store.
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from plinth import kvstore
|
from plinth import kvstore
|
||||||
|
from plinth.models import KVStore
|
||||||
|
|
||||||
pytestmark = pytest.mark.django_db
|
pytestmark = pytest.mark.django_db
|
||||||
|
|
||||||
@ -41,3 +42,19 @@ def test_get_default():
|
|||||||
expected = 'default'
|
expected = 'default'
|
||||||
actual = kvstore.get_default('bad_key', expected)
|
actual = kvstore.get_default('bad_key', expected)
|
||||||
assert expected == actual
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete():
|
||||||
|
"""Test that deleting key works."""
|
||||||
|
with pytest.raises(KVStore.DoesNotExist):
|
||||||
|
kvstore.delete('nonexistant_key')
|
||||||
|
|
||||||
|
with pytest.raises(KVStore.DoesNotExist):
|
||||||
|
kvstore.delete('nonexistant_key', ignore_missing=False)
|
||||||
|
|
||||||
|
kvstore.delete('nonexistant_key', ignore_missing=True)
|
||||||
|
|
||||||
|
kvstore.set('test-set-key', 'test-value')
|
||||||
|
kvstore.delete('test-set-key')
|
||||||
|
with pytest.raises(KVStore.DoesNotExist):
|
||||||
|
kvstore.delete('test-set-key')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user