Migrate motor to pymongo (#1705)

* 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 commit 5d0f6a9dfb.

* added optional dependency to pymongo to pyproject.toml

* Revert "migrated mongo storage from using deprecated motor to PyMongo"

This reverts commit 1c0207e1d1.

* 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 commit 86170e1cb9.

* fixed linting issues in PyMongoStorage

* changed allowed versions of pymongo, to be compatible with motor

* pinned the upper version of pymongo to <4.11
This commit is contained in:
kievzenit 2025-08-17 19:16:47 +03:00 committed by GitHub
parent 6aa6e008c2
commit 99fa2460da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 398 additions and 0 deletions

View file

@ -17,6 +17,7 @@ from aiogram.fsm.storage.memory import (
SimpleEventIsolation,
)
from aiogram.fsm.storage.mongo import MongoStorage
from aiogram.fsm.storage.pymongo import PyMongoStorage
from aiogram.fsm.storage.redis import RedisStorage
from tests.mocked_bot import MockedBot
@ -102,6 +103,36 @@ async def mongo_storage(mongo_server):
await storage.close()
@pytest.fixture()
def pymongo_server(request):
mongo_uri = request.config.getoption("--mongo")
if mongo_uri is None:
pytest.skip(SKIP_MESSAGE_PATTERN.format(db="mongo"))
else:
return mongo_uri
@pytest.fixture()
async def pymongo_storage(pymongo_server):
try:
parse_mongo_url(pymongo_server)
except InvalidURI as e:
raise UsageError(INVALID_URI_PATTERN.format(db="mongo", uri=pymongo_server, err=e))
storage = PyMongoStorage.from_url(
url=pymongo_server,
connection_kwargs={"serverSelectionTimeoutMS": 2000},
)
try:
await storage._client.server_info()
except PyMongoError as e:
pytest.fail(str(e))
else:
yield storage
await storage._client.drop_database(storage._database)
finally:
await storage.close()
@pytest.fixture()
async def memory_storage():
storage = MemoryStorage()