Clean up test keys properly

This commit is contained in:
Andrew Brookins 2021-11-12 07:56:47 -08:00
parent 8107933c1d
commit 58a798444d
3 changed files with 19 additions and 10 deletions

View file

@ -57,12 +57,12 @@ lint: $(INSTALL_STAMP) dist
$(POETRY) run twine check dist/* $(POETRY) run twine check dist/*
.PHONY: format .PHONY: format
format: $(INSTALL_STAMP) sync redis format: $(INSTALL_STAMP) sync
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME) $(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run black ./tests/ $(NAME) $(SYNC_NAME) $(POETRY) run black ./tests/ $(NAME) $(SYNC_NAME)
.PHONY: test .PHONY: test
test: $(INSTALL_STAMP) sync test: $(INSTALL_STAMP) sync redis
REDIS_OM_URL="$(REDIS_OM_URL)" $(POETRY) run pytest -n auto -vv ./tests/ ./tests_sync/ --cov-report term-missing --cov $(NAME) $(SYNC_NAME) REDIS_OM_URL="$(REDIS_OM_URL)" $(POETRY) run pytest -n auto -vv ./tests/ ./tests_sync/ --cov-report term-missing --cov $(NAME) $(SYNC_NAME)
.PHONY: test_oss .PHONY: test_oss

View file

@ -6,6 +6,9 @@ import pytest
from aredis_om import get_redis_connection from aredis_om import get_redis_connection
TEST_PREFIX = "redis-om:testing"
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def event_loop(request): def event_loop(request):
""" """
@ -22,20 +25,26 @@ def redis():
yield get_redis_connection() yield get_redis_connection()
async def _delete_test_keys(prefix: str, conn): def _delete_test_keys(prefix: str, conn):
keys = [] keys = []
async for key in conn.scan_iter(f"{prefix}:*"): for key in conn.scan_iter(f"{prefix}:*"):
keys.append(key) keys.append(key)
if keys: if keys:
conn.delete(*keys) conn.delete(*keys)
@pytest.fixture @pytest.fixture
def key_prefix(redis): def key_prefix(request, redis):
key_prefix = f"redis-om:{random.random()}" key_prefix = f"{TEST_PREFIX}:{random.random()}"
yield key_prefix yield key_prefix
@pytest.fixture(autouse=True) @pytest.fixture(scope="session", autouse=True)
async def delete_test_keys(redis, request, key_prefix): def cleanup_keys(request):
await _delete_test_keys(key_prefix, redis) 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)

View file

@ -432,7 +432,7 @@ async def test_access_result_by_index_not_cached(members, m):
assert await query.get_item(2) == member3 assert await query.get_item(2) == member3
def test_schema(m, key_prefix): def test_schema(m):
class Address(m.BaseHashModel): class Address(m.BaseHashModel):
a_string: str = Field(index=True) a_string: str = Field(index=True)
a_full_text_string: str = Field(index=True, full_text_search=True) a_full_text_string: str = Field(index=True, full_text_search=True)