aiogram/tests/test_fsm/storage/test_storages.py

46 lines
1.7 KiB
Python
Raw Normal View History

import pytest
from aiogram.fsm.storage.base import BaseStorage, StorageKey
from tests.mocked_bot import MockedBot
2021-10-11 01:30:19 +03:00
@pytest.fixture(name="storage_key")
def create_storage_key(bot: MockedBot):
2021-10-11 01:30:19 +03:00
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
@pytest.mark.parametrize(
"storage",
[pytest.lazy_fixture("redis_storage"), pytest.lazy_fixture("memory_storage")],
)
class TestStorages:
2021-10-11 01:30:19 +03:00
async def test_set_state(self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_state(key=storage_key) is None
2021-10-11 01:30:19 +03:00
await storage.set_state(key=storage_key, state="state")
assert await storage.get_state(key=storage_key) == "state"
await storage.set_state(key=storage_key, state=None)
assert await storage.get_state(key=storage_key) is None
2021-10-11 01:30:19 +03:00
async def test_set_data(self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_data(key=storage_key) == {}
2021-10-11 01:30:19 +03:00
await storage.set_data(key=storage_key, data={"foo": "bar"})
assert await storage.get_data(key=storage_key) == {"foo": "bar"}
await storage.set_data(key=storage_key, data={})
assert await storage.get_data(key=storage_key) == {}
2021-10-11 01:30:19 +03:00
async def test_update_data(
self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey
):
assert await storage.get_data(key=storage_key) == {}
assert await storage.update_data(key=storage_key, data={"foo": "bar"}) == {"foo": "bar"}
assert await storage.update_data(key=storage_key, data={"baz": "spam"}) == {
2021-10-11 01:30:19 +03:00
"foo": "bar",
"baz": "spam",
}
assert await storage.get_data(key=storage_key) == {
2021-06-15 02:01:57 +03:00
"foo": "bar",
"baz": "spam",
}