Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test keys refactoring #572

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions tests/acceptance/conftest.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import pytest
from tests.utils import Keys

from aiocache import Cache, caches


def pytest_configure():
"""
Before pytest_namespace was being used to set the keys for
testing but the feature was removed
https://docs.pytest.org/en/latest/deprecations.html#pytest-namespace
"""
pytest.KEY = "key"
pytest.KEY_1 = "random"


@pytest.fixture(autouse=True)
def reset_caches():
caches._caches = {}
Expand All @@ -31,9 +22,8 @@ def redis_cache(event_loop):
cache = Cache(Cache.REDIS, namespace="test")
yield cache

event_loop.run_until_complete(cache.delete(pytest.KEY))
event_loop.run_until_complete(cache.delete(pytest.KEY_1))
event_loop.run_until_complete(cache.delete(pytest.KEY + "-lock"))
for key in Keys:
event_loop.run_until_complete(cache.delete(key))
event_loop.run_until_complete(cache.close())


Expand All @@ -42,9 +32,8 @@ def memory_cache(event_loop):
cache = Cache(namespace="test")
yield cache

event_loop.run_until_complete(cache.delete(pytest.KEY))
event_loop.run_until_complete(cache.delete(pytest.KEY_1))
event_loop.run_until_complete(cache.delete(pytest.KEY + "-lock"))
for key in Keys:
event_loop.run_until_complete(cache.delete(key))
event_loop.run_until_complete(cache.close())


Expand All @@ -53,9 +42,8 @@ def memcached_cache(event_loop):
cache = Cache(Cache.MEMCACHED, namespace="test")
yield cache

event_loop.run_until_complete(cache.delete(pytest.KEY))
event_loop.run_until_complete(cache.delete(pytest.KEY_1))
event_loop.run_until_complete(cache.delete(pytest.KEY + "-lock"))
for key in Keys:
event_loop.run_until_complete(cache.delete(key))
event_loop.run_until_complete(cache.close())


Expand Down
135 changes: 68 additions & 67 deletions tests/acceptance/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

import pytest
from tests.utils import Keys

from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.memory import SimpleMemoryCache
Expand All @@ -21,142 +22,142 @@ async def test_setup(self, cache):

@pytest.mark.asyncio
async def test_get_missing(self, cache):
assert await cache.get(pytest.KEY) is None
assert await cache.get(pytest.KEY, default=1) == 1
assert await cache.get(Keys.KEY) is None
assert await cache.get(Keys.KEY, default=1) == 1

@pytest.mark.asyncio
async def test_get_existing(self, cache):
await cache.set(pytest.KEY, "value")
assert await cache.get(pytest.KEY) == "value"
await cache.set(Keys.KEY, "value")
assert await cache.get(Keys.KEY) == "value"

@pytest.mark.asyncio
async def test_multi_get(self, cache):
await cache.set(pytest.KEY, "value")
assert await cache.multi_get([pytest.KEY, pytest.KEY_1]) == ["value", None]
await cache.set(Keys.KEY, "value")
assert await cache.multi_get([Keys.KEY, Keys.KEY_1]) == ["value", None]

@pytest.mark.asyncio
async def test_delete_missing(self, cache):
assert await cache.delete(pytest.KEY) == 0
assert await cache.delete(Keys.KEY) == 0

@pytest.mark.asyncio
async def test_delete_existing(self, cache):
await cache.set(pytest.KEY, "value")
assert await cache.delete(pytest.KEY) == 1
await cache.set(Keys.KEY, "value")
assert await cache.delete(Keys.KEY) == 1

assert await cache.get(pytest.KEY) is None
assert await cache.get(Keys.KEY) is None

@pytest.mark.asyncio
async def test_set(self, cache):
assert await cache.set(pytest.KEY, "value") is True
assert await cache.set(Keys.KEY, "value") is True

@pytest.mark.asyncio
async def test_set_cancel_previous_ttl_handle(self, cache):
await cache.set(pytest.KEY, "value", ttl=2)
await cache.set(Keys.KEY, "value", ttl=2)

await asyncio.sleep(1)
assert await cache.get(pytest.KEY) == "value"
await cache.set(pytest.KEY, "new_value", ttl=2)
assert await cache.get(Keys.KEY) == "value"
await cache.set(Keys.KEY, "new_value", ttl=2)

await asyncio.sleep(1)
assert await cache.get(pytest.KEY) == "new_value"
assert await cache.get(Keys.KEY) == "new_value"

@pytest.mark.asyncio
async def test_multi_set(self, cache):
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
assert await cache.multi_set(pairs) is True
assert await cache.multi_get([pytest.KEY, pytest.KEY_1]) == ["value", "random_value"]
assert await cache.multi_get([Keys.KEY, Keys.KEY_1]) == ["value", "random_value"]

@pytest.mark.asyncio
async def test_multi_set_with_ttl(self, cache):
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
assert await cache.multi_set(pairs, ttl=1) is True
await asyncio.sleep(1.1)

assert await cache.multi_get([pytest.KEY, pytest.KEY_1]) == [None, None]
assert await cache.multi_get([Keys.KEY, Keys.KEY_1]) == [None, None]

@pytest.mark.asyncio
async def test_set_with_ttl(self, cache):
await cache.set(pytest.KEY, "value", ttl=1)
await cache.set(Keys.KEY, "value", ttl=1)
await asyncio.sleep(1.1)

assert await cache.get(pytest.KEY) is None
assert await cache.get(Keys.KEY) is None

@pytest.mark.asyncio
async def test_add_missing(self, cache):
assert await cache.add(pytest.KEY, "value", ttl=1) is True
assert await cache.add(Keys.KEY, "value", ttl=1) is True

@pytest.mark.asyncio
async def test_add_existing(self, cache):
assert await cache.set(pytest.KEY, "value") is True
assert await cache.set(Keys.KEY, "value") is True
with pytest.raises(ValueError):
await cache.add(pytest.KEY, "value")
await cache.add(Keys.KEY, "value")

@pytest.mark.asyncio
async def test_exists_missing(self, cache):
assert await cache.exists(pytest.KEY) is False
assert await cache.exists(Keys.KEY) is False

@pytest.mark.asyncio
async def test_exists_existing(self, cache):
await cache.set(pytest.KEY, "value")
assert await cache.exists(pytest.KEY) is True
await cache.set(Keys.KEY, "value")
assert await cache.exists(Keys.KEY) is True

@pytest.mark.asyncio
async def test_increment_missing(self, cache):
assert await cache.increment(pytest.KEY, delta=2) == 2
assert await cache.increment(pytest.KEY_1, delta=-2) == -2
assert await cache.increment(Keys.KEY, delta=2) == 2
assert await cache.increment(Keys.KEY_1, delta=-2) == -2

@pytest.mark.asyncio
async def test_increment_existing(self, cache):
await cache.set(pytest.KEY, 2)
assert await cache.increment(pytest.KEY, delta=2) == 4
assert await cache.increment(pytest.KEY, delta=1) == 5
assert await cache.increment(pytest.KEY, delta=-3) == 2
await cache.set(Keys.KEY, 2)
assert await cache.increment(Keys.KEY, delta=2) == 4
assert await cache.increment(Keys.KEY, delta=1) == 5
assert await cache.increment(Keys.KEY, delta=-3) == 2

@pytest.mark.asyncio
async def test_increment_typeerror(self, cache):
await cache.set(pytest.KEY, "value")
await cache.set(Keys.KEY, "value")
with pytest.raises(TypeError):
assert await cache.increment(pytest.KEY)
assert await cache.increment(Keys.KEY)

@pytest.mark.asyncio
async def test_expire_existing(self, cache):
await cache.set(pytest.KEY, "value")
assert await cache.expire(pytest.KEY, 1) is True
await cache.set(Keys.KEY, "value")
assert await cache.expire(Keys.KEY, 1) is True
await asyncio.sleep(1.1)
assert await cache.exists(pytest.KEY) is False
assert await cache.exists(Keys.KEY) is False

@pytest.mark.asyncio
async def test_expire_with_0(self, cache):
await cache.set(pytest.KEY, "value", 1)
assert await cache.expire(pytest.KEY, 0) is True
await cache.set(Keys.KEY, "value", 1)
assert await cache.expire(Keys.KEY, 0) is True
await asyncio.sleep(1.1)
assert await cache.exists(pytest.KEY) is True
assert await cache.exists(Keys.KEY) is True

@pytest.mark.asyncio
async def test_expire_missing(self, cache):
assert await cache.expire(pytest.KEY, 1) is False
assert await cache.expire(Keys.KEY, 1) is False

@pytest.mark.asyncio
async def test_clear(self, cache):
await cache.set(pytest.KEY, "value")
await cache.set(Keys.KEY, "value")
await cache.clear()

assert await cache.exists(pytest.KEY) is False
assert await cache.exists(Keys.KEY) is False

@pytest.mark.asyncio
async def test_close_pool_only_clears_resources(self, cache):
await cache.set(pytest.KEY, "value")
await cache.set(Keys.KEY, "value")
await cache.close()
assert await cache.set(pytest.KEY, "value") is True
assert await cache.get(pytest.KEY) == "value"
assert await cache.set(Keys.KEY, "value") is True
assert await cache.get(Keys.KEY) == "value"

@pytest.mark.asyncio
async def test_single_connection(self, cache):
pytest.skip("aioredis code is broken")
async with cache.get_connection() as conn:
assert isinstance(conn, _Conn)
assert await conn.set(pytest.KEY, "value") is True
assert await conn.get(pytest.KEY) == "value"
assert await conn.set(Keys.KEY, "value") is True
assert await conn.get(Keys.KEY) == "value"


class TestMemoryCache:
Expand All @@ -167,18 +168,18 @@ async def test_accept_explicit_args(self):

@pytest.mark.asyncio
async def test_set_float_ttl(self, memory_cache):
await memory_cache.set(pytest.KEY, "value", ttl=0.1)
await memory_cache.set(Keys.KEY, "value", ttl=0.1)
await asyncio.sleep(0.15)

assert await memory_cache.get(pytest.KEY) is None
assert await memory_cache.get(Keys.KEY) is None

@pytest.mark.asyncio
async def test_multi_set_float_ttl(self, memory_cache):
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
assert await memory_cache.multi_set(pairs, ttl=0.1) is True
await asyncio.sleep(0.15)

assert await memory_cache.multi_get([pytest.KEY, pytest.KEY_1]) == [None, None]
assert await memory_cache.multi_get([Keys.KEY, Keys.KEY_1]) == [None, None]

@pytest.mark.asyncio
async def test_raw(self, memory_cache):
Expand All @@ -188,10 +189,10 @@ async def test_raw(self, memory_cache):

@pytest.mark.asyncio
async def test_clear_with_namespace_memory(self, memory_cache):
await memory_cache.set(pytest.KEY, "value", namespace="test")
await memory_cache.set(Keys.KEY, "value", namespace="test")
await memory_cache.clear(namespace="test")

assert await memory_cache.exists(pytest.KEY, namespace="test") is False
assert await memory_cache.exists(Keys.KEY, namespace="test") is False


class TestMemcachedCache:
Expand All @@ -209,13 +210,13 @@ async def test_set_too_long_key(self, memcached_cache):
@pytest.mark.asyncio
async def test_set_float_ttl_fails(self, memcached_cache):
with pytest.raises(TypeError) as exc_info:
await memcached_cache.set(pytest.KEY, "value", ttl=0.1)
await memcached_cache.set(Keys.KEY, "value", ttl=0.1)
assert str(exc_info.value) == "aiomcache error: exptime not int: 0.1"

@pytest.mark.asyncio
async def test_multi_set_float_ttl(self, memcached_cache):
with pytest.raises(TypeError) as exc_info:
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
assert await memcached_cache.multi_set(pairs, ttl=0.1) is True
assert str(exc_info.value) == "aiomcache error: exptime not int: 0.1"

Expand All @@ -228,16 +229,16 @@ async def test_raw(self, memcached_cache):

@pytest.mark.asyncio
async def test_clear_with_namespace_memcached(self, memcached_cache):
await memcached_cache.set(pytest.KEY, "value", namespace="test")
await memcached_cache.set(Keys.KEY, "value", namespace="test")

with pytest.raises(ValueError):
await memcached_cache.clear(namespace="test")

assert await memcached_cache.exists(pytest.KEY, namespace="test") is True
assert await memcached_cache.exists(Keys.KEY, namespace="test") is True

@pytest.mark.asyncio
async def test_close(self, memcached_cache):
await memcached_cache.set(pytest.KEY, "value")
await memcached_cache.set(Keys.KEY, "value")
await memcached_cache._close()
assert memcached_cache.client._pool._pool.qsize() == 0

Expand All @@ -250,18 +251,18 @@ async def test_accept_explicit_args(self):

@pytest.mark.asyncio
async def test_float_ttl(self, redis_cache):
await redis_cache.set(pytest.KEY, "value", ttl=0.1)
await redis_cache.set(Keys.KEY, "value", ttl=0.1)
await asyncio.sleep(0.15)

assert await redis_cache.get(pytest.KEY) is None
assert await redis_cache.get(Keys.KEY) is None

@pytest.mark.asyncio
async def test_multi_set_float_ttl(self, redis_cache):
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
assert await redis_cache.multi_set(pairs, ttl=0.1) is True
await asyncio.sleep(0.15)

assert await redis_cache.multi_get([pytest.KEY, pytest.KEY_1]) == [None, None]
assert await redis_cache.multi_get([Keys.KEY, Keys.KEY_1]) == [None, None]

@pytest.mark.asyncio
async def test_raw(self, redis_cache):
Expand All @@ -271,13 +272,13 @@ async def test_raw(self, redis_cache):

@pytest.mark.asyncio
async def test_clear_with_namespace_redis(self, redis_cache):
await redis_cache.set(pytest.KEY, "value", namespace="test")
await redis_cache.set(Keys.KEY, "value", namespace="test")
await redis_cache.clear(namespace="test")

assert await redis_cache.exists(pytest.KEY, namespace="test") is False
assert await redis_cache.exists(Keys.KEY, namespace="test") is False

@pytest.mark.asyncio
async def test_close(self, redis_cache):
await redis_cache.set(pytest.KEY, "value")
await redis_cache.set(Keys.KEY, "value")
await redis_cache._close()
assert redis_cache._pool.size == 0
Loading