diff --git a/tests/acceptance/conftest.py b/tests/acceptance/conftest.py index 1965cbbc..27cf3e9e 100644 --- a/tests/acceptance/conftest.py +++ b/tests/acceptance/conftest.py @@ -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 = {} @@ -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()) @@ -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()) @@ -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()) diff --git a/tests/acceptance/test_base.py b/tests/acceptance/test_base.py index 45a22c96..624fdebe 100644 --- a/tests/acceptance/test_base.py +++ b/tests/acceptance/test_base.py @@ -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 @@ -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: @@ -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): @@ -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: @@ -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" @@ -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 @@ -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): @@ -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 diff --git a/tests/acceptance/test_decorators.py b/tests/acceptance/test_decorators.py index 03cf6773..96f0808f 100644 --- a/tests/acceptance/test_decorators.py +++ b/tests/acceptance/test_decorators.py @@ -3,13 +3,14 @@ from unittest import mock import pytest +from tests.utils import Keys from aiocache import cached, cached_stampede, multi_cached async def return_dict(keys=None): ret = {} - for value, key in enumerate(keys or [pytest.KEY, pytest.KEY_1]): + for value, key in enumerate(keys or [Keys.KEY, Keys.KEY_1]): ret[key] = str(value) return ret @@ -28,16 +29,16 @@ def default_cache(self, mocker, cache): @pytest.mark.asyncio async def test_cached_ttl(self, cache): - @cached(ttl=1, key=pytest.KEY) + @cached(ttl=1, key=Keys.KEY) async def fn(): return str(random.randint(1, 50)) resp1 = await fn() resp2 = await fn() - assert await cache.get(pytest.KEY) == resp1 == resp2 + assert await cache.get(Keys.KEY) == resp1 == resp2 await asyncio.sleep(1) - assert await cache.get(pytest.KEY) is None + assert await cache.get(Keys.KEY) is None @pytest.mark.asyncio async def test_cached_key_builder(self, cache): @@ -105,7 +106,7 @@ def default_cache(self, mocker, cache): async def test_multi_cached(self, cache): multi_cached_decorator = multi_cached("keys") - default_keys = {pytest.KEY, pytest.KEY_1} + default_keys = {Keys.KEY, Keys.KEY_1} await multi_cached_decorator(return_dict)(keys=default_keys) for key in default_keys: @@ -115,10 +116,10 @@ async def test_multi_cached(self, cache): async def test_keys_without_kwarg(self, cache): @multi_cached("keys") async def fn(keys): - return {pytest.KEY: 1} + return {Keys.KEY: 1} - await fn([pytest.KEY]) - assert await cache.exists(pytest.KEY) is True + await fn([Keys.KEY]) + assert await cache.exists(Keys.KEY) is True @pytest.mark.asyncio async def test_multi_cached_key_builder(self, cache): @@ -127,21 +128,21 @@ def build_key(key, f, self, keys, market="ES"): @multi_cached(keys_from_attr="keys", key_builder=build_key) async def fn(self, keys, market="ES"): - return {pytest.KEY: 1, pytest.KEY_1: 2} + return {Keys.KEY: 1, Keys.KEY_1: 2} - await fn("self", keys=[pytest.KEY, pytest.KEY_1]) - assert await cache.exists("fn_" + pytest.KEY + "_ES") is True - assert await cache.exists("fn_" + pytest.KEY_1 + "_ES") is True + await fn("self", keys=[Keys.KEY, Keys.KEY_1]) + assert await cache.exists("fn_" + Keys.KEY + "_ES") is True + assert await cache.exists("fn_" + Keys.KEY_1 + "_ES") is True @pytest.mark.asyncio async def test_fn_with_args(self, cache): @multi_cached("keys") async def fn(keys, *args): assert len(args) == 1 - return {pytest.KEY: 1} + return {Keys.KEY: 1} - await fn([pytest.KEY], "arg") - assert await cache.exists(pytest.KEY) is True + await fn([Keys.KEY], "arg") + assert await cache.exists(Keys.KEY) is True @pytest.mark.asyncio async def test_double_decorator(self, cache): @@ -154,7 +155,7 @@ async def wrapper(*args, **kwargs): @dummy_d @multi_cached("keys") async def fn(keys): - return {pytest.KEY: 1} + return {Keys.KEY: 1} - await fn([pytest.KEY]) - assert await cache.exists(pytest.KEY) is True + await fn([Keys.KEY]) + assert await cache.exists(Keys.KEY) is True diff --git a/tests/acceptance/test_lock.py b/tests/acceptance/test_lock.py index 885832b7..4c109b2c 100644 --- a/tests/acceptance/test_lock.py +++ b/tests/acceptance/test_lock.py @@ -1,6 +1,7 @@ import asyncio import pytest +from tests.utils import Keys from aiocache.lock import OptimisticLock, OptimisticLockError, RedLock from aiocache.serializers import StringSerializer @@ -8,7 +9,7 @@ @pytest.fixture def lock(cache): - return RedLock(cache, pytest.KEY, 20) + return RedLock(cache, Keys.KEY, 20) class TestRedLock: @@ -16,7 +17,7 @@ class TestRedLock: async def test_acquire(self, cache, lock): cache.serializer = StringSerializer() async with lock: - assert await cache.get(pytest.KEY + "-lock") == lock._value + assert await cache.get(Keys.KEY_LOCK) == lock._value @pytest.mark.asyncio async def test_release_does_nothing_when_no_lock(self, lock): @@ -26,7 +27,7 @@ async def test_release_does_nothing_when_no_lock(self, lock): async def test_acquire_release(self, cache, lock): async with lock: pass - assert await cache.get(pytest.KEY + "-lock") is None + assert await cache.get(Keys.KEY_LOCK) is None @pytest.mark.asyncio @pytest.mark.skip(reason="flaky test") @@ -36,16 +37,16 @@ async def test_locking_dogpile(self, mocker, cache): mocker.spy(cache, "_add") async def dummy(): - res = await cache.get(pytest.KEY) + res = await cache.get(Keys.KEY) if res is not None: return res - async with RedLock(cache, pytest.KEY, lease=5): - res = await cache.get(pytest.KEY) + async with RedLock(cache, Keys.KEY, lease=5): + res = await cache.get(Keys.KEY) if res is not None: return res await asyncio.sleep(0.1) - await cache.set(pytest.KEY, "value") + await cache.set(Keys.KEY, "value") await asyncio.gather(dummy(), dummy(), dummy(), dummy()) assert cache._add.call_count == 4 @@ -58,16 +59,16 @@ async def test_locking_dogpile_lease_expiration(self, mocker, cache): mocker.spy(cache, "set") async def dummy(): - res = await cache.get(pytest.KEY) + res = await cache.get(Keys.KEY) if res is not None: return res - async with RedLock(cache, pytest.KEY, lease=1): - res = await cache.get(pytest.KEY) + async with RedLock(cache, Keys.KEY, lease=1): + res = await cache.get(Keys.KEY) if res is not None: return res await asyncio.sleep(1.1) - await cache.set(pytest.KEY, "value") + await cache.set(Keys.KEY, "value") await asyncio.gather(dummy(), dummy(), dummy(), dummy()) assert cache.get.call_count == 8 @@ -76,7 +77,7 @@ async def dummy(): @pytest.mark.asyncio async def test_locking_dogpile_propagates_exceptions(self, cache): async def dummy(): - async with RedLock(cache, pytest.KEY, lease=1): + async with RedLock(cache, Keys.KEY, lease=1): raise ValueError() with pytest.raises(ValueError): @@ -86,7 +87,7 @@ async def dummy(): class TestMemoryRedLock: @pytest.fixture def lock(self, memory_cache): - return RedLock(memory_cache, pytest.KEY, 20) + return RedLock(memory_cache, Keys.KEY, 20) @pytest.mark.asyncio async def test_release_wrong_token_fails(self, lock): @@ -96,13 +97,13 @@ async def test_release_wrong_token_fails(self, lock): @pytest.mark.asyncio async def test_release_wrong_client_fails(self, memory_cache, lock): - wrong_lock = RedLock(memory_cache, pytest.KEY, 20) + wrong_lock = RedLock(memory_cache, Keys.KEY, 20) await lock.__aenter__() assert await wrong_lock.__aexit__("exc_type", "exc_value", "traceback") is None @pytest.mark.asyncio async def test_float_lease(self, memory_cache): - lock = RedLock(memory_cache, pytest.KEY, 0.1) + lock = RedLock(memory_cache, Keys.KEY, 0.1) await lock.__aenter__() await asyncio.sleep(0.2) assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None @@ -111,7 +112,7 @@ async def test_float_lease(self, memory_cache): class TestRedisRedLock: @pytest.fixture def lock(self, redis_cache): - return RedLock(redis_cache, pytest.KEY, 20) + return RedLock(redis_cache, Keys.KEY, 20) @pytest.mark.asyncio async def test_release_wrong_token_fails(self, lock): @@ -121,13 +122,13 @@ async def test_release_wrong_token_fails(self, lock): @pytest.mark.asyncio async def test_release_wrong_client_fails(self, redis_cache, lock): - wrong_lock = RedLock(redis_cache, pytest.KEY, 20) + wrong_lock = RedLock(redis_cache, Keys.KEY, 20) await lock.__aenter__() assert await wrong_lock.__aexit__("exc_type", "exc_value", "traceback") is None @pytest.mark.asyncio async def test_float_lease(self, redis_cache): - lock = RedLock(redis_cache, pytest.KEY, 0.1) + lock = RedLock(redis_cache, Keys.KEY, 0.1) await lock.__aenter__() await asyncio.sleep(0.2) assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None @@ -136,7 +137,7 @@ async def test_float_lease(self, redis_cache): class TestMemcachedRedLock: @pytest.fixture def lock(self, memcached_cache): - return RedLock(memcached_cache, pytest.KEY, 20) + return RedLock(memcached_cache, Keys.KEY, 20) @pytest.mark.asyncio async def test_release_wrong_token_succeeds_meh(self, lock): @@ -146,13 +147,13 @@ async def test_release_wrong_token_succeeds_meh(self, lock): @pytest.mark.asyncio async def test_release_wrong_client_succeeds_meh(self, memcached_cache, lock): - wrong_lock = RedLock(memcached_cache, pytest.KEY, 20) + wrong_lock = RedLock(memcached_cache, Keys.KEY, 20) await lock.__aenter__() assert await wrong_lock.__aexit__("exc_type", "exc_value", "traceback") is None @pytest.mark.asyncio async def test_float_lease(self, memcached_cache): - lock = RedLock(memcached_cache, pytest.KEY, 0.1) + lock = RedLock(memcached_cache, Keys.KEY, 0.1) with pytest.raises(TypeError): await lock.__aenter__() @@ -160,13 +161,13 @@ async def test_float_lease(self, memcached_cache): class TestOptimisticLock: @pytest.fixture def lock(self, cache): - return OptimisticLock(cache, pytest.KEY) + return OptimisticLock(cache, Keys.KEY) @pytest.mark.asyncio async def test_acquire(self, cache, lock): - await cache.set(pytest.KEY, "value") + await cache.set(Keys.KEY, "value") async with lock: - assert lock._token == await cache._gets(cache._build_key(pytest.KEY)) + assert lock._token == await cache._gets(cache._build_key(Keys.KEY)) @pytest.mark.asyncio async def test_release_does_nothing(self, lock): @@ -175,62 +176,62 @@ async def test_release_does_nothing(self, lock): @pytest.mark.asyncio async def test_check_and_set_not_existing_never_fails(self, cache, lock): async with lock as locked: - await cache.set(pytest.KEY, "conflicting_value") + await cache.set(Keys.KEY, "conflicting_value") await locked.cas("value") - assert await cache.get(pytest.KEY) == "value" + assert await cache.get(Keys.KEY) == "value" @pytest.mark.asyncio async def test_check_and_set(self, cache, lock): - await cache.set(pytest.KEY, "previous_value") + await cache.set(Keys.KEY, "previous_value") async with lock as locked: await locked.cas("value") - assert await cache.get(pytest.KEY) == "value" + assert await cache.get(Keys.KEY) == "value" @pytest.mark.asyncio async def test_check_and_set_fail(self, cache, lock): - await cache.set(pytest.KEY, "previous_value") + await cache.set(Keys.KEY, "previous_value") with pytest.raises(OptimisticLockError): async with lock as locked: - await cache.set(pytest.KEY, "conflicting_value") + await cache.set(Keys.KEY, "conflicting_value") await locked.cas("value") @pytest.mark.asyncio async def test_check_and_set_with_int_ttl(self, cache, lock): - await cache.set(pytest.KEY, "previous_value") + await cache.set(Keys.KEY, "previous_value") async with lock as locked: await locked.cas("value", ttl=1) await asyncio.sleep(1) - assert await cache.get(pytest.KEY) is None + assert await cache.get(Keys.KEY) is None class TestMemoryOptimisticLock: @pytest.fixture def lock(self, memory_cache): - return OptimisticLock(memory_cache, pytest.KEY) + return OptimisticLock(memory_cache, Keys.KEY) @pytest.mark.asyncio async def test_check_and_set_with_float_ttl(self, memory_cache, lock): - await memory_cache.set(pytest.KEY, "previous_value") + await memory_cache.set(Keys.KEY, "previous_value") async with lock as locked: await locked.cas("value", ttl=0.1) await asyncio.sleep(1) - assert await memory_cache.get(pytest.KEY) is None + assert await memory_cache.get(Keys.KEY) is None class TestRedisOptimisticLock: @pytest.fixture def lock(self, redis_cache): - return OptimisticLock(redis_cache, pytest.KEY) + return OptimisticLock(redis_cache, Keys.KEY) @pytest.mark.asyncio async def test_check_and_set_with_float_ttl(self, redis_cache, lock): - await redis_cache.set(pytest.KEY, "previous_value") + await redis_cache.set(Keys.KEY, "previous_value") async with lock as locked: await locked.cas("value", ttl=0.1) await asyncio.sleep(1) - assert await redis_cache.get(pytest.KEY) is None + assert await redis_cache.get(Keys.KEY) is None diff --git a/tests/acceptance/test_serializers.py b/tests/acceptance/test_serializers.py index c0669124..ee50a846 100644 --- a/tests/acceptance/test_serializers.py +++ b/tests/acceptance/test_serializers.py @@ -3,6 +3,7 @@ import pytest from marshmallow import Schema, fields, post_load +from tests.utils import Keys try: import ujson as json @@ -66,22 +67,22 @@ class TestNullSerializer: @pytest.mark.asyncio async def test_set_get_types(self, memory_cache, obj): memory_cache.serializer = NullSerializer() - assert await memory_cache.set(pytest.KEY, obj) is True - assert await memory_cache.get(pytest.KEY) is obj + assert await memory_cache.set(Keys.KEY, obj) is True + assert await memory_cache.get(Keys.KEY) is obj @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_add_get_types(self, memory_cache, obj): memory_cache.serializer = NullSerializer() - assert await memory_cache.add(pytest.KEY, obj) is True - assert await memory_cache.get(pytest.KEY) is obj + assert await memory_cache.add(Keys.KEY, obj) is True + assert await memory_cache.get(Keys.KEY) is obj @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_multi_set_multi_get_types(self, memory_cache, obj): memory_cache.serializer = NullSerializer() - assert await memory_cache.multi_set([(pytest.KEY, obj)]) is True - assert (await memory_cache.multi_get([pytest.KEY]))[0] is obj + assert await memory_cache.multi_set([(Keys.KEY, obj)]) is True + assert (await memory_cache.multi_get([Keys.KEY]))[0] is obj class TestStringSerializer: @@ -92,22 +93,22 @@ class TestStringSerializer: @pytest.mark.asyncio async def test_set_get_types(self, cache, obj): cache.serializer = StringSerializer() - assert await cache.set(pytest.KEY, obj) is True - assert await cache.get(pytest.KEY) == str(obj) + assert await cache.set(Keys.KEY, obj) is True + assert await cache.get(Keys.KEY) == str(obj) @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_add_get_types(self, cache, obj): cache.serializer = StringSerializer() - assert await cache.add(pytest.KEY, obj) is True - assert await cache.get(pytest.KEY) == str(obj) + assert await cache.add(Keys.KEY, obj) is True + assert await cache.get(Keys.KEY) == str(obj) @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_multi_set_multi_get_types(self, cache, obj): cache.serializer = StringSerializer() - assert await cache.multi_set([(pytest.KEY, obj)]) is True - assert await cache.multi_get([pytest.KEY]) == [str(obj)] + assert await cache.multi_set([(Keys.KEY, obj)]) is True + assert await cache.multi_get([Keys.KEY]) == [str(obj)] class TestJsonSerializer: @@ -118,22 +119,22 @@ class TestJsonSerializer: @pytest.mark.asyncio async def test_set_get_types(self, cache, obj): cache.serializer = JsonSerializer() - assert await cache.set(pytest.KEY, obj) is True - assert await cache.get(pytest.KEY) == json.loads(json.dumps(obj)) + assert await cache.set(Keys.KEY, obj) is True + assert await cache.get(Keys.KEY) == json.loads(json.dumps(obj)) @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_add_get_types(self, cache, obj): cache.serializer = JsonSerializer() - assert await cache.add(pytest.KEY, obj) is True - assert await cache.get(pytest.KEY) == json.loads(json.dumps(obj)) + assert await cache.add(Keys.KEY, obj) is True + assert await cache.get(Keys.KEY) == json.loads(json.dumps(obj)) @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_multi_set_multi_get_types(self, cache, obj): cache.serializer = JsonSerializer() - assert await cache.multi_set([(pytest.KEY, obj)]) is True - assert await cache.multi_get([pytest.KEY]) == [json.loads(json.dumps(obj))] + assert await cache.multi_set([(Keys.KEY, obj)]) is True + assert await cache.multi_get([Keys.KEY]) == [json.loads(json.dumps(obj))] class TestPickleSerializer: @@ -144,36 +145,36 @@ class TestPickleSerializer: @pytest.mark.asyncio async def test_set_get_types(self, cache, obj): cache.serializer = PickleSerializer() - assert await cache.set(pytest.KEY, obj) is True - assert await cache.get(pytest.KEY) == pickle.loads(pickle.dumps(obj)) + assert await cache.set(Keys.KEY, obj) is True + assert await cache.get(Keys.KEY) == pickle.loads(pickle.dumps(obj)) @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_add_get_types(self, cache, obj): cache.serializer = PickleSerializer() - assert await cache.add(pytest.KEY, obj) is True - assert await cache.get(pytest.KEY) == pickle.loads(pickle.dumps(obj)) + assert await cache.add(Keys.KEY, obj) is True + assert await cache.get(Keys.KEY) == pickle.loads(pickle.dumps(obj)) @pytest.mark.parametrize("obj", TYPES) @pytest.mark.asyncio async def test_multi_set_multi_get_types(self, cache, obj): cache.serializer = PickleSerializer() - assert await cache.multi_set([(pytest.KEY, obj)]) is True - assert await cache.multi_get([pytest.KEY]) == [pickle.loads(pickle.dumps(obj))] + assert await cache.multi_set([(Keys.KEY, obj)]) is True + assert await cache.multi_get([Keys.KEY]) == [pickle.loads(pickle.dumps(obj))] class TestAltSerializers: @pytest.mark.asyncio async def test_get_set_alt_serializer_functions(self, cache): cache.serializer = StringSerializer() - await cache.set(pytest.KEY, "value", dumps_fn=dumps) - assert await cache.get(pytest.KEY) == "v4lu3" - assert await cache.get(pytest.KEY, loads_fn=loads) == "value" + await cache.set(Keys.KEY, "value", dumps_fn=dumps) + assert await cache.get(Keys.KEY) == "v4lu3" + assert await cache.get(Keys.KEY, loads_fn=loads) == "value" @pytest.mark.asyncio async def test_get_set_alt_serializer_class(self, cache): my_serializer = MyTypeSchema() my_obj = MyType() cache.serializer = my_serializer - await cache.set(pytest.KEY, my_obj) - assert await cache.get(pytest.KEY) == my_serializer.loads(my_serializer.dumps(my_obj)) + await cache.set(Keys.KEY, my_obj) + assert await cache.get(Keys.KEY) == my_serializer.loads(my_serializer.dumps(my_obj)) diff --git a/tests/ut/backends/test_memcached.py b/tests/ut/backends/test_memcached.py index e7d93fda..1b32c914 100644 --- a/tests/ut/backends/test_memcached.py +++ b/tests/ut/backends/test_memcached.py @@ -2,6 +2,7 @@ import aiomcache import pytest +from tests.utils import Keys from aiocache.backends.memcached import MemcachedBackend, MemcachedCache from aiocache.base import BaseCache @@ -47,172 +48,172 @@ def test_setup_casts(self): @pytest.mark.asyncio async def test_get(self, memcached): memcached.client.get.return_value = b"value" - assert await memcached._get(pytest.KEY) == "value" - memcached.client.get.assert_called_with(pytest.KEY) + assert await memcached._get(Keys.KEY) == "value" + memcached.client.get.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_gets(self, memcached): memcached.client.gets.return_value = b"value", 12345 - assert await memcached._gets(pytest.KEY) == 12345 - memcached.client.gets.assert_called_with(pytest.KEY.encode()) + assert await memcached._gets(Keys.KEY) == 12345 + memcached.client.gets.assert_called_with(Keys.KEY.encode()) @pytest.mark.asyncio async def test_get_none(self, memcached): memcached.client.get.return_value = None - assert await memcached._get(pytest.KEY) is None - memcached.client.get.assert_called_with(pytest.KEY) + assert await memcached._get(Keys.KEY) is None + memcached.client.get.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_get_no_encoding(self, memcached): memcached.client.get.return_value = b"value" - assert await memcached._get(pytest.KEY, encoding=None) == b"value" - memcached.client.get.assert_called_with(pytest.KEY) + assert await memcached._get(Keys.KEY, encoding=None) == b"value" + memcached.client.get.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_set(self, memcached): - await memcached._set(pytest.KEY, "value") - memcached.client.set.assert_called_with(pytest.KEY, b"value", exptime=0) + await memcached._set(Keys.KEY, "value") + memcached.client.set.assert_called_with(Keys.KEY, b"value", exptime=0) - await memcached._set(pytest.KEY, "value", ttl=1) - memcached.client.set.assert_called_with(pytest.KEY, b"value", exptime=1) + await memcached._set(Keys.KEY, "value", ttl=1) + memcached.client.set.assert_called_with(Keys.KEY, b"value", exptime=1) @pytest.mark.asyncio async def test_set_float_ttl(self, memcached): memcached.client.set.side_effect = aiomcache.exceptions.ValidationException("msg") with pytest.raises(TypeError) as exc_info: - await memcached._set(pytest.KEY, "value", ttl=0.1) + await memcached._set(Keys.KEY, "value", ttl=0.1) assert str(exc_info.value) == "aiomcache error: msg" @pytest.mark.asyncio async def test_set_cas_token(self, mocker, memcached): mocker.spy(memcached, "_cas") - await memcached._set(pytest.KEY, "value", _cas_token="token") - memcached._cas.assert_called_with(pytest.KEY, b"value", "token", ttl=0, _conn=None) + await memcached._set(Keys.KEY, "value", _cas_token="token") + memcached._cas.assert_called_with(Keys.KEY, b"value", "token", ttl=0, _conn=None) @pytest.mark.asyncio async def test_cas(self, mocker, memcached): memcached.client.cas.return_value = True - assert await memcached._cas(pytest.KEY, b"value", "token", ttl=0) is True - memcached.client.cas.assert_called_with(pytest.KEY, b"value", "token", exptime=0) + assert await memcached._cas(Keys.KEY, b"value", "token", ttl=0) is True + memcached.client.cas.assert_called_with(Keys.KEY, b"value", "token", exptime=0) @pytest.mark.asyncio async def test_cas_fail(self, mocker, memcached): memcached.client.cas.return_value = False - assert await memcached._cas(pytest.KEY, b"value", "token", ttl=0) is False - memcached.client.cas.assert_called_with(pytest.KEY, b"value", "token", exptime=0) + assert await memcached._cas(Keys.KEY, b"value", "token", ttl=0) is False + memcached.client.cas.assert_called_with(Keys.KEY, b"value", "token", exptime=0) @pytest.mark.asyncio async def test_multi_get(self, memcached): memcached.client.multi_get.return_value = [b"value", b"random"] - assert await memcached._multi_get([pytest.KEY, pytest.KEY_1]) == ["value", "random"] - memcached.client.multi_get.assert_called_with(pytest.KEY, pytest.KEY_1) + assert await memcached._multi_get([Keys.KEY, Keys.KEY_1]) == ["value", "random"] + memcached.client.multi_get.assert_called_with(Keys.KEY, Keys.KEY_1) @pytest.mark.asyncio async def test_multi_get_none(self, memcached): memcached.client.multi_get.return_value = [b"value", None] - assert await memcached._multi_get([pytest.KEY, pytest.KEY_1]) == ["value", None] - memcached.client.multi_get.assert_called_with(pytest.KEY, pytest.KEY_1) + assert await memcached._multi_get([Keys.KEY, Keys.KEY_1]) == ["value", None] + memcached.client.multi_get.assert_called_with(Keys.KEY, Keys.KEY_1) @pytest.mark.asyncio async def test_multi_get_no_encoding(self, memcached): memcached.client.multi_get.return_value = [b"value", None] - assert await memcached._multi_get([pytest.KEY, pytest.KEY_1], encoding=None) == [ + assert await memcached._multi_get([Keys.KEY, Keys.KEY_1], encoding=None) == [ b"value", None, ] - memcached.client.multi_get.assert_called_with(pytest.KEY, pytest.KEY_1) + memcached.client.multi_get.assert_called_with(Keys.KEY, Keys.KEY_1) @pytest.mark.asyncio async def test_multi_set(self, memcached): - await memcached._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")]) - memcached.client.set.assert_any_call(pytest.KEY, b"value", exptime=0) - memcached.client.set.assert_any_call(pytest.KEY_1, b"random", exptime=0) + await memcached._multi_set([(Keys.KEY, "value"), (Keys.KEY_1, "random")]) + memcached.client.set.assert_any_call(Keys.KEY, b"value", exptime=0) + memcached.client.set.assert_any_call(Keys.KEY_1, b"random", exptime=0) assert memcached.client.set.call_count == 2 - await memcached._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")], ttl=1) - memcached.client.set.assert_any_call(pytest.KEY, b"value", exptime=1) - memcached.client.set.assert_any_call(pytest.KEY_1, b"random", exptime=1) + await memcached._multi_set([(Keys.KEY, "value"), (Keys.KEY_1, "random")], ttl=1) + memcached.client.set.assert_any_call(Keys.KEY, b"value", exptime=1) + memcached.client.set.assert_any_call(Keys.KEY_1, b"random", exptime=1) assert memcached.client.set.call_count == 4 @pytest.mark.asyncio async def test_multi_set_float_ttl(self, memcached): memcached.client.set.side_effect = aiomcache.exceptions.ValidationException("msg") with pytest.raises(TypeError) as exc_info: - await memcached._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")], ttl=0.1) + await memcached._multi_set([(Keys.KEY, "value"), (Keys.KEY_1, "random")], ttl=0.1) assert str(exc_info.value) == "aiomcache error: msg" @pytest.mark.asyncio async def test_add(self, memcached): - await memcached._add(pytest.KEY, "value") - memcached.client.add.assert_called_with(pytest.KEY, b"value", exptime=0) + await memcached._add(Keys.KEY, "value") + memcached.client.add.assert_called_with(Keys.KEY, b"value", exptime=0) - await memcached._add(pytest.KEY, "value", ttl=1) - memcached.client.add.assert_called_with(pytest.KEY, b"value", exptime=1) + await memcached._add(Keys.KEY, "value", ttl=1) + memcached.client.add.assert_called_with(Keys.KEY, b"value", exptime=1) @pytest.mark.asyncio async def test_add_existing(self, memcached): memcached.client.add.return_value = False with pytest.raises(ValueError): - await memcached._add(pytest.KEY, "value") + await memcached._add(Keys.KEY, "value") @pytest.mark.asyncio async def test_add_float_ttl(self, memcached): memcached.client.add.side_effect = aiomcache.exceptions.ValidationException("msg") with pytest.raises(TypeError) as exc_info: - await memcached._add(pytest.KEY, "value", 0.1) + await memcached._add(Keys.KEY, "value", 0.1) assert str(exc_info.value) == "aiomcache error: msg" @pytest.mark.asyncio async def test_exists(self, memcached): - await memcached._exists(pytest.KEY) - memcached.client.append.assert_called_with(pytest.KEY, b"") + await memcached._exists(Keys.KEY) + memcached.client.append.assert_called_with(Keys.KEY, b"") @pytest.mark.asyncio async def test_increment(self, memcached): - await memcached._increment(pytest.KEY, 2) - memcached.client.incr.assert_called_with(pytest.KEY, 2) + await memcached._increment(Keys.KEY, 2) + memcached.client.incr.assert_called_with(Keys.KEY, 2) @pytest.mark.asyncio async def test_increment_negative(self, memcached): - await memcached._increment(pytest.KEY, -2) - memcached.client.decr.assert_called_with(pytest.KEY, 2) + await memcached._increment(Keys.KEY, -2) + memcached.client.decr.assert_called_with(Keys.KEY, 2) @pytest.mark.asyncio async def test_increment_missing(self, memcached): memcached.client.incr.side_effect = aiomcache.exceptions.ClientException("NOT_FOUND") - await memcached._increment(pytest.KEY, 2) - memcached.client.incr.assert_called_with(pytest.KEY, 2) - memcached.client.set.assert_called_with(pytest.KEY, b"2", exptime=0) + await memcached._increment(Keys.KEY, 2) + memcached.client.incr.assert_called_with(Keys.KEY, 2) + memcached.client.set.assert_called_with(Keys.KEY, b"2", exptime=0) @pytest.mark.asyncio async def test_increment_missing_negative(self, memcached): memcached.client.decr.side_effect = aiomcache.exceptions.ClientException("NOT_FOUND") - await memcached._increment(pytest.KEY, -2) - memcached.client.decr.assert_called_with(pytest.KEY, 2) - memcached.client.set.assert_called_with(pytest.KEY, b"-2", exptime=0) + await memcached._increment(Keys.KEY, -2) + memcached.client.decr.assert_called_with(Keys.KEY, 2) + memcached.client.set.assert_called_with(Keys.KEY, b"-2", exptime=0) @pytest.mark.asyncio async def test_increment_typerror(self, memcached): memcached.client.incr.side_effect = aiomcache.exceptions.ClientException("msg") with pytest.raises(TypeError) as exc_info: - await memcached._increment(pytest.KEY, 2) + await memcached._increment(Keys.KEY, 2) assert str(exc_info.value) == "aiomcache error: msg" @pytest.mark.asyncio async def test_expire(self, memcached): - await memcached._expire(pytest.KEY, 1) - memcached.client.touch.assert_called_with(pytest.KEY, 1) + await memcached._expire(Keys.KEY, 1) + memcached.client.touch.assert_called_with(Keys.KEY, 1) @pytest.mark.asyncio async def test_delete(self, memcached): - assert await memcached._delete(pytest.KEY) == 1 - memcached.client.delete.assert_called_with(pytest.KEY) + assert await memcached._delete(Keys.KEY) == 1 + memcached.client.delete.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_delete_missing(self, memcached): memcached.client.delete.return_value = False - assert await memcached._delete(pytest.KEY) == 0 - memcached.client.delete.assert_called_with(pytest.KEY) + assert await memcached._delete(Keys.KEY) == 0 + memcached.client.delete.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_clear(self, memcached): @@ -226,23 +227,23 @@ async def test_clear_with_namespace(self, memcached): @pytest.mark.asyncio async def test_raw(self, memcached): - await memcached._raw("get", pytest.KEY) - await memcached._raw("set", pytest.KEY, 1) - memcached.client.get.assert_called_with(pytest.KEY) - memcached.client.set.assert_called_with(pytest.KEY, 1) + await memcached._raw("get", Keys.KEY) + await memcached._raw("set", Keys.KEY, 1) + memcached.client.get.assert_called_with(Keys.KEY) + memcached.client.set.assert_called_with(Keys.KEY, 1) @pytest.mark.asyncio async def test_raw_bytes(self, memcached): - await memcached._raw("set", pytest.KEY, "asd") - await memcached._raw("get", pytest.KEY, encoding=None) - memcached.client.get.assert_called_with(pytest.KEY) - memcached.client.set.assert_called_with(pytest.KEY, "asd") + await memcached._raw("set", Keys.KEY, "asd") + await memcached._raw("get", Keys.KEY, encoding=None) + memcached.client.get.assert_called_with(Keys.KEY) + memcached.client.set.assert_called_with(Keys.KEY, "asd") @pytest.mark.asyncio async def test_redlock_release(self, mocker, memcached): mocker.spy(memcached, "_delete") - await memcached._redlock_release(pytest.KEY, "random") - memcached._delete.assert_called_with(pytest.KEY) + await memcached._redlock_release(Keys.KEY, "random") + memcached._delete.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_close(self, memcached): @@ -271,13 +272,13 @@ def test_parse_uri_path(self): @pytest.mark.parametrize( "namespace, expected", - ([None, "test" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns" + pytest.KEY]), # type: ignore[attr-defined] # noqa: B950 + ([None, "test" + Keys.KEY], ["", Keys.KEY], ["my_ns", "my_ns" + Keys.KEY]), # type: ignore[attr-defined] # noqa: B950 ) def test_build_key_bytes(self, set_test_namespace, memcached_cache, namespace, expected): - assert memcached_cache.build_key(pytest.KEY, namespace=namespace) == expected.encode() + assert memcached_cache.build_key(Keys.KEY, namespace=namespace) == expected.encode() def test_build_key_no_namespace(self, memcached_cache): - assert memcached_cache.build_key(pytest.KEY, namespace=None) == pytest.KEY.encode() + assert memcached_cache.build_key(Keys.KEY, namespace=None) == Keys.KEY.encode() def test_build_key_no_spaces(self, memcached_cache): assert memcached_cache.build_key("hello world") == b"hello_world" diff --git a/tests/ut/backends/test_memory.py b/tests/ut/backends/test_memory.py index 537ca72d..8051df6e 100644 --- a/tests/ut/backends/test_memory.py +++ b/tests/ut/backends/test_memory.py @@ -2,6 +2,7 @@ from unittest.mock import ANY, MagicMock, patch import pytest +from tests.utils import Keys from aiocache.backends.memory import SimpleMemoryBackend, SimpleMemoryCache from aiocache.base import BaseCache @@ -19,145 +20,145 @@ def memory(mocker): class TestSimpleMemoryBackend: @pytest.mark.asyncio async def test_get(self, memory): - await memory._get(pytest.KEY) - SimpleMemoryBackend._cache.get.assert_called_with(pytest.KEY) + await memory._get(Keys.KEY) + SimpleMemoryBackend._cache.get.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_gets(self, mocker, memory): mocker.spy(memory, "_get") - await memory._gets(pytest.KEY) - memory._get.assert_called_with(pytest.KEY, encoding="utf-8", _conn=ANY) + await memory._gets(Keys.KEY) + memory._get.assert_called_with(Keys.KEY, encoding="utf-8", _conn=ANY) @pytest.mark.asyncio async def test_set(self, memory): - await memory._set(pytest.KEY, "value") - SimpleMemoryBackend._cache.__setitem__.assert_called_with(pytest.KEY, "value") + await memory._set(Keys.KEY, "value") + SimpleMemoryBackend._cache.__setitem__.assert_called_with(Keys.KEY, "value") @pytest.mark.asyncio async def test_set_no_ttl_no_handle(self, memory): - await memory._set(pytest.KEY, "value", ttl=0) - assert pytest.KEY not in memory._handlers + await memory._set(Keys.KEY, "value", ttl=0) + assert Keys.KEY not in memory._handlers - await memory._set(pytest.KEY, "value") - assert pytest.KEY not in memory._handlers + await memory._set(Keys.KEY, "value") + assert Keys.KEY not in memory._handlers @pytest.mark.asyncio async def test_set_cancel_previous_ttl_handle(self, memory, mocker): with patch("asyncio.get_event_loop"): - await memory._set(pytest.KEY, "value", ttl=0.1) - memory._handlers[pytest.KEY].cancel.assert_not_called() + await memory._set(Keys.KEY, "value", ttl=0.1) + memory._handlers[Keys.KEY].cancel.assert_not_called() - await memory._set(pytest.KEY, "new_value", ttl=0.1) - memory._handlers[pytest.KEY].cancel.assert_called_once_with() + await memory._set(Keys.KEY, "new_value", ttl=0.1) + memory._handlers[Keys.KEY].cancel.assert_called_once_with() @pytest.mark.asyncio async def test_set_ttl_handle(self, memory): - await memory._set(pytest.KEY, "value", ttl=100) - assert pytest.KEY in memory._handlers - assert isinstance(memory._handlers[pytest.KEY], asyncio.Handle) + await memory._set(Keys.KEY, "value", ttl=100) + assert Keys.KEY in memory._handlers + assert isinstance(memory._handlers[Keys.KEY], asyncio.Handle) @pytest.mark.asyncio async def test_set_cas_token(self, mocker, memory): memory._cache.get.return_value = "old_value" - assert await memory._set(pytest.KEY, "value", _cas_token="old_value") == 1 - SimpleMemoryBackend._cache.__setitem__.assert_called_with(pytest.KEY, "value") + assert await memory._set(Keys.KEY, "value", _cas_token="old_value") == 1 + SimpleMemoryBackend._cache.__setitem__.assert_called_with(Keys.KEY, "value") @pytest.mark.asyncio async def test_set_cas_fail(self, mocker, memory): memory._cache.get.return_value = "value" - assert await memory._set(pytest.KEY, "value", _cas_token="old_value") == 0 + assert await memory._set(Keys.KEY, "value", _cas_token="old_value") == 0 assert SimpleMemoryBackend._cache.__setitem__.call_count == 0 @pytest.mark.asyncio async def test_multi_get(self, memory): - await memory._multi_get([pytest.KEY, pytest.KEY_1]) - SimpleMemoryBackend._cache.get.assert_any_call(pytest.KEY) - SimpleMemoryBackend._cache.get.assert_any_call(pytest.KEY_1) + await memory._multi_get([Keys.KEY, Keys.KEY_1]) + SimpleMemoryBackend._cache.get.assert_any_call(Keys.KEY) + SimpleMemoryBackend._cache.get.assert_any_call(Keys.KEY_1) @pytest.mark.asyncio async def test_multi_set(self, memory): - await memory._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")]) - SimpleMemoryBackend._cache.__setitem__.assert_any_call(pytest.KEY, "value") - SimpleMemoryBackend._cache.__setitem__.assert_any_call(pytest.KEY_1, "random") + await memory._multi_set([(Keys.KEY, "value"), (Keys.KEY_1, "random")]) + SimpleMemoryBackend._cache.__setitem__.assert_any_call(Keys.KEY, "value") + SimpleMemoryBackend._cache.__setitem__.assert_any_call(Keys.KEY_1, "random") @pytest.mark.asyncio async def test_add(self, memory, mocker): mocker.spy(memory, "_set") - await memory._add(pytest.KEY, "value") - memory._set.assert_called_with(pytest.KEY, "value", ttl=None) + await memory._add(Keys.KEY, "value") + memory._set.assert_called_with(Keys.KEY, "value", ttl=None) @pytest.mark.asyncio async def test_add_existing(self, memory): SimpleMemoryBackend._cache.__contains__.return_value = True with pytest.raises(ValueError): - await memory._add(pytest.KEY, "value") + await memory._add(Keys.KEY, "value") @pytest.mark.asyncio async def test_exists(self, memory): - await memory._exists(pytest.KEY) - SimpleMemoryBackend._cache.__contains__.assert_called_with(pytest.KEY) + await memory._exists(Keys.KEY) + SimpleMemoryBackend._cache.__contains__.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_increment(self, memory): - await memory._increment(pytest.KEY, 2) - SimpleMemoryBackend._cache.__contains__.assert_called_with(pytest.KEY) - SimpleMemoryBackend._cache.__setitem__.assert_called_with(pytest.KEY, 2) + await memory._increment(Keys.KEY, 2) + SimpleMemoryBackend._cache.__contains__.assert_called_with(Keys.KEY) + SimpleMemoryBackend._cache.__setitem__.assert_called_with(Keys.KEY, 2) @pytest.mark.asyncio async def test_increment_missing(self, memory): SimpleMemoryBackend._cache.__contains__.return_value = True SimpleMemoryBackend._cache.__getitem__.return_value = 2 - await memory._increment(pytest.KEY, 2) - SimpleMemoryBackend._cache.__getitem__.assert_called_with(pytest.KEY) - SimpleMemoryBackend._cache.__setitem__.assert_called_with(pytest.KEY, 4) + await memory._increment(Keys.KEY, 2) + SimpleMemoryBackend._cache.__getitem__.assert_called_with(Keys.KEY) + SimpleMemoryBackend._cache.__setitem__.assert_called_with(Keys.KEY, 4) @pytest.mark.asyncio async def test_increment_typerror(self, memory): SimpleMemoryBackend._cache.__contains__.return_value = True SimpleMemoryBackend._cache.__getitem__.return_value = "asd" with pytest.raises(TypeError): - await memory._increment(pytest.KEY, 2) + await memory._increment(Keys.KEY, 2) @pytest.mark.asyncio async def test_expire_no_handle_no_ttl(self, memory): SimpleMemoryBackend._cache.__contains__.return_value = True - await memory._expire(pytest.KEY, 0) - assert memory._handlers.get(pytest.KEY) is None + await memory._expire(Keys.KEY, 0) + assert memory._handlers.get(Keys.KEY) is None @pytest.mark.asyncio async def test_expire_no_handle_ttl(self, memory): SimpleMemoryBackend._cache.__contains__.return_value = True - await memory._expire(pytest.KEY, 1) - assert isinstance(memory._handlers.get(pytest.KEY), asyncio.Handle) + await memory._expire(Keys.KEY, 1) + assert isinstance(memory._handlers.get(Keys.KEY), asyncio.Handle) @pytest.mark.asyncio async def test_expire_handle_ttl(self, memory): fake = MagicMock() - SimpleMemoryBackend._handlers[pytest.KEY] = fake + SimpleMemoryBackend._handlers[Keys.KEY] = fake SimpleMemoryBackend._cache.__contains__.return_value = True - await memory._expire(pytest.KEY, 1) + await memory._expire(Keys.KEY, 1) assert fake.cancel.call_count == 1 - assert isinstance(memory._handlers.get(pytest.KEY), asyncio.Handle) + assert isinstance(memory._handlers.get(Keys.KEY), asyncio.Handle) @pytest.mark.asyncio async def test_expire_missing(self, memory): SimpleMemoryBackend._cache.__contains__.return_value = False - assert await memory._expire(pytest.KEY, 1) is False + assert await memory._expire(Keys.KEY, 1) is False @pytest.mark.asyncio async def test_delete(self, memory): fake = MagicMock() - SimpleMemoryBackend._handlers[pytest.KEY] = fake - await memory._delete(pytest.KEY) + SimpleMemoryBackend._handlers[Keys.KEY] = fake + await memory._delete(Keys.KEY) assert fake.cancel.call_count == 1 - assert pytest.KEY not in SimpleMemoryBackend._handlers - SimpleMemoryBackend._cache.pop.assert_called_with(pytest.KEY, None) + assert Keys.KEY not in SimpleMemoryBackend._handlers + SimpleMemoryBackend._cache.pop.assert_called_with(Keys.KEY, None) @pytest.mark.asyncio async def test_delete_missing(self, memory): SimpleMemoryBackend._cache.pop.return_value = None - await memory._delete(pytest.KEY) - SimpleMemoryBackend._cache.pop.assert_called_with(pytest.KEY, None) + await memory._delete(Keys.KEY) + SimpleMemoryBackend._cache.pop.assert_called_with(Keys.KEY, None) @pytest.mark.asyncio async def test_delete_non_truthy(self, memory): @@ -168,10 +169,10 @@ async def test_delete_non_truthy(self, memory): bool(non_truthy) SimpleMemoryBackend._cache.pop.return_value = non_truthy - await memory._delete(pytest.KEY) + await memory._delete(Keys.KEY) assert non_truthy.__bool__.call_count == 1 - SimpleMemoryBackend._cache.pop.assert_called_with(pytest.KEY, None) + SimpleMemoryBackend._cache.pop.assert_called_with(Keys.KEY, None) @pytest.mark.asyncio async def test_clear_namespace(self, memory): @@ -191,24 +192,24 @@ async def test_clear_no_namespace(self, memory): @pytest.mark.asyncio async def test_raw(self, memory): - await memory._raw("get", pytest.KEY) - SimpleMemoryBackend._cache.get.assert_called_with(pytest.KEY) + await memory._raw("get", Keys.KEY) + SimpleMemoryBackend._cache.get.assert_called_with(Keys.KEY) - await memory._set(pytest.KEY, "value") - SimpleMemoryBackend._cache.__setitem__.assert_called_with(pytest.KEY, "value") + await memory._set(Keys.KEY, "value") + SimpleMemoryBackend._cache.__setitem__.assert_called_with(Keys.KEY, "value") @pytest.mark.asyncio async def test_redlock_release(self, memory): SimpleMemoryBackend._cache.get.return_value = "lock" - assert await memory._redlock_release(pytest.KEY, "lock") == 1 - SimpleMemoryBackend._cache.get.assert_called_with(pytest.KEY) - SimpleMemoryBackend._cache.pop.assert_called_with(pytest.KEY) + assert await memory._redlock_release(Keys.KEY, "lock") == 1 + SimpleMemoryBackend._cache.get.assert_called_with(Keys.KEY) + SimpleMemoryBackend._cache.pop.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_redlock_release_nokey(self, memory): SimpleMemoryBackend._cache.get.return_value = None - assert await memory._redlock_release(pytest.KEY, "lock") == 0 - SimpleMemoryBackend._cache.get.assert_called_with(pytest.KEY) + assert await memory._redlock_release(Keys.KEY, "lock") == 0 + SimpleMemoryBackend._cache.get.assert_called_with(Keys.KEY) assert SimpleMemoryBackend._cache.pop.call_count == 0 diff --git a/tests/ut/backends/test_redis.py b/tests/ut/backends/test_redis.py index e36fdd73..79136af4 100644 --- a/tests/ut/backends/test_redis.py +++ b/tests/ut/backends/test_redis.py @@ -2,6 +2,7 @@ import aioredis import pytest +from tests.utils import Keys from aiocache.backends.redis import RedisBackend, RedisCache, conn from aiocache.base import BaseCache @@ -132,39 +133,39 @@ async def test_get_pool_calls_create_pool(self, redis, create_pool): @pytest.mark.asyncio async def test_get(self, redis, redis_connection): - await redis._get(pytest.KEY) - redis_connection.get.assert_called_with(pytest.KEY, encoding="utf-8") + await redis._get(Keys.KEY) + redis_connection.get.assert_called_with(Keys.KEY, encoding="utf-8") @pytest.mark.asyncio async def test_gets(self, mocker, redis, redis_connection): mocker.spy(redis, "_get") - await redis._gets(pytest.KEY) - redis._get.assert_called_with(pytest.KEY, encoding="utf-8", _conn=ANY) + await redis._gets(Keys.KEY) + redis._get.assert_called_with(Keys.KEY, encoding="utf-8", _conn=ANY) @pytest.mark.asyncio async def test_set(self, redis, redis_connection): - await redis._set(pytest.KEY, "value") - redis_connection.set.assert_called_with(pytest.KEY, "value") + await redis._set(Keys.KEY, "value") + redis_connection.set.assert_called_with(Keys.KEY, "value") - await redis._set(pytest.KEY, "value", ttl=1) - redis_connection.setex.assert_called_with(pytest.KEY, 1, "value") + await redis._set(Keys.KEY, "value", ttl=1) + redis_connection.setex.assert_called_with(Keys.KEY, 1, "value") @pytest.mark.asyncio async def test_set_cas_token(self, mocker, redis, redis_connection): mocker.spy(redis, "_cas") - await redis._set(pytest.KEY, "value", _cas_token="old_value", _conn=redis_connection) + await redis._set(Keys.KEY, "value", _cas_token="old_value", _conn=redis_connection) redis._cas.assert_called_with( - pytest.KEY, "value", "old_value", ttl=None, _conn=redis_connection + Keys.KEY, "value", "old_value", ttl=None, _conn=redis_connection ) @pytest.mark.asyncio async def test_cas(self, mocker, redis, redis_connection): mocker.spy(redis, "_raw") - await redis._cas(pytest.KEY, "value", "old_value", ttl=10, _conn=redis_connection) + await redis._cas(Keys.KEY, "value", "old_value", ttl=10, _conn=redis_connection) redis._raw.assert_called_with( "eval", redis.CAS_SCRIPT, - [pytest.KEY], + [Keys.KEY], ["value", "old_value", "EX", 10], _conn=redis_connection, ) @@ -172,84 +173,84 @@ async def test_cas(self, mocker, redis, redis_connection): @pytest.mark.asyncio async def test_cas_float_ttl(self, mocker, redis, redis_connection): mocker.spy(redis, "_raw") - await redis._cas(pytest.KEY, "value", "old_value", ttl=0.1, _conn=redis_connection) + await redis._cas(Keys.KEY, "value", "old_value", ttl=0.1, _conn=redis_connection) redis._raw.assert_called_with( "eval", redis.CAS_SCRIPT, - [pytest.KEY], + [Keys.KEY], ["value", "old_value", "PX", 100], _conn=redis_connection, ) @pytest.mark.asyncio async def test_multi_get(self, redis, redis_connection): - await redis._multi_get([pytest.KEY, pytest.KEY_1]) - redis_connection.mget.assert_called_with(pytest.KEY, pytest.KEY_1, encoding="utf-8") + await redis._multi_get([Keys.KEY, Keys.KEY_1]) + redis_connection.mget.assert_called_with(Keys.KEY, Keys.KEY_1, encoding="utf-8") @pytest.mark.asyncio async def test_multi_set(self, redis, redis_connection): - await redis._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")]) - redis_connection.mset.assert_called_with(pytest.KEY, "value", pytest.KEY_1, "random") + await redis._multi_set([(Keys.KEY, "value"), (Keys.KEY_1, "random")]) + redis_connection.mset.assert_called_with(Keys.KEY, "value", Keys.KEY_1, "random") @pytest.mark.asyncio async def test_multi_set_with_ttl(self, redis, redis_connection): - await redis._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")], ttl=1) + await redis._multi_set([(Keys.KEY, "value"), (Keys.KEY_1, "random")], ttl=1) assert redis_connection.multi_exec.call_count == 1 - redis_connection.mset.assert_called_with(pytest.KEY, "value", pytest.KEY_1, "random") - redis_connection.expire.assert_any_call(pytest.KEY, timeout=1) - redis_connection.expire.assert_any_call(pytest.KEY_1, timeout=1) + redis_connection.mset.assert_called_with(Keys.KEY, "value", Keys.KEY_1, "random") + redis_connection.expire.assert_any_call(Keys.KEY, timeout=1) + redis_connection.expire.assert_any_call(Keys.KEY_1, timeout=1) assert redis_connection.execute.call_count == 1 @pytest.mark.asyncio async def test_add(self, redis, redis_connection): - await redis._add(pytest.KEY, "value") - redis_connection.set.assert_called_with(pytest.KEY, "value", exist=ANY, expire=None) + await redis._add(Keys.KEY, "value") + redis_connection.set.assert_called_with(Keys.KEY, "value", exist=ANY, expire=None) - await redis._add(pytest.KEY, "value", 1) - redis_connection.set.assert_called_with(pytest.KEY, "value", exist=ANY, expire=1) + await redis._add(Keys.KEY, "value", 1) + redis_connection.set.assert_called_with(Keys.KEY, "value", exist=ANY, expire=1) @pytest.mark.asyncio async def test_add_existing(self, redis, redis_connection): redis_connection.set.return_value = False with pytest.raises(ValueError): - await redis._add(pytest.KEY, "value") + await redis._add(Keys.KEY, "value") @pytest.mark.asyncio async def test_add_float_ttl(self, redis, redis_connection): - await redis._add(pytest.KEY, "value", 0.1) - redis_connection.set.assert_called_with(pytest.KEY, "value", exist=ANY, pexpire=100) + await redis._add(Keys.KEY, "value", 0.1) + redis_connection.set.assert_called_with(Keys.KEY, "value", exist=ANY, pexpire=100) @pytest.mark.asyncio async def test_exists(self, redis, redis_connection): redis_connection.exists.return_value = 1 - await redis._exists(pytest.KEY) - redis_connection.exists.assert_called_with(pytest.KEY) + await redis._exists(Keys.KEY) + redis_connection.exists.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_expire(self, redis, redis_connection): - await redis._expire(pytest.KEY, ttl=1) - redis_connection.expire.assert_called_with(pytest.KEY, 1) + await redis._expire(Keys.KEY, ttl=1) + redis_connection.expire.assert_called_with(Keys.KEY, 1) @pytest.mark.asyncio async def test_increment(self, redis, redis_connection): - await redis._increment(pytest.KEY, delta=2) - redis_connection.incrby.assert_called_with(pytest.KEY, 2) + await redis._increment(Keys.KEY, delta=2) + redis_connection.incrby.assert_called_with(Keys.KEY, 2) @pytest.mark.asyncio async def test_increment_typerror(self, redis, redis_connection): redis_connection.incrby.side_effect = aioredis.errors.ReplyError("msg") with pytest.raises(TypeError): - await redis._increment(pytest.KEY, 2) + await redis._increment(Keys.KEY, 2) @pytest.mark.asyncio async def test_expire_0_ttl(self, redis, redis_connection): - await redis._expire(pytest.KEY, ttl=0) - redis_connection.persist.assert_called_with(pytest.KEY) + await redis._expire(Keys.KEY, ttl=0) + redis_connection.persist.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_delete(self, redis, redis_connection): - await redis._delete(pytest.KEY) - redis_connection.delete.assert_called_with(pytest.KEY) + await redis._delete(Keys.KEY) + redis_connection.delete.assert_called_with(Keys.KEY) @pytest.mark.asyncio async def test_clear(self, redis, redis_connection): @@ -270,20 +271,20 @@ async def test_clear_no_namespace(self, redis, redis_connection): @pytest.mark.asyncio async def test_raw(self, redis, redis_connection): - await redis._raw("get", pytest.KEY) - await redis._raw("set", pytest.KEY, 1) - redis_connection.get.assert_called_with(pytest.KEY, encoding=ANY) - redis_connection.set.assert_called_with(pytest.KEY, 1) + await redis._raw("get", Keys.KEY) + await redis._raw("set", Keys.KEY, 1) + redis_connection.get.assert_called_with(Keys.KEY, encoding=ANY) + redis_connection.set.assert_called_with(Keys.KEY, 1) @pytest.mark.asyncio async def test_redlock_release(self, mocker, redis): mocker.spy(redis, "_raw") - await redis._redlock_release(pytest.KEY, "random") - redis._raw.assert_called_with("eval", redis.RELEASE_SCRIPT, [pytest.KEY], ["random"]) + await redis._redlock_release(Keys.KEY, "random") + redis._raw.assert_called_with("eval", redis.RELEASE_SCRIPT, [Keys.KEY], ["random"]) @pytest.mark.asyncio async def test_close_when_connected(self, redis): - await redis._raw("set", pytest.KEY, 1) + await redis._raw("set", Keys.KEY, 1) await redis._close() assert redis._pool.clear.call_count == 1 @@ -339,10 +340,10 @@ def test_parse_uri_path(self, path, expected): @pytest.mark.parametrize( "namespace, expected", - ([None, "test:" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns:" + pytest.KEY]), + ([None, "test:" + Keys.KEY], ["", Keys.KEY], ["my_ns", "my_ns:" + Keys.KEY]), ) def test_build_key_double_dot(self, set_test_namespace, redis_cache, namespace, expected): - assert redis_cache.build_key(pytest.KEY, namespace=namespace) == expected + assert redis_cache.build_key(Keys.KEY, namespace=namespace) == expected def test_build_key_no_namespace(self, redis_cache): - assert redis_cache.build_key(pytest.KEY, namespace=None) == pytest.KEY + assert redis_cache.build_key(Keys.KEY, namespace=None) == Keys.KEY diff --git a/tests/ut/conftest.py b/tests/ut/conftest.py index 8c12c810..e10d947c 100644 --- a/tests/ut/conftest.py +++ b/tests/ut/conftest.py @@ -19,16 +19,6 @@ from aiocache.serializers import BaseSerializer -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.set_config( diff --git a/tests/ut/test_base.py b/tests/ut/test_base.py index 78249134..af0c72e0 100644 --- a/tests/ut/test_base.py +++ b/tests/ut/test_base.py @@ -3,6 +3,7 @@ from unittest.mock import ANY, AsyncMock, MagicMock, patch import pytest +from tests.utils import Keys from aiocache.base import API, BaseCache, _Conn @@ -155,47 +156,47 @@ def test_str_timeout(self): @pytest.mark.asyncio async def test_add(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._add(pytest.KEY, "value", 0) + await base_cache._add(Keys.KEY, "value", 0) @pytest.mark.asyncio async def test_get(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._get(pytest.KEY, "utf-8") + await base_cache._get(Keys.KEY, "utf-8") @pytest.mark.asyncio async def test_set(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._set(pytest.KEY, "value", 0) + await base_cache._set(Keys.KEY, "value", 0) @pytest.mark.asyncio async def test_multi_get(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._multi_get([pytest.KEY], encoding="utf-8") + await base_cache._multi_get([Keys.KEY], encoding="utf-8") @pytest.mark.asyncio async def test_multi_set(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._multi_set([(pytest.KEY, "value")], 0) + await base_cache._multi_set([(Keys.KEY, "value")], 0) @pytest.mark.asyncio async def test_delete(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._delete(pytest.KEY) + await base_cache._delete(Keys.KEY) @pytest.mark.asyncio async def test_exists(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._exists(pytest.KEY) + await base_cache._exists(Keys.KEY) @pytest.mark.asyncio async def test_increment(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._increment(pytest.KEY, 2) + await base_cache._increment(Keys.KEY, 2) @pytest.mark.asyncio async def test_expire(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._expire(pytest.KEY, 0) + await base_cache._expire(Keys.KEY, 0) @pytest.mark.asyncio async def test_clear(self, base_cache): @@ -205,7 +206,7 @@ async def test_clear(self, base_cache): @pytest.mark.asyncio async def test_raw(self, base_cache): with pytest.raises(NotImplementedError): - await base_cache._raw("get", pytest.KEY) + await base_cache._raw("get", Keys.KEY) @pytest.mark.asyncio async def test_close(self, base_cache): @@ -227,58 +228,58 @@ def set_test_namespace(self, base_cache): @pytest.mark.parametrize( "namespace, expected", - ([None, "test" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns" + pytest.KEY]), # type: ignore[attr-defined] # noqa: B950 + ([None, "test" + Keys.KEY], ["", Keys.KEY], ["my_ns", "my_ns" + Keys.KEY]), # type: ignore[attr-defined] # noqa: B950 ) def test_build_key(self, set_test_namespace, base_cache, namespace, expected): - assert base_cache.build_key(pytest.KEY, namespace=namespace) == expected + assert base_cache.build_key(Keys.KEY, namespace=namespace) == expected def test_alt_build_key(self): cache = BaseCache(key_builder=lambda key, namespace: "x") - assert cache.build_key(pytest.KEY, "namespace") == "x" + assert cache.build_key(Keys.KEY, "namespace") == "x" @pytest.mark.asyncio async def test_add_ttl_cache_default(self, base_cache): base_cache._add = AsyncMock() - await base_cache.add(pytest.KEY, "value") + await base_cache.add(Keys.KEY, "value") - base_cache._add.assert_called_once_with(pytest.KEY, "value", _conn=None, ttl=None) + base_cache._add.assert_called_once_with(Keys.KEY, "value", _conn=None, ttl=None) @pytest.mark.asyncio async def test_add_ttl_default(self, base_cache): base_cache.ttl = 10 base_cache._add = AsyncMock() - await base_cache.add(pytest.KEY, "value") + await base_cache.add(Keys.KEY, "value") - base_cache._add.assert_called_once_with(pytest.KEY, "value", _conn=None, ttl=10) + base_cache._add.assert_called_once_with(Keys.KEY, "value", _conn=None, ttl=10) @pytest.mark.asyncio async def test_add_ttl_overriden(self, base_cache): base_cache.ttl = 10 base_cache._add = AsyncMock() - await base_cache.add(pytest.KEY, "value", ttl=20) + await base_cache.add(Keys.KEY, "value", ttl=20) - base_cache._add.assert_called_once_with(pytest.KEY, "value", _conn=None, ttl=20) + base_cache._add.assert_called_once_with(Keys.KEY, "value", _conn=None, ttl=20) @pytest.mark.asyncio async def test_add_ttl_none(self, base_cache): base_cache.ttl = 10 base_cache._add = AsyncMock() - await base_cache.add(pytest.KEY, "value", ttl=None) + await base_cache.add(Keys.KEY, "value", ttl=None) - base_cache._add.assert_called_once_with(pytest.KEY, "value", _conn=None, ttl=None) + base_cache._add.assert_called_once_with(Keys.KEY, "value", _conn=None, ttl=None) @pytest.mark.asyncio async def test_set_ttl_cache_default(self, base_cache): base_cache._set = AsyncMock() - await base_cache.set(pytest.KEY, "value") + await base_cache.set(Keys.KEY, "value") base_cache._set.assert_called_once_with( - pytest.KEY, "value", _cas_token=None, _conn=None, ttl=None + Keys.KEY, "value", _cas_token=None, _conn=None, ttl=None ) @pytest.mark.asyncio @@ -286,10 +287,10 @@ async def test_set_ttl_default(self, base_cache): base_cache.ttl = 10 base_cache._set = AsyncMock() - await base_cache.set(pytest.KEY, "value") + await base_cache.set(Keys.KEY, "value") base_cache._set.assert_called_once_with( - pytest.KEY, "value", _cas_token=None, _conn=None, ttl=10 + Keys.KEY, "value", _cas_token=None, _conn=None, ttl=10 ) @pytest.mark.asyncio @@ -297,10 +298,10 @@ async def test_set_ttl_overriden(self, base_cache): base_cache.ttl = 10 base_cache._set = AsyncMock() - await base_cache.set(pytest.KEY, "value", ttl=20) + await base_cache.set(Keys.KEY, "value", ttl=20) base_cache._set.assert_called_once_with( - pytest.KEY, "value", _cas_token=None, _conn=None, ttl=20 + Keys.KEY, "value", _cas_token=None, _conn=None, ttl=20 ) @pytest.mark.asyncio @@ -308,20 +309,20 @@ async def test_set_ttl_none(self, base_cache): base_cache.ttl = 10 base_cache._set = AsyncMock() - await base_cache.set(pytest.KEY, "value", ttl=None) + await base_cache.set(Keys.KEY, "value", ttl=None) base_cache._set.assert_called_once_with( - pytest.KEY, "value", _cas_token=None, _conn=None, ttl=None + Keys.KEY, "value", _cas_token=None, _conn=None, ttl=None ) @pytest.mark.asyncio async def test_multi_set_ttl_cache_default(self, base_cache): base_cache._multi_set = AsyncMock() - await base_cache.multi_set([[pytest.KEY, "value"], [pytest.KEY_1, "value1"]]) + await base_cache.multi_set([[Keys.KEY, "value"], [Keys.KEY_1, "value1"]]) base_cache._multi_set.assert_called_once_with( - [(pytest.KEY, "value"), (pytest.KEY_1, "value1")], _conn=None, ttl=None + [(Keys.KEY, "value"), (Keys.KEY_1, "value1")], _conn=None, ttl=None ) @pytest.mark.asyncio @@ -329,10 +330,10 @@ async def test_multi_set_ttl_default(self, base_cache): base_cache.ttl = 10 base_cache._multi_set = AsyncMock() - await base_cache.multi_set([[pytest.KEY, "value"], [pytest.KEY_1, "value1"]]) + await base_cache.multi_set([[Keys.KEY, "value"], [Keys.KEY_1, "value1"]]) base_cache._multi_set.assert_called_once_with( - [(pytest.KEY, "value"), (pytest.KEY_1, "value1")], _conn=None, ttl=10 + [(Keys.KEY, "value"), (Keys.KEY_1, "value1")], _conn=None, ttl=10 ) @pytest.mark.asyncio @@ -340,10 +341,10 @@ async def test_multi_set_ttl_overriden(self, base_cache): base_cache.ttl = 10 base_cache._multi_set = AsyncMock() - await base_cache.multi_set([[pytest.KEY, "value"], [pytest.KEY_1, "value1"]], ttl=20) + await base_cache.multi_set([[Keys.KEY, "value"], [Keys.KEY_1, "value1"]], ttl=20) base_cache._multi_set.assert_called_once_with( - [(pytest.KEY, "value"), (pytest.KEY_1, "value1")], _conn=None, ttl=20 + [(Keys.KEY, "value"), (Keys.KEY_1, "value1")], _conn=None, ttl=20 ) @pytest.mark.asyncio @@ -351,10 +352,10 @@ async def test_multi_set_ttl_none(self, base_cache): base_cache.ttl = 10 base_cache._multi_set = AsyncMock() - await base_cache.multi_set([[pytest.KEY, "value"], [pytest.KEY_1, "value1"]], ttl=None) + await base_cache.multi_set([[Keys.KEY, "value"], [Keys.KEY_1, "value1"]], ttl=None) base_cache._multi_set.assert_called_once_with( - [(pytest.KEY, "value"), (pytest.KEY_1, "value1")], _conn=None, ttl=None + [(Keys.KEY, "value"), (Keys.KEY_1, "value1")], _conn=None, ttl=None ) @@ -373,10 +374,10 @@ async def asleep(self, *args, **kwargs): @pytest.mark.asyncio async def test_get(self, mock_cache): - await mock_cache.get(pytest.KEY) + await mock_cache.get(Keys.KEY) mock_cache._get.assert_called_with( - mock_cache._build_key(pytest.KEY), encoding=ANY, _conn=ANY + mock_cache._build_key(Keys.KEY), encoding=ANY, _conn=ANY ) assert mock_cache.plugins[0].pre_get.call_count == 1 assert mock_cache.plugins[0].post_get.call_count == 1 @@ -386,26 +387,26 @@ async def test_get_timeouts(self, mock_cache): mock_cache._get = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.get(pytest.KEY) + await mock_cache.get(Keys.KEY) @pytest.mark.asyncio async def test_get_default(self, mock_cache): mock_cache._serializer.loads.return_value = None - assert await mock_cache.get(pytest.KEY, default=1) == 1 + assert await mock_cache.get(Keys.KEY, default=1) == 1 @pytest.mark.asyncio async def test_get_negative_default(self, mock_cache): mock_cache._serializer.loads.return_value = False - assert await mock_cache.get(pytest.KEY) is False + assert await mock_cache.get(Keys.KEY) is False @pytest.mark.asyncio async def test_set(self, mock_cache): - await mock_cache.set(pytest.KEY, "value", ttl=2) + await mock_cache.set(Keys.KEY, "value", ttl=2) mock_cache._set.assert_called_with( - mock_cache._build_key(pytest.KEY), ANY, ttl=2, _cas_token=None, _conn=ANY + mock_cache._build_key(Keys.KEY), ANY, ttl=2, _cas_token=None, _conn=ANY ) assert mock_cache.plugins[0].pre_set.call_count == 1 assert mock_cache.plugins[0].post_set.call_count == 1 @@ -415,14 +416,14 @@ async def test_set_timeouts(self, mock_cache): mock_cache._set = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.set(pytest.KEY, "value") + await mock_cache.set(Keys.KEY, "value") @pytest.mark.asyncio async def test_add(self, mock_cache): mock_cache._exists = AsyncMock(return_value=False) - await mock_cache.add(pytest.KEY, "value", ttl=2) + await mock_cache.add(Keys.KEY, "value", ttl=2) - key = mock_cache._build_key(pytest.KEY) + key = mock_cache._build_key(Keys.KEY) mock_cache._add.assert_called_with(key, ANY, ttl=2, _conn=ANY) assert mock_cache.plugins[0].pre_add.call_count == 1 assert mock_cache.plugins[0].post_add.call_count == 1 @@ -432,14 +433,14 @@ async def test_add_timeouts(self, mock_cache): mock_cache._add = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.add(pytest.KEY, "value") + await mock_cache.add(Keys.KEY, "value") @pytest.mark.asyncio async def test_mget(self, mock_cache): - await mock_cache.multi_get([pytest.KEY, pytest.KEY_1]) + await mock_cache.multi_get([Keys.KEY, Keys.KEY_1]) mock_cache._multi_get.assert_called_with( - [mock_cache._build_key(pytest.KEY), mock_cache._build_key(pytest.KEY_1)], + [mock_cache._build_key(Keys.KEY), mock_cache._build_key(Keys.KEY_1)], encoding=ANY, _conn=ANY, ) @@ -451,14 +452,14 @@ async def test_mget_timeouts(self, mock_cache): mock_cache._multi_get = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.multi_get(pytest.KEY, "value") + await mock_cache.multi_get(Keys.KEY, "value") @pytest.mark.asyncio async def test_mset(self, mock_cache): - await mock_cache.multi_set([[pytest.KEY, "value"], [pytest.KEY_1, "value1"]], ttl=2) + await mock_cache.multi_set([[Keys.KEY, "value"], [Keys.KEY_1, "value1"]], ttl=2) mock_cache._multi_set.assert_called_with( - [(mock_cache._build_key(pytest.KEY), ANY), (mock_cache._build_key(pytest.KEY_1), ANY)], + [(mock_cache._build_key(Keys.KEY), ANY), (mock_cache._build_key(Keys.KEY_1), ANY)], ttl=2, _conn=ANY, ) @@ -470,13 +471,13 @@ async def test_mset_timeouts(self, mock_cache): mock_cache._multi_set = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.multi_set([[pytest.KEY, "value"], [pytest.KEY_1, "value1"]]) + await mock_cache.multi_set([[Keys.KEY, "value"], [Keys.KEY_1, "value1"]]) @pytest.mark.asyncio async def test_exists(self, mock_cache): - await mock_cache.exists(pytest.KEY) + await mock_cache.exists(Keys.KEY) - mock_cache._exists.assert_called_with(mock_cache._build_key(pytest.KEY), _conn=ANY) + mock_cache._exists.assert_called_with(mock_cache._build_key(Keys.KEY), _conn=ANY) assert mock_cache.plugins[0].pre_exists.call_count == 1 assert mock_cache.plugins[0].post_exists.call_count == 1 @@ -485,13 +486,13 @@ async def test_exists_timeouts(self, mock_cache): mock_cache._exists = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.exists(pytest.KEY) + await mock_cache.exists(Keys.KEY) @pytest.mark.asyncio async def test_increment(self, mock_cache): - await mock_cache.increment(pytest.KEY, 2) + await mock_cache.increment(Keys.KEY, 2) - mock_cache._increment.assert_called_with(mock_cache._build_key(pytest.KEY), 2, _conn=ANY) + mock_cache._increment.assert_called_with(mock_cache._build_key(Keys.KEY), 2, _conn=ANY) assert mock_cache.plugins[0].pre_increment.call_count == 1 assert mock_cache.plugins[0].post_increment.call_count == 1 @@ -500,13 +501,13 @@ async def test_increment_timeouts(self, mock_cache): mock_cache._increment = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.increment(pytest.KEY) + await mock_cache.increment(Keys.KEY) @pytest.mark.asyncio async def test_delete(self, mock_cache): - await mock_cache.delete(pytest.KEY) + await mock_cache.delete(Keys.KEY) - mock_cache._delete.assert_called_with(mock_cache._build_key(pytest.KEY), _conn=ANY) + mock_cache._delete.assert_called_with(mock_cache._build_key(Keys.KEY), _conn=ANY) assert mock_cache.plugins[0].pre_delete.call_count == 1 assert mock_cache.plugins[0].post_delete.call_count == 1 @@ -515,12 +516,12 @@ async def test_delete_timeouts(self, mock_cache): mock_cache._delete = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.delete(pytest.KEY) + await mock_cache.delete(Keys.KEY) @pytest.mark.asyncio async def test_expire(self, mock_cache): - await mock_cache.expire(pytest.KEY, 1) - mock_cache._expire.assert_called_with(mock_cache._build_key(pytest.KEY), 1, _conn=ANY) + await mock_cache.expire(Keys.KEY, 1) + mock_cache._expire.assert_called_with(mock_cache._build_key(Keys.KEY), 1, _conn=ANY) assert mock_cache.plugins[0].pre_expire.call_count == 1 assert mock_cache.plugins[0].post_expire.call_count == 1 @@ -529,12 +530,12 @@ async def test_expire_timeouts(self, mock_cache): mock_cache._expire = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.expire(pytest.KEY, 0) + await mock_cache.expire(Keys.KEY, 0) @pytest.mark.asyncio async def test_clear(self, mock_cache): - await mock_cache.clear(pytest.KEY) - mock_cache._clear.assert_called_with(mock_cache._build_key(pytest.KEY), _conn=ANY) + await mock_cache.clear(Keys.KEY) + mock_cache._clear.assert_called_with(mock_cache._build_key(Keys.KEY), _conn=ANY) assert mock_cache.plugins[0].pre_clear.call_count == 1 assert mock_cache.plugins[0].post_clear.call_count == 1 @@ -543,13 +544,13 @@ async def test_clear_timeouts(self, mock_cache): mock_cache._clear = self.asleep with pytest.raises(asyncio.TimeoutError): - await mock_cache.clear(pytest.KEY) + await mock_cache.clear(Keys.KEY) @pytest.mark.asyncio async def test_raw(self, mock_cache): - await mock_cache.raw("get", pytest.KEY) + await mock_cache.raw("get", Keys.KEY) mock_cache._raw.assert_called_with( - "get", mock_cache._build_key(pytest.KEY), encoding=ANY, _conn=ANY + "get", mock_cache._build_key(Keys.KEY), encoding=ANY, _conn=ANY ) assert mock_cache.plugins[0].pre_raw.call_count == 1 assert mock_cache.plugins[0].post_raw.call_count == 1 diff --git a/tests/ut/test_lock.py b/tests/ut/test_lock.py index 8c3cc0a8..9b4ff64b 100644 --- a/tests/ut/test_lock.py +++ b/tests/ut/test_lock.py @@ -2,6 +2,7 @@ from unittest.mock import Mock, patch import pytest +from tests.utils import Keys from aiocache.lock import OptimisticLock, OptimisticLockError, RedLock @@ -10,35 +11,35 @@ class TestRedLock: @pytest.fixture def lock(self, mock_cache): RedLock._EVENTS = {} - yield RedLock(mock_cache, pytest.KEY, 20) + yield RedLock(mock_cache, Keys.KEY, 20) @pytest.mark.asyncio async def test_acquire(self, mock_cache, lock): await lock._acquire() - mock_cache._add.assert_called_with(pytest.KEY + "-lock", lock._value, ttl=20) - assert lock._EVENTS[pytest.KEY + "-lock"].is_set() is False + mock_cache._add.assert_called_with(Keys.KEY_LOCK, lock._value, ttl=20) + assert lock._EVENTS[Keys.KEY_LOCK].is_set() is False @pytest.mark.asyncio async def test_release(self, mock_cache, lock): mock_cache._redlock_release.return_value = True await lock._acquire() await lock._release() - mock_cache._redlock_release.assert_called_with(pytest.KEY + "-lock", lock._value) - assert pytest.KEY + "-lock" not in lock._EVENTS + mock_cache._redlock_release.assert_called_with(Keys.KEY_LOCK, lock._value) + assert Keys.KEY_LOCK not in lock._EVENTS @pytest.mark.asyncio async def test_release_no_acquire(self, mock_cache, lock): mock_cache._redlock_release.return_value = False - assert pytest.KEY + "-lock" not in lock._EVENTS + assert Keys.KEY_LOCK not in lock._EVENTS await lock._release() - assert pytest.KEY + "-lock" not in lock._EVENTS + assert Keys.KEY_LOCK not in lock._EVENTS @pytest.mark.asyncio async def test_context_manager(self, mock_cache, lock): async with lock: pass - mock_cache._add.assert_called_with(pytest.KEY + "-lock", lock._value, ttl=20) - mock_cache._redlock_release.assert_called_with(pytest.KEY + "-lock", lock._value) + mock_cache._add.assert_called_with(Keys.KEY_LOCK, lock._value, ttl=20) + mock_cache._redlock_release.assert_called_with(Keys.KEY_LOCK, lock._value) @pytest.mark.asyncio async def test_raises_exceptions(self, mock_cache, lock): @@ -64,35 +65,35 @@ async def test_wait_for_release_no_acquire(self, mock_cache, lock): @pytest.mark.asyncio async def test_multiple_locks_lock(self, mock_cache, lock): - lock_1 = RedLock(mock_cache, pytest.KEY, 20) - lock_2 = RedLock(mock_cache, pytest.KEY, 20) + lock_1 = RedLock(mock_cache, Keys.KEY, 20) + lock_2 = RedLock(mock_cache, Keys.KEY, 20) mock_cache._add.side_effect = [True, ValueError(), ValueError()] await lock._acquire() - event = lock._EVENTS[pytest.KEY + "-lock"] + event = lock._EVENTS[Keys.KEY_LOCK] - assert pytest.KEY + "-lock" in lock._EVENTS - assert pytest.KEY + "-lock" in lock_1._EVENTS - assert pytest.KEY + "-lock" in lock_2._EVENTS + assert Keys.KEY_LOCK in lock._EVENTS + assert Keys.KEY_LOCK in lock_1._EVENTS + assert Keys.KEY_LOCK in lock_2._EVENTS assert not event.is_set() await asyncio.gather(lock_1._acquire(), lock._release(), lock_2._acquire()) - assert pytest.KEY + "-lock" not in lock._EVENTS - assert pytest.KEY + "-lock" not in lock_1._EVENTS - assert pytest.KEY + "-lock" not in lock_2._EVENTS + assert Keys.KEY_LOCK not in lock._EVENTS + assert Keys.KEY_LOCK not in lock_1._EVENTS + assert Keys.KEY_LOCK not in lock_2._EVENTS assert event.is_set() class TestOptimisticLock: @pytest.fixture def lock(self, mock_cache): - yield OptimisticLock(mock_cache, pytest.KEY) + yield OptimisticLock(mock_cache, Keys.KEY) def test_init(self, mock_cache, lock): assert lock.client == mock_cache assert lock._token is None - assert lock.key == pytest.KEY - assert lock.ns_key == mock_cache._build_key(pytest.KEY) + assert lock.key == Keys.KEY + assert lock.ns_key == mock_cache._build_key(Keys.KEY) @pytest.mark.asyncio async def test_aenter_returns_lock(self, lock): @@ -106,14 +107,14 @@ async def test_aexit_not_crashing(self, lock): @pytest.mark.asyncio async def test_acquire_calls_get(self, lock): await lock._acquire() - lock.client._gets.assert_called_with(pytest.KEY) + lock.client._gets.assert_called_with(Keys.KEY) assert lock._token == lock.client._gets.return_value @pytest.mark.asyncio async def test_cas_calls_set_with_token(self, lock): await lock._acquire() await lock.cas("value") - lock.client.set.assert_called_with(pytest.KEY, "value", _cas_token=lock._token) + lock.client.set.assert_called_with(Keys.KEY, "value", _cas_token=lock._token) @pytest.mark.asyncio async def test_wrong_token_raises_error(self, mock_cache, lock): diff --git a/tests/ut/test_plugins.py b/tests/ut/test_plugins.py index 5883d905..814b638c 100644 --- a/tests/ut/test_plugins.py +++ b/tests/ut/test_plugins.py @@ -1,6 +1,7 @@ from unittest.mock import MagicMock import pytest +from tests.utils import Keys from aiocache.base import API, BaseCache from aiocache.plugins import BasePlugin, HitMissRatioPlugin, TimingPlugin @@ -57,13 +58,13 @@ def plugin(self): @pytest.mark.asyncio async def test_post_get(self, plugin): client = MagicMock(spec=BaseCache) - await plugin.post_get(client, pytest.KEY) + await plugin.post_get(client, Keys.KEY) assert client.hit_miss_ratio["hits"] == 0 assert client.hit_miss_ratio["total"] == 1 assert client.hit_miss_ratio["hit_ratio"] == 0 - await plugin.post_get(client, pytest.KEY, ret="value") + await plugin.post_get(client, Keys.KEY, ret="value") assert client.hit_miss_ratio["hits"] == 1 assert client.hit_miss_ratio["total"] == 2 assert client.hit_miss_ratio["hit_ratio"] == 0.5 @@ -71,13 +72,13 @@ async def test_post_get(self, plugin): @pytest.mark.asyncio async def test_post_multi_get(self, plugin): client = MagicMock(spec=BaseCache) - await plugin.post_multi_get(client, [pytest.KEY, pytest.KEY_1], ret=[None, None]) + await plugin.post_multi_get(client, [Keys.KEY, Keys.KEY_1], ret=[None, None]) assert client.hit_miss_ratio["hits"] == 0 assert client.hit_miss_ratio["total"] == 2 assert client.hit_miss_ratio["hit_ratio"] == 0 - await plugin.post_multi_get(client, [pytest.KEY, pytest.KEY_1], ret=["value", "random"]) + await plugin.post_multi_get(client, [Keys.KEY, Keys.KEY_1], ret=["value", "random"]) assert client.hit_miss_ratio["hits"] == 2 assert client.hit_miss_ratio["total"] == 4 assert client.hit_miss_ratio["hit_ratio"] == 0.5 diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..c62f77ba --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,7 @@ +from enum import Enum + + +class Keys(str, Enum): + KEY: str = "key" + KEY_1: str = "random" + KEY_LOCK: str = "key-lock"