2023-11-16 02:08:36 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
|
|
2022-02-19 01:45:59 +02:00
|
|
|
import pytest
|
|
|
|
|
|
2022-08-14 01:07:52 +03:00
|
|
|
from aiogram.fsm.storage.base import BaseEventIsolation, StorageKey
|
2024-05-07 22:42:31 +03:00
|
|
|
from aiogram.fsm.storage.redis import RedisEventIsolation, RedisStorage
|
2022-02-19 01:45:59 +02:00
|
|
|
from tests.mocked_bot import MockedBot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="storage_key")
|
2023-04-08 18:01:11 +03:00
|
|
|
def create_storage_key(bot: MockedBot):
|
2022-02-19 01:45:59 +02:00
|
|
|
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"isolation",
|
|
|
|
|
[
|
|
|
|
|
pytest.lazy_fixture("redis_isolation"),
|
|
|
|
|
pytest.lazy_fixture("lock_isolation"),
|
|
|
|
|
pytest.lazy_fixture("disabled_isolation"),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
class TestIsolations:
|
|
|
|
|
async def test_lock(
|
|
|
|
|
self,
|
|
|
|
|
isolation: BaseEventIsolation,
|
|
|
|
|
storage_key: StorageKey,
|
|
|
|
|
):
|
2023-04-08 18:01:11 +03:00
|
|
|
async with isolation.lock(key=storage_key):
|
2023-10-01 15:28:54 +03:00
|
|
|
assert True, "Are you kidding me?"
|
2023-11-16 02:08:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRedisEventIsolation:
|
2024-05-07 22:42:31 +03:00
|
|
|
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
|
|
|
|
|
|
2023-11-16 02:08:36 +02:00
|
|
|
def test_init_without_key_builder(self):
|
|
|
|
|
redis = AsyncMock()
|
|
|
|
|
isolation = RedisEventIsolation(redis=redis)
|
|
|
|
|
assert isolation.redis is redis
|
|
|
|
|
|
|
|
|
|
assert isolation.key_builder is not None
|
|
|
|
|
|
|
|
|
|
def test_init_with_key_builder(self):
|
|
|
|
|
redis = AsyncMock()
|
|
|
|
|
key_builder = AsyncMock()
|
|
|
|
|
isolation = RedisEventIsolation(redis=redis, key_builder=key_builder)
|
|
|
|
|
assert isolation.redis is redis
|
|
|
|
|
assert isolation.key_builder is key_builder
|
|
|
|
|
|
|
|
|
|
def test_create_from_url(self):
|
|
|
|
|
with patch("redis.asyncio.connection.ConnectionPool.from_url") as pool:
|
|
|
|
|
isolation = RedisEventIsolation.from_url("redis://localhost:6379/0")
|
|
|
|
|
assert isinstance(isolation, RedisEventIsolation)
|
|
|
|
|
assert isolation.redis is not None
|
|
|
|
|
assert isolation.key_builder is not None
|
|
|
|
|
|
2023-11-18 21:28:34 +02:00
|
|
|
pool.assert_called_once_with("redis://localhost:6379/0")
|
2023-11-16 02:08:36 +02:00
|
|
|
|
|
|
|
|
async def test_close(self):
|
|
|
|
|
isolation = RedisEventIsolation(redis=AsyncMock())
|
|
|
|
|
await isolation.close()
|
|
|
|
|
|
|
|
|
|
# close is not called because connection should be closed from the storage
|
|
|
|
|
# assert isolation.redis.close.called_once()
|