Skip to content

Commit

Permalink
Make aiomcache and redis deps optional for unit tests [WIP] (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorny authored and Dreamsorcerer committed Jun 22, 2023
1 parent 37ce464 commit 89e779b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 19 deletions.
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ universal=1
max-line-length=100

[tool:pytest]
addopts = --cov=aiocache --cov=tests/ --cov-report term
addopts = --cov=aiocache --cov=tests/ --cov-report term --strict-markers
asyncio_mode = auto
junit_suite_name = aiohttp_test_suite
filterwarnings=
error
testpaths = tests/
junit_family=xunit2
xfail_strict = true
markers =
memcached: tests requiring memcached backend
redis: tests requiring redis backend

[coverage:run]
branch = True
Expand Down
7 changes: 6 additions & 1 deletion tests/acceptance/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ async def memcached_cache():
await asyncio.gather(*(cache.delete(k) for k in (*Keys, KEY_LOCK)))


@pytest.fixture(params=("redis_cache", "memory_cache", "memcached_cache"))
@pytest.fixture(
params=(
pytest.param("redis_cache", marks=pytest.mark.redis),
"memory_cache",
pytest.param("memcached_cache", marks=pytest.mark.memcached),
))
def cache(request):
return request.getfixturevalue(request.param)
8 changes: 6 additions & 2 deletions tests/acceptance/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import pytest

from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.memory import SimpleMemoryCache
from aiocache.backends.redis import RedisCache
from aiocache.base import _Conn
from ..utils import Keys

Expand Down Expand Up @@ -171,8 +169,11 @@ async def test_clear_with_namespace_memory(self, memory_cache):
assert await memory_cache.exists(Keys.KEY, namespace="test") is False


@pytest.mark.memcached
class TestMemcachedCache:
async def test_accept_explicit_args(self):
from aiocache.backends.memcached import MemcachedCache

with pytest.raises(TypeError):
MemcachedCache(random_attr="wtf")

Expand Down Expand Up @@ -212,8 +213,11 @@ async def test_close(self, memcached_cache):
assert memcached_cache.client._pool._pool.qsize() == 0


@pytest.mark.redis
class TestRedisCache:
async def test_accept_explicit_args(self):
from aiocache.backends.redis import RedisCache

with pytest.raises(TypeError):
RedisCache(random_attr="wtf")

Expand Down
15 changes: 12 additions & 3 deletions tests/acceptance/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pytest

from aiocache import Cache
from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.memory import SimpleMemoryCache
from aiocache.backends.redis import RedisCache


class TestCache:
Expand All @@ -15,7 +13,10 @@ def test_from_url_memory_no_endpoint(self):
with pytest.raises(TypeError):
Cache.from_url("memory://endpoint:10")

@pytest.mark.redis
async def test_from_url_redis(self):
from aiocache.backends.redis import RedisCache

url = ("redis://endpoint:1000/0/?password=pass"
+ "&pool_max_size=50&create_connection_timeout=20")

Expand All @@ -27,7 +28,10 @@ async def test_from_url_redis(self):
assert cache.pool_max_size == 50
assert cache.create_connection_timeout == 20

@pytest.mark.memcached
async def test_from_url_memcached(self):
from aiocache.backends.memcached import MemcachedCache

url = "memcached://endpoint:1000?pool_size=10"

async with Cache.from_url(url) as cache:
Expand All @@ -36,7 +40,12 @@ async def test_from_url_memcached(self):
assert cache.port == 1000
assert cache.pool_size == 10

@pytest.mark.parametrize("scheme", ("memory", "redis", "memcached"))
@pytest.mark.parametrize(
"scheme",
(pytest.param("redis", marks=pytest.mark.redis),
"memory",
pytest.param("memcached", marks=pytest.mark.memcached),
))
def test_from_url_unexpected_param(self, scheme):
with pytest.raises(TypeError):
Cache.from_url("{}://?arg1=arg1".format(scheme))
3 changes: 3 additions & 0 deletions tests/acceptance/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async def test_float_lease(self, memory_cache):
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None


@pytest.mark.redis
class TestRedisRedLock:
@pytest.fixture
def lock(self, redis_cache):
Expand Down Expand Up @@ -160,6 +161,7 @@ async def test_float_lease(self, redis_cache):
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None


@pytest.mark.memcached
class TestMemcachedRedLock:
@pytest.fixture
def lock(self, memcached_cache):
Expand Down Expand Up @@ -255,6 +257,7 @@ async def test_check_and_set_with_float_ttl(self, memory_cache, lock):
assert await memory_cache.get(Keys.KEY) is None


@pytest.mark.redis
class TestRedisOptimisticLock:
@pytest.fixture
def lock(self, redis_cache):
Expand Down
6 changes: 4 additions & 2 deletions tests/ut/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import pytest

from aiocache import caches
from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.redis import RedisCache
from aiocache.base import BaseCache
from aiocache.plugins import BasePlugin

Expand Down Expand Up @@ -54,11 +52,15 @@ def base_cache():

@pytest.fixture
async def redis_cache():
from aiocache.backends.redis import RedisCache

async with RedisCache() as cache:
yield cache


@pytest.fixture
async def memcached_cache():
from aiocache.backends.memcached import MemcachedCache

async with MemcachedCache() as cache:
yield cache
40 changes: 30 additions & 10 deletions tests/ut/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,38 @@
import pytest

from aiocache import AIOCACHE_CACHES, Cache, caches
from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.memory import SimpleMemoryCache
from aiocache.backends.redis import RedisCache
from aiocache.exceptions import InvalidCacheType
from aiocache.factory import _class_from_string, _create_cache
from aiocache.plugins import HitMissRatioPlugin, TimingPlugin
from aiocache.serializers import JsonSerializer, PickleSerializer

assert Cache.REDIS is not None
assert Cache.MEMCACHED is not None
CACHE_NAMES = (Cache.MEMORY.NAME, Cache.REDIS.NAME, Cache.MEMCACHED.NAME)

CACHE_NAMES = [Cache.MEMORY.NAME]

try:
from aiocache.backends.memcached import MemcachedCache
except ImportError:
MemcachedCache = None
else:
assert Cache.MEMCACHED is not None
CACHE_NAMES.append(Cache.MEMCACHED.NAME)

try:
from aiocache.backends.redis import RedisCache
except ImportError:
RedisCache = None
else:
assert Cache.REDIS is not None
CACHE_NAMES.append(Cache.REDIS.NAME)


@pytest.mark.redis
def test_class_from_string():
assert _class_from_string("aiocache.RedisCache") == RedisCache


@pytest.mark.redis
def test_create_simple_cache():
redis = _create_cache(RedisCache, endpoint="127.0.0.10", port=6378)

Expand All @@ -29,15 +44,15 @@ def test_create_simple_cache():


def test_create_cache_with_everything():
redis = _create_cache(
RedisCache,
cache = _create_cache(
SimpleMemoryCache,
serializer={"class": PickleSerializer, "encoding": "encoding"},
plugins=[{"class": "aiocache.plugins.TimingPlugin"}],
)

assert isinstance(redis.serializer, PickleSerializer)
assert redis.serializer.encoding == "encoding"
assert isinstance(redis.plugins[0], TimingPlugin)
assert isinstance(cache.serializer, PickleSerializer)
assert cache.serializer.encoding == "encoding"
assert isinstance(cache.plugins[0], TimingPlugin)


class TestCache:
Expand Down Expand Up @@ -164,6 +179,7 @@ def test_reuse_instance(self):
def test_create_not_reuse(self):
assert caches.create("default") is not caches.create("default")

@pytest.mark.redis
def test_create_extra_args(self):
caches.set_config(
{
Expand All @@ -180,6 +196,7 @@ def test_create_extra_args(self):
assert cache.endpoint == "127.0.0.10"
assert cache.db == 10

@pytest.mark.redis
def test_retrieve_cache(self):
caches.set_config(
{
Expand Down Expand Up @@ -209,6 +226,7 @@ def test_retrieve_cache(self):
assert cache.serializer.encoding == "encoding"
assert len(cache.plugins) == 2

@pytest.mark.redis
def test_retrieve_cache_new_instance(self):
caches.set_config(
{
Expand Down Expand Up @@ -236,6 +254,7 @@ def test_retrieve_cache_new_instance(self):
assert cache.serializer.encoding == "encoding"
assert len(cache.plugins) == 2

@pytest.mark.redis
def test_multiple_caches(self):
caches.set_config(
{
Expand Down Expand Up @@ -330,6 +349,7 @@ def test_set_config_no_default(self):
}
)

@pytest.mark.redis
def test_ensure_plugins_order(self):
caches.set_config(
{
Expand Down

0 comments on commit 89e779b

Please sign in to comment.