aiogram/tests/test_fsm/test_context.py

58 lines
1.9 KiB
Python
Raw Normal View History

2021-05-13 22:04:10 +03:00
import pytest
from aiogram.fsm.context import FSMContext
from aiogram.fsm.storage.base import StorageKey
from aiogram.fsm.storage.memory import MemoryStorage
from tests.mocked_bot import MockedBot
2021-05-13 22:04:10 +03:00
@pytest.fixture()
def state(bot: MockedBot):
2021-05-13 22:04:10 +03:00
storage = MemoryStorage()
2021-10-11 01:30:19 +03:00
key = StorageKey(user_id=42, chat_id=-42, bot_id=bot.id)
ctx = storage.storage[key]
2021-05-13 22:04:10 +03:00
ctx.state = "test"
ctx.data = {"foo": "bar"}
2021-10-11 01:30:19 +03:00
return FSMContext(bot=bot, storage=storage, key=key)
2021-05-13 22:04:10 +03:00
class TestFSMContext:
async def test_address_mapping(self, bot: MockedBot):
2021-05-13 22:04:10 +03:00
storage = MemoryStorage()
2021-10-11 01:30:19 +03:00
ctx = storage.storage[StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)]
2021-05-13 22:04:10 +03:00
ctx.state = "test"
ctx.data = {"foo": "bar"}
2021-10-11 01:30:19 +03:00
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)
)
2021-05-13 22:04:10 +03:00
assert await state.get_state() == "test"
assert await state2.get_state() is None
assert await state3.get_state() is None
assert await state.get_data() == {"foo": "bar"}
assert await state2.get_data() == {}
assert await state3.get_data() == {}
await state2.set_state("experiments")
assert await state.get_state() == "test"
assert await state3.get_state() is None
await state3.set_data({"key": "value"})
assert await state2.get_data() == {}
await state.update_data({"key": "value"})
assert await state.get_data() == {"foo": "bar", "key": "value"}
await state.clear()
assert await state.get_state() is None
assert await state.get_data() == {}
assert await state2.get_state() == "experiments"