2020-11-09 00:34:51 +03:00
|
|
|
import aioredis.util
|
2020-02-12 20:07:14 +02:00
|
|
|
import pytest
|
|
|
|
|
from _pytest.config import UsageError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2020-11-08 21:49:34 +00:00
|
|
|
parser.addoption(
|
|
|
|
|
"--redis", default=None, help="run tests which require redis connection"
|
|
|
|
|
)
|
2020-02-12 20:07:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
2020-11-08 21:49:34 +00: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:
|
2020-11-08 21:49:34 +00: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
|
|
|
|
|
try:
|
|
|
|
|
address, options = aioredis.util.parse_url(redis_uri)
|
2020-11-08 21:48:49 +00:00
|
|
|
if not isinstance(address, tuple):
|
2020-11-08 21:49:34 +00:00
|
|
|
raise AssertionError(
|
|
|
|
|
"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}")
|
|
|
|
|
|
|
|
|
|
|
2020-11-08 21:49:34 +00:00
|
|
|
@pytest.fixture(scope="session")
|
2020-02-12 20:07:14 +02:00
|
|
|
def redis_options(request):
|
|
|
|
|
redis_uri = request.config.getoption("--redis")
|
|
|
|
|
(host, port), options = aioredis.util.parse_url(redis_uri)
|
2020-11-08 21:49:34 +00:00
|
|
|
options.update({"host": host, "port": port})
|
2020-02-12 20:07:14 +02:00
|
|
|
return options
|