Skip to content

Commit

Permalink
feat: reset_redis/clean_redis fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jun 6, 2023
1 parent 64981db commit 2a8da13
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ckan/tests/pytest_ckan/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@

import ckan.plugins
import ckan.cli
import ckan.lib.search as search
import ckan.model as model
from ckan.common import config, aslist
from ckan.lib import redis, search


@register
Expand Down Expand Up @@ -214,6 +214,34 @@ def reset_index():
return search.clear_all


@pytest.fixture(scope="session")
def reset_redis():
"""Callable for removing all keys from Redis.
Accepts redis key-pattern for narrowing down the list of items to
remove. By default removes everything.
"""
def cleaner(pattern: str = "*") -> int:
"""Remove keys matching pattern.
Return number of removed records.
"""
conn = redis.connect_to_redis()
keys = conn.keys(pattern)
if keys:
return conn.delete(*keys)
return 0

return cleaner


@pytest.fixture()
def clean_redis(reset_redis):
"""Remove all keys from Redis.
"""
reset_redis()


@pytest.fixture
def clean_db(reset_db):
"""Resets the database to the initial state.
Expand Down

0 comments on commit 2a8da13

Please sign in to comment.