mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
feat(storage): add get_value method to MongoStorage
Implement get_value method for MongoStorage that retrieves individual values from storage data using MongoDB projections. Add comprehensive test coverage for the new method.
This commit is contained in:
parent
25e9127db9
commit
49e9b34b3f
3 changed files with 101 additions and 1 deletions
|
|
@ -149,6 +149,88 @@ class TestStateAndDataDoNotAffectEachOther:
|
|||
}
|
||||
|
||||
|
||||
class TestGetValue:
|
||||
async def test_get_existing_value(
|
||||
self,
|
||||
mongo_storage: MongoStorage,
|
||||
storage_key: StorageKey,
|
||||
):
|
||||
await mongo_storage.set_data(
|
||||
storage_key, {"key": "value", "number": 42, "list": [1, 2, 3]}
|
||||
)
|
||||
|
||||
assert await mongo_storage.get_value(storage_key, "key") == "value"
|
||||
assert await mongo_storage.get_value(storage_key, "number") == 42
|
||||
assert await mongo_storage.get_value(storage_key, "list") == [1, 2, 3]
|
||||
|
||||
async def test_get_non_existing_value(
|
||||
self,
|
||||
mongo_storage: MongoStorage,
|
||||
storage_key: StorageKey,
|
||||
):
|
||||
await mongo_storage.set_data(storage_key, {"key": "value"})
|
||||
|
||||
assert await mongo_storage.get_value(storage_key, "non_existing_key") is None
|
||||
assert (
|
||||
await mongo_storage.get_value(storage_key, "non_existing_key", default="default")
|
||||
== "default"
|
||||
)
|
||||
|
||||
async def test_get_value_from_non_existing_document(
|
||||
self,
|
||||
mongo_storage: MongoStorage,
|
||||
storage_key: StorageKey,
|
||||
):
|
||||
assert await mongo_storage._collection.find_one({}) is None
|
||||
|
||||
assert await mongo_storage.get_value(storage_key, "any_key") is None
|
||||
assert (
|
||||
await mongo_storage.get_value(storage_key, "any_key", default="default") == "default"
|
||||
)
|
||||
|
||||
async def test_get_value_with_document_containing_only_state(
|
||||
self,
|
||||
mongo_storage: MongoStorage,
|
||||
storage_key: StorageKey,
|
||||
):
|
||||
await mongo_storage.set_state(storage_key, "test")
|
||||
|
||||
document = await mongo_storage._collection.find_one({})
|
||||
assert document is not None
|
||||
assert "data" not in document
|
||||
|
||||
assert await mongo_storage.get_value(storage_key, "any_key") is None
|
||||
assert (
|
||||
await mongo_storage.get_value(storage_key, "any_key", default="default") == "default"
|
||||
)
|
||||
|
||||
async def test_get_value_uses_projection(
|
||||
self,
|
||||
mongo_storage: MongoStorage,
|
||||
storage_key: StorageKey,
|
||||
monkeypatch,
|
||||
):
|
||||
await mongo_storage.set_data(
|
||||
storage_key, {"key1": "value1", "key2": "value2", "key3": {"nested": "data"}}
|
||||
)
|
||||
|
||||
original_find_one = mongo_storage._collection.find_one
|
||||
calls = []
|
||||
|
||||
async def mock_find_one(*args, **kwargs):
|
||||
calls.append(kwargs)
|
||||
return await original_find_one(*args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(mongo_storage._collection, "find_one", mock_find_one)
|
||||
|
||||
value = await mongo_storage.get_value(storage_key, "key2")
|
||||
|
||||
assert len(calls) == 1
|
||||
assert "projection" in calls[0]
|
||||
assert calls[0]["projection"] == {"_id": 0, "data.key2": 1}
|
||||
assert value == "value2"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value,result",
|
||||
[
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue