mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
* migrated mongo storage from using deprecated motor to PyMongo * added storages to __init__.py file to improve DX * changelog file created * Revert "added storages to __init__.py file to improve DX" This reverts commit5d0f6a9dfb. * added optional dependency to pymongo to pyproject.toml * Revert "migrated mongo storage from using deprecated motor to PyMongo" This reverts commit1c0207e1d1. * added deprecation warning to mongo storage * created pymongo storage * added entry for PyMongoStorage to documentation in fsm.storages * updated changelog to have information about how to migrate from MongoStorage to PyMongoStorage * added test for pymongo storage (copied from mongo storage test) * fixed formatting using black and isort * fixed bug in close method of PyMongoStorage (client close method was not awaited) * added test for PyMongoStorage that checks if storage could be properly closed * pymongo package changed to be lower case in PyMongoStorage * added fixture registration for pymongo storage * test for pymongo is now using proper test fixtures * removed redundant call to get_data, because we have checked this condition in the previous line * added more tests to pymongo test, to check for all possible cases of using update_data method * fixed PyMongoStorage update_data method implementation * added pymongo tests to test_storages * fixed pymongo tests, update_data method should not delete document when {} was passed * Revert "fixed PyMongoStorage update_data method implementation" This reverts commit86170e1cb9. * fixed linting issues in PyMongoStorage * changed allowed versions of pymongo, to be compatible with motor * pinned the upper version of pymongo to <4.11
86 lines
3.4 KiB
Python
86 lines
3.4 KiB
Python
from typing import TypedDict
|
|
|
|
import pytest
|
|
|
|
from aiogram.exceptions import DataNotDictLikeError
|
|
from aiogram.fsm.storage.base import BaseStorage, StorageKey
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"storage",
|
|
[
|
|
pytest.lazy_fixture("redis_storage"),
|
|
pytest.lazy_fixture("mongo_storage"),
|
|
pytest.lazy_fixture("pymongo_storage"),
|
|
pytest.lazy_fixture("memory_storage"),
|
|
],
|
|
)
|
|
class TestStorages:
|
|
async def test_set_state(self, storage: BaseStorage, storage_key: StorageKey):
|
|
assert await storage.get_state(key=storage_key) is None
|
|
|
|
await storage.set_state(key=storage_key, state="state")
|
|
assert await storage.get_state(key=storage_key) == "state"
|
|
await storage.set_state(key=storage_key, state=None)
|
|
assert await storage.get_state(key=storage_key) is None
|
|
|
|
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"
|
|
)
|
|
|
|
class CustomTypedDict(TypedDict, total=False):
|
|
foo: str
|
|
bar: str
|
|
|
|
await storage.set_data(key=storage_key, data=CustomTypedDict(foo="bar", bar="baz"))
|
|
assert await storage.get_data(key=storage_key) == {"foo": "bar", "bar": "baz"}
|
|
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"
|
|
)
|
|
|
|
with pytest.raises(DataNotDictLikeError):
|
|
await storage.set_data(key=storage_key, data=())
|
|
|
|
async def test_update_data(self, storage: BaseStorage, storage_key: StorageKey):
|
|
assert await storage.get_data(key=storage_key) == {}
|
|
assert await storage.update_data(key=storage_key, data={"foo": "bar"}) == {"foo": "bar"}
|
|
assert await storage.update_data(key=storage_key, data={}) == {"foo": "bar"}
|
|
assert await storage.get_data(key=storage_key) == {"foo": "bar"}
|
|
assert await storage.update_data(key=storage_key, data={"baz": "spam"}) == {
|
|
"foo": "bar",
|
|
"baz": "spam",
|
|
}
|
|
assert await storage.get_data(key=storage_key) == {
|
|
"foo": "bar",
|
|
"baz": "spam",
|
|
}
|
|
assert await storage.update_data(key=storage_key, data={"baz": "test"}) == {
|
|
"foo": "bar",
|
|
"baz": "test",
|
|
}
|
|
assert await storage.get_data(key=storage_key) == {
|
|
"foo": "bar",
|
|
"baz": "test",
|
|
}
|