Skip to content

Commit

Permalink
add test for cache blacklist and flushdb at the end of each test
Browse files Browse the repository at this point in the history
  • Loading branch information
dvora-h committed Jan 7, 2024
1 parent cea3458 commit 29f60c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_asyncio/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async def test_get_from_cache():
# get key from redis
assert await r.get("foo") == b"barbar"

await r.flushdb()
await r.aclose()


Expand All @@ -51,6 +52,7 @@ async def test_cache_max_size():
# the first key is not in the local cache anymore
assert cache.get(("GET", "foo")) is None

await r.flushdb()
await r.aclose()


Expand All @@ -69,6 +71,7 @@ async def test_cache_ttl():
# the key is not in the local cache anymore
assert cache.get(("GET", "foo")) is None

await r.flushdb()
await r.aclose()


Expand Down Expand Up @@ -96,6 +99,7 @@ async def test_cache_lfu_eviction():
assert cache.get(("GET", "foo")) == b"bar"
assert cache.get(("GET", "foo2")) is None

await r.flushdb()
await r.aclose()


Expand All @@ -117,4 +121,20 @@ async def test_cache_decode_response():
# get key from redis
assert await r.get("foo") == "barbar"

await r.flushdb()
await r.aclose()


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
async def test_cache_blacklist():
cache = _LocalCache()
r = redis.Redis(client_cache=cache, cache_blacklist=["LLEN"], protocol=3)
# add list to redis
await r.lpush("mylist", "foo", "bar", "baz")
assert await r.llen("mylist") == 3
assert await r.lindex("mylist", 1) == b"bar"
assert cache.get(("LLEN", "mylist")) is None
assert cache.get(("LINDEX", "mylist", 1)) == b"bar"

await r.flushdb()
await r.aclose()
24 changes: 24 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def test_get_from_cache():
# get key from redis
assert r.get("foo") == b"barbar"

r.flushdb()


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
def test_cache_max_size():
Expand All @@ -49,6 +51,8 @@ def test_cache_max_size():
# the first key is not in the local cache anymore
assert cache.get(("GET", "foo")) is None

r.flushdb()


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
def test_cache_ttl():
Expand All @@ -65,6 +69,8 @@ def test_cache_ttl():
# the key is not in the local cache anymore
assert cache.get(("GET", "foo")) is None

r.flushdb()


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
def test_cache_lfu_eviction():
Expand All @@ -90,6 +96,8 @@ def test_cache_lfu_eviction():
assert cache.get(("GET", "foo")) == b"bar"
assert cache.get(("GET", "foo2")) is None

r.flushdb()


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
def test_cache_decode_response():
Expand All @@ -108,3 +116,19 @@ def test_cache_decode_response():
assert cache.get(("GET", "foo")) is None
# get key from redis
assert r.get("foo") == "barbar"

r.flushdb()


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
def test_cache_blacklist():
cache = _LocalCache()
r = redis.Redis(client_cache=cache, cache_blacklist=["LLEN"], protocol=3)
# add list to redis
r.lpush("mylist", "foo", "bar", "baz")
assert r.llen("mylist") == 3
assert r.lindex("mylist", 1) == b"bar"
assert cache.get(("LLEN", "mylist")) is None
assert cache.get(("LINDEX", "mylist", 1)) == b"bar"

r.flushdb()

0 comments on commit 29f60c4

Please sign in to comment.