2021-09-22 00:52:38 +03:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2019-11-18 17:44:07 +02:00
|
|
|
import pytest
|
2021-06-15 01:45:31 +03:00
|
|
|
from _pytest.config import UsageError
|
2024-05-07 22:42:31 +03:00
|
|
|
from pymongo.errors import InvalidURI, PyMongoError
|
|
|
|
|
from pymongo.uri_parser import parse_uri as parse_mongo_url
|
2022-04-16 19:07:32 +03:00
|
|
|
from redis.asyncio.connection import parse_url as parse_redis_url
|
2019-11-18 17:44:07 +02:00
|
|
|
|
2023-07-28 22:23:32 +03:00
|
|
|
from aiogram import Dispatcher
|
2023-01-12 02:49:58 +02:00
|
|
|
from aiogram.fsm.storage.memory import (
|
|
|
|
|
DisabledEventIsolation,
|
|
|
|
|
MemoryStorage,
|
|
|
|
|
SimpleEventIsolation,
|
|
|
|
|
)
|
2024-05-07 22:42:31 +03:00
|
|
|
from aiogram.fsm.storage.mongo import MongoStorage
|
2022-08-14 01:07:52 +03:00
|
|
|
from aiogram.fsm.storage.redis import RedisEventIsolation, RedisStorage
|
2019-11-18 17:44:07 +02:00
|
|
|
from tests.mocked_bot import MockedBot
|
|
|
|
|
|
2021-09-22 00:52:38 +03:00
|
|
|
DATA_DIR = Path(__file__).parent / "data"
|
|
|
|
|
|
2019-11-18 17:44:07 +02:00
|
|
|
|
2021-06-15 01:45:31 +03:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
|
parser.addoption("--redis", default=None, help="run tests which require redis connection")
|
2024-05-07 22:42:31 +03:00
|
|
|
parser.addoption("--mongo", default=None, help="run tests which require mongo connection")
|
2021-06-15 01:45:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
|
config.addinivalue_line("markers", "redis: marked tests require redis connection to run")
|
2024-05-07 22:42:31 +03:00
|
|
|
config.addinivalue_line("markers", "mongo: marked tests require mongo connection to run")
|
2021-06-15 01:45:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
2024-05-07 22:42:31 +03:00
|
|
|
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}")
|
2021-06-15 01:45:31 +03:00
|
|
|
|
|
|
|
|
|
2023-11-16 02:08:36 +02:00
|
|
|
@pytest.fixture()
|
2021-06-15 01:45:31 +03:00
|
|
|
def redis_server(request):
|
|
|
|
|
redis_uri = request.config.getoption("--redis")
|
|
|
|
|
return redis_uri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
@pytest.mark.redis
|
|
|
|
|
async def redis_storage(redis_server):
|
|
|
|
|
if not redis_server:
|
|
|
|
|
pytest.skip("Redis is not available here")
|
|
|
|
|
storage = RedisStorage.from_url(redis_server)
|
2021-07-29 00:40:50 +03:00
|
|
|
try:
|
|
|
|
|
await storage.redis.info()
|
|
|
|
|
except ConnectionError as e:
|
|
|
|
|
pytest.skip(str(e))
|
2021-06-15 01:45:31 +03:00
|
|
|
try:
|
|
|
|
|
yield storage
|
|
|
|
|
finally:
|
|
|
|
|
conn = await storage.redis
|
|
|
|
|
await conn.flushdb()
|
|
|
|
|
await storage.close()
|
|
|
|
|
|
|
|
|
|
|
2024-05-07 22:42:31 +03:00
|
|
|
@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._database)
|
|
|
|
|
finally:
|
|
|
|
|
await storage.close()
|
|
|
|
|
|
|
|
|
|
|
2021-06-15 01:45:31 +03:00
|
|
|
@pytest.fixture()
|
|
|
|
|
async def memory_storage():
|
|
|
|
|
storage = MemoryStorage()
|
|
|
|
|
try:
|
|
|
|
|
yield storage
|
|
|
|
|
finally:
|
|
|
|
|
await storage.close()
|
|
|
|
|
|
|
|
|
|
|
2022-02-19 01:45:59 +02:00
|
|
|
@pytest.fixture()
|
|
|
|
|
@pytest.mark.redis
|
2023-11-16 02:08:36 +02:00
|
|
|
async def redis_isolation(redis_storage):
|
|
|
|
|
isolation = redis_storage.create_isolation()
|
|
|
|
|
return isolation
|
2022-02-19 01:45:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
async def lock_isolation():
|
|
|
|
|
isolation = SimpleEventIsolation()
|
|
|
|
|
try:
|
|
|
|
|
yield isolation
|
|
|
|
|
finally:
|
|
|
|
|
await isolation.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
async def disabled_isolation():
|
|
|
|
|
isolation = DisabledEventIsolation()
|
|
|
|
|
try:
|
|
|
|
|
yield isolation
|
|
|
|
|
finally:
|
|
|
|
|
await isolation.close()
|
|
|
|
|
|
|
|
|
|
|
2019-11-18 17:44:07 +02:00
|
|
|
@pytest.fixture()
|
|
|
|
|
def bot():
|
2023-07-28 22:23:32 +03:00
|
|
|
return MockedBot()
|
2022-02-19 01:45:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
async def dispatcher():
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
await dp.emit_startup()
|
|
|
|
|
try:
|
|
|
|
|
yield dp
|
|
|
|
|
finally:
|
|
|
|
|
await dp.emit_shutdown()
|