2022-02-08 03:30:41 +03:00
|
|
|
import asyncio
|
2022-09-18 15:32:42 +03:00
|
|
|
import pytest_asyncio
|
2022-02-08 03:30:41 +03:00
|
|
|
|
2021-09-05 23:46:50 +03:00
|
|
|
import aioredis
|
2020-02-12 20:07:14 +02:00
|
|
|
import pytest
|
|
|
|
|
from _pytest.config import UsageError
|
2021-09-05 23:46:50 +03:00
|
|
|
|
2022-02-08 03:30:41 +03:00
|
|
|
from aiogram import Bot
|
|
|
|
|
from . import TOKEN
|
|
|
|
|
|
2021-09-05 23:46:50 +03:00
|
|
|
try:
|
|
|
|
|
import aioredis.util
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
2020-02-12 20:07:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2021-09-05 23:46:50 +03:00
|
|
|
parser.addoption(
|
|
|
|
|
"--redis",
|
|
|
|
|
default=None,
|
|
|
|
|
help="run tests which require redis connection",
|
|
|
|
|
)
|
2022-12-31 02:48:53 +06:00
|
|
|
parser.addini("asyncio_mode", "", default='auto')
|
2020-02-12 20:07:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
2021-09-05 23:46:50 +03:00
|
|
|
config.addinivalue_line(
|
|
|
|
|
"markers",
|
|
|
|
|
"redis: marked tests require redis connection to run",
|
|
|
|
|
)
|
2020-02-12 20:07:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
|
|
|
|
redis_uri = config.getoption("--redis")
|
|
|
|
|
if redis_uri is None:
|
2021-09-05 23:46:50 +03:00
|
|
|
skip_redis = pytest.mark.skip(
|
|
|
|
|
reason="need --redis option with redis URI to run"
|
|
|
|
|
)
|
2020-02-12 20:07:14 +02:00
|
|
|
for item in items:
|
|
|
|
|
if "redis" in item.keywords:
|
|
|
|
|
item.add_marker(skip_redis)
|
|
|
|
|
return
|
2021-09-05 23:46:50 +03:00
|
|
|
|
|
|
|
|
redis_version = int(aioredis.__version__.split(".")[0])
|
|
|
|
|
options = None
|
|
|
|
|
if redis_version == 1:
|
|
|
|
|
(host, port), options = aioredis.util.parse_url(redis_uri)
|
|
|
|
|
options.update({'host': host, 'port': port})
|
|
|
|
|
elif redis_version == 2:
|
|
|
|
|
try:
|
|
|
|
|
options = aioredis.connection.parse_url(redis_uri)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
raise UsageError(f"Invalid redis URI {redis_uri!r}: {e}")
|
|
|
|
|
|
2020-02-12 20:07:14 +02:00
|
|
|
try:
|
2021-09-05 23:46:50 +03:00
|
|
|
assert isinstance(options, dict), \
|
|
|
|
|
"Only redis and rediss schemas are supported, eg redis://foo."
|
2020-02-12 20:07:14 +02:00
|
|
|
except AssertionError as e:
|
|
|
|
|
raise UsageError(f"Invalid redis URI {redis_uri!r}: {e}")
|
|
|
|
|
|
|
|
|
|
|
2022-09-18 15:32:42 +03:00
|
|
|
@pytest_asyncio.fixture(scope='session')
|
2020-02-12 20:07:14 +02:00
|
|
|
def redis_options(request):
|
|
|
|
|
redis_uri = request.config.getoption("--redis")
|
2021-09-05 23:46:50 +03:00
|
|
|
if redis_uri is None:
|
|
|
|
|
pytest.skip("need --redis option with redis URI to run")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
redis_version = int(aioredis.__version__.split(".")[0])
|
|
|
|
|
if redis_version == 1:
|
|
|
|
|
(host, port), options = aioredis.util.parse_url(redis_uri)
|
|
|
|
|
options.update({'host': host, 'port': port})
|
|
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
if redis_version == 2:
|
|
|
|
|
try:
|
|
|
|
|
return aioredis.connection.parse_url(redis_uri)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
raise UsageError(f"Invalid redis URI {redis_uri!r}: {e}")
|
|
|
|
|
|
|
|
|
|
raise UsageError("Unsupported aioredis version")
|
2022-02-08 03:30:41 +03:00
|
|
|
|
|
|
|
|
|
2022-09-18 15:32:42 +03:00
|
|
|
@pytest_asyncio.fixture(name='bot')
|
2022-02-08 03:30:41 +03:00
|
|
|
async def bot_fixture():
|
|
|
|
|
"""Bot fixture."""
|
|
|
|
|
bot = Bot(TOKEN)
|
|
|
|
|
yield bot
|
|
|
|
|
session = await bot.get_session()
|
|
|
|
|
if session and not session.closed:
|
|
|
|
|
await session.close()
|
|
|
|
|
await asyncio.sleep(0.2)
|