Rework FSM storage key

This commit is contained in:
Alex Root Junior 2021-10-11 01:30:19 +03:00
parent 8c4d4ef30a
commit 7c6cf3c122
10 changed files with 213 additions and 160 deletions

View file

@ -1,6 +1,7 @@
import pytest
from aiogram.dispatcher.fsm.context import FSMContext
from aiogram.dispatcher.fsm.storage.base import StorageKey
from aiogram.dispatcher.fsm.storage.memory import MemoryStorage
from tests.mocked_bot import MockedBot
@ -10,21 +11,28 @@ pytestmark = pytest.mark.asyncio
@pytest.fixture()
def state(bot: MockedBot):
storage = MemoryStorage()
ctx = storage.storage[bot][-42][42]
key = StorageKey(user_id=42, chat_id=-42, bot_id=bot.id)
ctx = storage.storage[key]
ctx.state = "test"
ctx.data = {"foo": "bar"}
return FSMContext(bot=bot, storage=storage, user_id=-42, chat_id=42)
return FSMContext(bot=bot, storage=storage, key=key)
class TestFSMContext:
async def test_address_mapping(self, bot: MockedBot):
storage = MemoryStorage()
ctx = storage.storage[bot][-42][42]
ctx = storage.storage[StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)]
ctx.state = "test"
ctx.data = {"foo": "bar"}
state = FSMContext(bot=bot, storage=storage, chat_id=-42, user_id=42)
state2 = FSMContext(bot=bot, storage=storage, chat_id=42, user_id=42)
state3 = FSMContext(bot=bot, storage=storage, chat_id=69, user_id=69)
state = FSMContext(
bot=bot, storage=storage, key=StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
)
state2 = FSMContext(
bot=bot, storage=storage, key=StorageKey(chat_id=42, user_id=42, bot_id=bot.id)
)
state3 = FSMContext(
bot=bot, storage=storage, key=StorageKey(chat_id=69, user_id=69, bot_id=bot.id)
)
assert await state.get_state() == "test"
assert await state2.get_state() is None