Add function get_value to BaseStorage (#1594)

* Add function `get_value` to all built-in storage implementations, `FSMContext` and `SceneWizard` (#1431)

* Fix type hints

* Split up return statements in `get_value` functions

* Implement `get_value` method in `BaseStorage` and remove redundant implementations
This commit is contained in:
Arthur Khachaturov 2024-11-02 17:48:01 +03:00 committed by GitHub
parent fd014d2026
commit 592267dd99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 120 additions and 4 deletions

View file

@ -22,11 +22,27 @@ class TestStorages:
async def test_set_data(self, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_data(key=storage_key) == {}
assert await storage.get_value(storage_key=storage_key, dict_key="foo") is None
assert (
await storage.get_value(storage_key=storage_key, dict_key="foo", default="baz")
== "baz"
)
await storage.set_data(key=storage_key, data={"foo": "bar"})
assert await storage.get_data(key=storage_key) == {"foo": "bar"}
assert await storage.get_value(storage_key=storage_key, dict_key="foo") == "bar"
assert (
await storage.get_value(storage_key=storage_key, dict_key="foo", default="baz")
== "bar"
)
await storage.set_data(key=storage_key, data={})
assert await storage.get_data(key=storage_key) == {}
assert await storage.get_value(storage_key=storage_key, dict_key="foo") is None
assert (
await storage.get_value(storage_key=storage_key, dict_key="foo", default="baz")
== "baz"
)
async def test_update_data(self, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_data(key=storage_key) == {}

View file

@ -34,6 +34,10 @@ class TestFSMContext:
assert await state2.get_data() == {}
assert await state3.get_data() == {}
assert await state.get_value("foo") == "bar"
assert await state2.get_value("foo") is None
assert await state3.get_value("foo", "baz") == "baz"
await state2.set_state("experiments")
assert await state.get_state() == "test"
assert await state3.get_state() is None

View file

@ -1004,6 +1004,24 @@ class TestSceneWizard:
wizard.state.get_data.assert_called_once_with()
async def test_scene_wizard_get_value_with_default(self):
wizard = SceneWizard(
scene_config=AsyncMock(),
manager=AsyncMock(),
state=AsyncMock(),
update_type="message",
event=AsyncMock(),
data={},
)
args = ("test_key", "test_default")
value = "test_value"
wizard.state.get_value = AsyncMock(return_value=value)
result = await wizard.get_value(*args)
wizard.state.get_value.assert_called_once_with(*args)
assert result == value
async def test_scene_wizard_update_data_if_data(self):
wizard = SceneWizard(
scene_config=AsyncMock(),