-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added testing for cached decorator and fixed bug
- Loading branch information
Showing
7 changed files
with
89 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,60 @@ | ||
import pytest | ||
import aiocache | ||
import asyncio | ||
|
||
from aiocache import RedisCache, SimpleMemoryCache | ||
from aiocache import RedisCache, SimpleMemoryCache, cached | ||
from aiocache.utils import get_default_cache | ||
from aiocache.serializers import PickleSerializer, DefaultSerializer | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_default_cache_with_backend(): | ||
class TestCachedDecorator: | ||
|
||
cache = get_default_cache( | ||
backend=RedisCache, namespace="test", endpoint="http://...", | ||
port=6379, serializer=PickleSerializer()) | ||
@pytest.mark.asyncio | ||
async def test_cached_no_args(self, mocker): | ||
mocker.spy(asyncio, 'sleep') | ||
cached_decorator = cached(ttl=10) | ||
|
||
assert isinstance(cache, RedisCache) | ||
assert cache.namespace == "test" | ||
assert cache.endpoint == "http://..." | ||
assert cache.port == 6379 | ||
assert isinstance(cache.serializer, PickleSerializer) | ||
resp1 = await cached_decorator(asyncio.sleep)(1) | ||
resp2 = await cached_decorator(asyncio.sleep)(1) | ||
|
||
assert asyncio.sleep.call_count == 1 | ||
assert resp1 is resp2 | ||
|
||
@pytest.mark.asyncio | ||
async def test_default_cache_with_global(): | ||
aiocache.config_default_cache( | ||
backend=RedisCache, namespace="test", endpoint="http://...", | ||
port=6379, serializer=PickleSerializer()) | ||
cache = get_default_cache() | ||
|
||
assert isinstance(cache, RedisCache) | ||
assert cache.namespace == "test" | ||
assert cache.endpoint == "http://..." | ||
assert cache.port == 6379 | ||
assert isinstance(cache.serializer, PickleSerializer) | ||
class TestDefaultCache: | ||
|
||
aiocache.default_cache = None | ||
@pytest.mark.asyncio | ||
async def test_default_cache_with_backend(self): | ||
|
||
cache = get_default_cache( | ||
backend=RedisCache, namespace="test", endpoint="http://...", | ||
port=6379, serializer=PickleSerializer()) | ||
|
||
@pytest.mark.asyncio | ||
async def test_default_cache_with_default(): | ||
cache = get_default_cache(namespace="test") | ||
assert isinstance(cache, RedisCache) | ||
assert cache.namespace == "test" | ||
assert cache.endpoint == "http://..." | ||
assert cache.port == 6379 | ||
assert isinstance(cache.serializer, PickleSerializer) | ||
|
||
assert isinstance(cache, SimpleMemoryCache) | ||
assert cache.namespace == "test" | ||
assert isinstance(cache.serializer, DefaultSerializer) | ||
@pytest.mark.asyncio | ||
async def test_default_cache_with_global(self): | ||
aiocache.config_default_cache( | ||
backend=RedisCache, namespace="test", endpoint="http://...", | ||
port=6379, serializer=PickleSerializer()) | ||
cache = get_default_cache() | ||
|
||
assert isinstance(cache, RedisCache) | ||
assert cache.namespace == "test" | ||
assert cache.endpoint == "http://..." | ||
assert cache.port == 6379 | ||
assert isinstance(cache.serializer, PickleSerializer) | ||
|
||
aiocache.default_cache = None | ||
|
||
@pytest.mark.asyncio | ||
async def test_default_cache_with_default(self): | ||
cache = get_default_cache(namespace="test") | ||
|
||
assert isinstance(cache, SimpleMemoryCache) | ||
assert cache.namespace == "test" | ||
assert isinstance(cache.serializer, DefaultSerializer) |