Improve docstrings for key-value store

This commit is contained in:
Matthias 2023-04-08 10:09:31 +02:00
parent 4d4f4bf23e
commit ac817b7808
2 changed files with 11 additions and 0 deletions

View File

@ -38,11 +38,17 @@ class _KeyValueStoreModel(ModelBase):
class KeyValueStore():
"""
Generic bot-wide, persistent key-value store
Can be used to store generic values, e.g. very first bot startup time.
Supports the types str, datetime, float and int.
"""
@staticmethod
def get_value(key: str) -> Optional[ValueTypes]:
"""
Get the value for the given key.
:param key: Key to get the value for
"""
kv = _KeyValueStoreModel.session.query(_KeyValueStoreModel).filter(
_KeyValueStoreModel.key == key).first()
@ -63,6 +69,8 @@ class KeyValueStore():
def store_value(key: str, value: ValueTypes) -> None:
"""
Store the given value for the given key.
:param key: Key to store the value for - can be used in get-value to retrieve the key
:param value: Value to store - can be str, datetime, float or int
"""
kv = _KeyValueStoreModel.session.query(_KeyValueStoreModel).filter(
_KeyValueStoreModel.key == key).first()
@ -89,6 +97,7 @@ class KeyValueStore():
def delete_value(key: str) -> None:
"""
Delete the value for the given key.
:param key: Key to delete the value for
"""
kv = _KeyValueStoreModel.session.query(_KeyValueStoreModel).filter(
_KeyValueStoreModel.key == key).first()

View File

@ -32,6 +32,8 @@ def test_key_value_store(time_machine):
# test deleting
KeyValueStore.delete_value("test_float")
assert KeyValueStore.get_value("test_float") is None
# Delete same value again (should not fail)
KeyValueStore.delete_value("test_float")
with pytest.raises(ValueError, match=r"Unknown value type"):
KeyValueStore.store_value("test_float", {'some': 'dict'})