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()
|
||||
|
|
|
|||
|
|
@ -5,3 +5,11 @@ services:
|
|||
image: redis:6-alpine
|
||||
ports:
|
||||
- "${REDIS_PORT-6379}:6379"
|
||||
|
||||
mongo:
|
||||
image: mongo:7.0.6
|
||||
environment:
|
||||
MONGO_INITDB_ROOT_USERNAME: mongo
|
||||
MONGO_INITDB_ROOT_PASSWORD: mongo
|
||||
ports:
|
||||
- "${MONGODB_PORT-27017}:27017"
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, patch
|
|||
import pytest
|
||||
|
||||
from aiogram.fsm.storage.base import BaseEventIsolation, StorageKey
|
||||
from aiogram.fsm.storage.redis import RedisEventIsolation
|
||||
from aiogram.fsm.storage.redis import RedisEventIsolation, RedisStorage
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
|
||||
|
|
@ -32,6 +32,14 @@ class TestIsolations:
|
|||
|
||||
|
||||
class TestRedisEventIsolation:
|
||||
def test_create_isolation(self):
|
||||
fake_redis = object()
|
||||
storage = RedisStorage(redis=fake_redis)
|
||||
isolation = storage.create_isolation()
|
||||
assert isinstance(isolation, RedisEventIsolation)
|
||||
assert isolation.redis is fake_redis
|
||||
assert isolation.key_builder is storage.key_builder
|
||||
|
||||
def test_init_without_key_builder(self):
|
||||
redis = AsyncMock()
|
||||
isolation = RedisEventIsolation(redis=redis)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
import pytest
|
||||
|
||||
from aiogram.fsm.storage.base import DEFAULT_DESTINY, StorageKey
|
||||
from aiogram.fsm.storage.redis import (
|
||||
DefaultKeyBuilder,
|
||||
RedisEventIsolation,
|
||||
RedisStorage,
|
||||
)
|
||||
from aiogram.fsm.storage.redis import DefaultKeyBuilder
|
||||
|
||||
PREFIX = "test"
|
||||
BOT_ID = 42
|
||||
|
|
@ -15,7 +11,7 @@ THREAD_ID = 3
|
|||
FIELD = "data"
|
||||
|
||||
|
||||
class TestRedisDefaultKeyBuilder:
|
||||
class TestDefaultKeyBuilder:
|
||||
@pytest.mark.parametrize(
|
||||
"with_bot_id,with_destiny,result",
|
||||
[
|
||||
|
|
@ -59,11 +55,3 @@ class TestRedisDefaultKeyBuilder:
|
|||
destiny=DEFAULT_DESTINY,
|
||||
)
|
||||
assert key_builder.build(key, FIELD) == f"{PREFIX}:{CHAT_ID}:{THREAD_ID}:{USER_ID}:{FIELD}"
|
||||
|
||||
def test_create_isolation(self):
|
||||
fake_redis = object()
|
||||
storage = RedisStorage(redis=fake_redis)
|
||||
isolation = storage.create_isolation()
|
||||
assert isinstance(isolation, RedisEventIsolation)
|
||||
assert isolation.redis is fake_redis
|
||||
assert isolation.key_builder is storage.key_builder
|
||||
37
tests/test_fsm/storage/test_mongo.py
Normal file
37
tests/test_fsm/storage/test_mongo.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import pytest
|
||||
|
||||
from aiogram.fsm.state import State
|
||||
from aiogram.fsm.storage.mongo import MongoStorage, StorageKey
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
|
||||
@pytest.fixture(name="storage_key")
|
||||
def create_storage_key(bot: MockedBot):
|
||||
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
|
||||
|
||||
|
||||
async def test_update_not_existing_data_with_empty_dictionary(
|
||||
mongo_storage: MongoStorage,
|
||||
storage_key: StorageKey,
|
||||
):
|
||||
assert await mongo_storage._data_collection.find_one({}) is None
|
||||
assert await mongo_storage.get_data(key=storage_key) == {}
|
||||
assert await mongo_storage.update_data(key=storage_key, data={}) == {}
|
||||
assert await mongo_storage._data_collection.find_one({}) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value,result",
|
||||
[
|
||||
[None, None],
|
||||
["", ""],
|
||||
["text", "text"],
|
||||
[State(), None],
|
||||
[State(state="*"), "*"],
|
||||
[State("text"), "@:text"],
|
||||
[State("test", group_name="Test"), "Test:test"],
|
||||
[[1, 2, 3], "[1, 2, 3]"],
|
||||
],
|
||||
)
|
||||
def test_resolve_state(value, result, mongo_storage: MongoStorage):
|
||||
assert mongo_storage.resolve_state(value) == result
|
||||
Loading…
Add table
Add a link
Reference in a new issue