mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Added MongoStorage for FSM
This commit is contained in:
parent
329c7f4c97
commit
a5ccf28498
11 changed files with 312 additions and 102 deletions
|
|
@ -2,6 +2,8 @@ from pathlib import Path
|
|||
|
||||
import pytest
|
||||
from _pytest.config import UsageError
|
||||
from pymongo.errors import InvalidURI, PyMongoError
|
||||
from pymongo.uri_parser import parse_uri as parse_mongo_url
|
||||
from redis.asyncio.connection import parse_url as parse_redis_url
|
||||
|
||||
from aiogram import Dispatcher
|
||||
|
|
@ -10,6 +12,7 @@ from aiogram.fsm.storage.memory import (
|
|||
MemoryStorage,
|
||||
SimpleEventIsolation,
|
||||
)
|
||||
from aiogram.fsm.storage.mongo import MongoStorage
|
||||
from aiogram.fsm.storage.redis import RedisEventIsolation, RedisStorage
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
|
|
@ -18,24 +21,27 @@ DATA_DIR = Path(__file__).parent / "data"
|
|||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--redis", default=None, help="run tests which require redis connection")
|
||||
parser.addoption("--mongo", default=None, help="run tests which require mongo connection")
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
config.addinivalue_line("markers", "redis: marked tests require redis connection to run")
|
||||
config.addinivalue_line("markers", "mongo: marked tests require mongo connection to run")
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(config, items):
|
||||
redis_uri = config.getoption("--redis")
|
||||
if redis_uri is None:
|
||||
skip_redis = pytest.mark.skip(reason="need --redis option with redis URI to run")
|
||||
for item in items:
|
||||
if "redis" in item.keywords:
|
||||
item.add_marker(skip_redis)
|
||||
return
|
||||
try:
|
||||
parse_redis_url(redis_uri)
|
||||
except ValueError as e:
|
||||
raise UsageError(f"Invalid redis URI {redis_uri!r}: {e}")
|
||||
for db, parse_uri in [("redis", parse_redis_url), ("mongo", parse_mongo_url)]:
|
||||
uri = config.getoption(f"--{db}")
|
||||
if uri is None:
|
||||
skip = pytest.mark.skip(reason=f"need --{db} option with {db} URI to run")
|
||||
for item in items:
|
||||
if db in item.keywords:
|
||||
item.add_marker(skip)
|
||||
else:
|
||||
try:
|
||||
parse_uri(uri)
|
||||
except (ValueError, InvalidURI) as e:
|
||||
raise UsageError(f"Invalid {db} URI {uri!r}: {e}")
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
|
@ -62,6 +68,29 @@ async def redis_storage(redis_server):
|
|||
await storage.close()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mongo_server(request):
|
||||
mongo_uri = request.config.getoption("--mongo")
|
||||
return mongo_uri
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@pytest.mark.mongo
|
||||
async def mongo_storage(mongo_server):
|
||||
if not mongo_server:
|
||||
pytest.skip("MongoDB is not available here")
|
||||
storage = MongoStorage.from_url(mongo_server)
|
||||
try:
|
||||
await storage._client.server_info()
|
||||
except PyMongoError as e:
|
||||
pytest.skip(str(e))
|
||||
else:
|
||||
yield storage
|
||||
await storage._client.drop_database(storage._db_name)
|
||||
finally:
|
||||
await storage.close()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
async def memory_storage():
|
||||
storage = MemoryStorage()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue