Skip to content

Commit 58a7984

Browse files
author
Andrew Brookins
committed
Clean up test keys properly
1 parent 8107933 commit 58a7984

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ lint: $(INSTALL_STAMP) dist
5757
$(POETRY) run twine check dist/*
5858

5959
.PHONY: format
60-
format: $(INSTALL_STAMP) sync redis
60+
format: $(INSTALL_STAMP) sync
6161
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
6262
$(POETRY) run black ./tests/ $(NAME) $(SYNC_NAME)
6363

6464
.PHONY: test
65-
test: $(INSTALL_STAMP) sync
65+
test: $(INSTALL_STAMP) sync redis
6666
REDIS_OM_URL="$(REDIS_OM_URL)" $(POETRY) run pytest -n auto -vv ./tests/ ./tests_sync/ --cov-report term-missing --cov $(NAME) $(SYNC_NAME)
6767

6868
.PHONY: test_oss

tests/conftest.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from aredis_om import get_redis_connection
77

88

9+
TEST_PREFIX = "redis-om:testing"
10+
11+
912
@pytest.fixture(scope="session")
1013
def event_loop(request):
1114
"""
@@ -22,20 +25,26 @@ def redis():
2225
yield get_redis_connection()
2326

2427

25-
async def _delete_test_keys(prefix: str, conn):
28+
def _delete_test_keys(prefix: str, conn):
2629
keys = []
27-
async for key in conn.scan_iter(f"{prefix}:*"):
30+
for key in conn.scan_iter(f"{prefix}:*"):
2831
keys.append(key)
2932
if keys:
3033
conn.delete(*keys)
3134

3235

3336
@pytest.fixture
34-
def key_prefix(redis):
35-
key_prefix = f"redis-om:{random.random()}"
37+
def key_prefix(request, redis):
38+
key_prefix = f"{TEST_PREFIX}:{random.random()}"
3639
yield key_prefix
3740

3841

39-
@pytest.fixture(autouse=True)
40-
async def delete_test_keys(redis, request, key_prefix):
41-
await _delete_test_keys(key_prefix, redis)
42+
@pytest.fixture(scope="session", autouse=True)
43+
def cleanup_keys(request):
44+
def cleanup_keys():
45+
# Always use the sync Redis connection with finalizer. Setting up an
46+
# async finalizer should work, but I'm not suer how yet!
47+
from redis_om.connections import get_redis_connection as get_sync_redis
48+
_delete_test_keys(TEST_PREFIX, get_sync_redis())
49+
50+
request.addfinalizer(cleanup_keys)

tests/test_hash_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ async def test_access_result_by_index_not_cached(members, m):
432432
assert await query.get_item(2) == member3
433433

434434

435-
def test_schema(m, key_prefix):
435+
def test_schema(m):
436436
class Address(m.BaseHashModel):
437437
a_string: str = Field(index=True)
438438
a_full_text_string: str = Field(index=True, full_text_search=True)

0 commit comments

Comments
 (0)