Clean up test keys properly
This commit is contained in:
parent
8107933c1d
commit
58a798444d
3 changed files with 19 additions and 10 deletions
|
@ -6,6 +6,9 @@ import pytest
|
|||
from aredis_om import get_redis_connection
|
||||
|
||||
|
||||
TEST_PREFIX = "redis-om:testing"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def event_loop(request):
|
||||
"""
|
||||
|
@ -22,20 +25,26 @@ def redis():
|
|||
yield get_redis_connection()
|
||||
|
||||
|
||||
async def _delete_test_keys(prefix: str, conn):
|
||||
def _delete_test_keys(prefix: str, conn):
|
||||
keys = []
|
||||
async for key in conn.scan_iter(f"{prefix}:*"):
|
||||
for key in conn.scan_iter(f"{prefix}:*"):
|
||||
keys.append(key)
|
||||
if keys:
|
||||
conn.delete(*keys)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def key_prefix(redis):
|
||||
key_prefix = f"redis-om:{random.random()}"
|
||||
def key_prefix(request, redis):
|
||||
key_prefix = f"{TEST_PREFIX}:{random.random()}"
|
||||
yield key_prefix
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def delete_test_keys(redis, request, key_prefix):
|
||||
await _delete_test_keys(key_prefix, redis)
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def cleanup_keys(request):
|
||||
def cleanup_keys():
|
||||
# Always use the sync Redis connection with finalizer. Setting up an
|
||||
# async finalizer should work, but I'm not suer how yet!
|
||||
from redis_om.connections import get_redis_connection as get_sync_redis
|
||||
_delete_test_keys(TEST_PREFIX, get_sync_redis())
|
||||
|
||||
request.addfinalizer(cleanup_keys)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue