diff --git a/aiocache/backends/memcached.py b/aiocache/backends/memcached.py index 5bda4928..65a5a8a6 100644 --- a/aiocache/backends/memcached.py +++ b/aiocache/backends/memcached.py @@ -44,8 +44,8 @@ async def _set(self, key, value, ttl=0, _cas_token=None, _conn=None): return await self._cas(key, value, _cas_token, ttl=ttl, _conn=_conn) try: return await self.client.set(key, value, exptime=ttl or 0) - except aiomcache.exceptions.ValidationException: - raise TypeError("memcached doesn't support float ttl") + except aiomcache.exceptions.ValidationException as e: + raise TypeError('aiomcache error: {}'.format(str(e))) async def _cas(self, key, value, token, ttl=None, _conn=None): return await self.client.cas(key, value, token, exptime=ttl or 0) @@ -58,8 +58,8 @@ async def _multi_set(self, pairs, ttl=0, _conn=None): try: await asyncio.gather(*tasks) - except aiomcache.exceptions.ValidationException: - raise TypeError("memcached doesn't support float ttl") + except aiomcache.exceptions.ValidationException as e: + raise TypeError('aiomcache error: {}'.format(str(e))) return True @@ -67,8 +67,8 @@ async def _add(self, key, value, ttl=0, _conn=None): value = str.encode(value) if isinstance(value, str) else value try: ret = await self.client.add(key, value, exptime=ttl or 0) - except aiomcache.exceptions.ValidationException: - raise TypeError("memcached doesn't support float ttl") + except aiomcache.exceptions.ValidationException as e: + raise TypeError('aiomcache error: {}'.format(str(e))) if not ret: raise ValueError( "Key {} already exists, use .set to update the value".format(key)) @@ -89,7 +89,7 @@ async def _increment(self, key, delta, _conn=None): if "NOT_FOUND" in str(e): await self._set(key, str(delta).encode()) else: - raise TypeError("Value is not an integer") from None + raise TypeError('aiomcache error: {}'.format(str(e))) return incremented or delta diff --git a/tests/acceptance/test_base.py b/tests/acceptance/test_base.py index 2f835c09..e50d858e 100644 --- a/tests/acceptance/test_base.py +++ b/tests/acceptance/test_base.py @@ -186,16 +186,24 @@ async def test_accept_explicit_args(self): with pytest.raises(TypeError): MemcachedCache(random_attr="wtf") + @pytest.mark.asyncio + async def test_set_too_long_key(self, memcached_cache): + with pytest.raises(TypeError) as exc_info: + await memcached_cache.set('a' * 2000, 'value') + assert str(exc_info.value).startswith('aiomcache error: invalid key') + @pytest.mark.asyncio async def test_set_float_ttl_fails(self, memcached_cache): - with pytest.raises(TypeError): - await memcached_cache.set(pytest.KEY, "value", ttl=0.1) + with pytest.raises(TypeError) as exc_info: + await memcached_cache.set(pytest.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): - pairs = [(pytest.KEY, b"value"), [pytest.KEY_1, b"random_value"]] + with pytest.raises(TypeError) as exc_info: + pairs = [(pytest.KEY, b'value'), [pytest.KEY_1, b'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' @pytest.mark.asyncio async def test_raw(self, memcached_cache): diff --git a/tests/ut/backends/test_memcached.py b/tests/ut/backends/test_memcached.py index d09502dd..101f51c6 100644 --- a/tests/ut/backends/test_memcached.py +++ b/tests/ut/backends/test_memcached.py @@ -76,8 +76,9 @@ async def test_set(self, memcached): @pytest.mark.asyncio async def test_set_float_ttl(self, memcached): memcached.client.set.side_effect = aiomcache.exceptions.ValidationException("msg") - with pytest.raises(TypeError): - await memcached._set(pytest.KEY, "value", ttl=0.1) + with pytest.raises(TypeError) as exc_info: + await memcached._set(pytest.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): @@ -132,8 +133,9 @@ async def test_multi_set(self, memcached): @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): + with pytest.raises(TypeError) as exc_info: await memcached._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")], ttl=0.1) + assert str(exc_info.value) == 'aiomcache error: msg' @pytest.mark.asyncio async def test_add(self, memcached): @@ -152,8 +154,9 @@ async def test_add_existing(self, memcached): @pytest.mark.asyncio async def test_add_float_ttl(self, memcached): memcached.client.add.side_effect = aiomcache.exceptions.ValidationException("msg") - with pytest.raises(TypeError): + with pytest.raises(TypeError) as exc_info: await memcached._add(pytest.KEY, "value", 0.1) + assert str(exc_info.value) == 'aiomcache error: msg' @pytest.mark.asyncio async def test_exists(self, memcached): @@ -186,9 +189,10 @@ async def test_increment_missing_negative(self, memcached): @pytest.mark.asyncio async def test_increment_typerror(self, memcached): - memcached.client.incr.side_effect = aiomcache.exceptions.ClientException("") - with pytest.raises(TypeError): + memcached.client.incr.side_effect = aiomcache.exceptions.ClientException('msg') + with pytest.raises(TypeError) as exc_info: await memcached._increment(pytest.KEY, 2) + assert str(exc_info.value) == 'aiomcache error: msg' @pytest.mark.asyncio async def test_expire(self, memcached): diff --git a/tests/ut/conftest.py b/tests/ut/conftest.py index bf2bd827..4140dfac 100644 --- a/tests/ut/conftest.py +++ b/tests/ut/conftest.py @@ -26,11 +26,6 @@ def reset_caches(): }) -@pytest.fixture(autouse=True) -def disable_logs(mocker): - mocker.patch("logging.getLogger") - - class MockCache(BaseCache): def __init__(self):