From 5a9a4c888ee38161bfd48275c68169706c151493 Mon Sep 17 00:00:00 2001 From: Bachynin Ivan Date: Wed, 12 Feb 2020 20:07:14 +0200 Subject: [PATCH] add simple tests for RedisStorage2 --- .gitignore | 3 +++ tests/conftest.py | 36 ++++++++++++++++++++++++- tests/contrib/fsm_storage/test_redis.py | 33 +++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/contrib/fsm_storage/test_redis.py diff --git a/.gitignore b/.gitignore index a8b34bd1..d20c39ba 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,6 @@ docs/html # i18n/l10n *.mo + +# pynev +.python-version diff --git a/tests/conftest.py b/tests/conftest.py index fe936e18..03c8dbe4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,35 @@ -# pytest_plugins = "pytest_asyncio.plugin" +import pytest +from _pytest.config import UsageError +import aioredis.util + + +def pytest_addoption(parser): + parser.addoption("--redis", default=None, + help="run tests which require redis connection") + + +def pytest_configure(config): + config.addinivalue_line("markers", "redis: marked tests require redis connection to run") + + +def pytest_collection_modifyitems(config, items): + redis_uri = config.getoption("--redis") + if redis_uri is None: + skip_redis = pytest.mark.skip(reason="need --redis option with redis URI to run") + for item in items: + if "redis" in item.keywords: + item.add_marker(skip_redis) + return + try: + address, options = aioredis.util.parse_url(redis_uri) + assert isinstance(address, tuple), "Only redis and rediss schemas are supported, eg redis://foo." + except AssertionError as e: + raise UsageError(f"Invalid redis URI {redis_uri!r}: {e}") + + +@pytest.fixture(scope='session') +def redis_options(request): + redis_uri = request.config.getoption("--redis") + (host, port), options = aioredis.util.parse_url(redis_uri) + options.update({'host': host, 'port': port}) + return options diff --git a/tests/contrib/fsm_storage/test_redis.py b/tests/contrib/fsm_storage/test_redis.py new file mode 100644 index 00000000..527c905e --- /dev/null +++ b/tests/contrib/fsm_storage/test_redis.py @@ -0,0 +1,33 @@ +import pytest + +from aiogram.contrib.fsm_storage.redis import RedisStorage2 + + +@pytest.fixture() +async def store(redis_options): + s = RedisStorage2(**redis_options) + try: + yield s + finally: + conn = await s.redis() + await conn.flushdb() + await s.close() + await s.wait_closed() + + +@pytest.mark.redis +class TestRedisStorage2: + @pytest.mark.asyncio + async def test_set_get(self, store): + assert await store.get_data(chat='1234') == {} + await store.set_data(chat='1234', data={'foo': 'bar'}) + assert await store.get_data(chat='1234') == {'foo': 'bar'} + + @pytest.mark.asyncio + async def test_close_and_open_connection(self, store): + await store.set_data(chat='1234', data={'foo': 'bar'}) + assert await store.get_data(chat='1234') == {'foo': 'bar'} + pool_id = id(store._redis) + await store.close() + assert await store.get_data(chat='1234') == {'foo': 'bar'} # new pool was opened at this point + assert id(store._redis) != pool_id