remove key from storage, when not needed

This commit is contained in:
Andrey Tikhonov 2021-05-17 20:46:53 +03:00
parent 0e28756a10
commit 8ea3546cda
4 changed files with 68 additions and 17 deletions

View file

@ -1,10 +1,25 @@
import pytest
from aiogram.contrib.fsm_storage.redis import RedisStorage2
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.contrib.fsm_storage.redis import RedisStorage2, RedisStorage
@pytest.fixture()
async def store(redis_options):
@pytest.mark.redis
async def redis_store(redis_options):
s = RedisStorage(**redis_options)
try:
yield s
finally:
conn = await s.redis()
await conn.execute('FLUSHDB')
await s.close()
await s.wait_closed()
@pytest.fixture()
@pytest.mark.redis
async def redis_store2(redis_options):
s = RedisStorage2(**redis_options)
try:
yield s
@ -15,8 +30,19 @@ async def store(redis_options):
await s.wait_closed()
@pytest.mark.redis
class TestRedisStorage2:
@pytest.fixture()
async def memory_store():
yield MemoryStorage()
@pytest.mark.parametrize(
"store", [
pytest.lazy_fixture('redis_store'),
pytest.lazy_fixture('redis_store2'),
pytest.lazy_fixture('memory_store')
]
)
class TestStorage:
@pytest.mark.asyncio
async def test_set_get(self, store):
assert await store.get_data(chat='1234') == {}
@ -24,10 +50,17 @@ class TestRedisStorage2:
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
async def test_reset(self, store):
await store.reset_data(chat='1234')
assert await store.get_data(chat='1234') == {}
class TestRedisStorage2:
@pytest.mark.asyncio
async def test_close_and_open_connection(self, redis_store):
await redis_store.set_data(chat='1234', data={'foo': 'bar'})
assert await redis_store.get_data(chat='1234') == {'foo': 'bar'}
pool_id = id(redis_store._redis)
await redis_store.close()
assert await redis_store.get_data(chat='1234') == {'foo': 'bar'} # new pool was opened at this point
assert id(redis_store._redis) != pool_id