Skip to content

Commit

Permalink
Syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Miranda committed Jan 4, 2019
1 parent 78f71da commit fb181e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 0 additions & 2 deletions aiocache/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@


class InvalidCacheType(Exception):
pass
16 changes: 8 additions & 8 deletions aiocache/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ def _create_cache(cache, serializer=None, plugins=None, **kwargs):

class Cache:

MEMORY = 'memory'
REDIS = 'redis'
MEMCACHED = 'memcached'
MEMORY = "memory"
REDIS = "redis"
MEMCACHED = "memcached"

_PROTOCOL_MAPPING = {
'memory': SimpleMemoryCache,
'redis': RedisCache,
'memcached': MemcachedCache,
"memory": SimpleMemoryCache,
"redis": RedisCache,
"memcached": MemcachedCache,
}

def __new__(cls, cache_type=MEMORY, **kwargs):
try:
cache_class = cls.get_protocol_class(cache_type)
except KeyError as e:
raise InvalidCacheType(
'Invalid cache type, you can only use {}'.format(
list(cls._PROTOCOL_MAPPING.keys()))) from e
"Invalid cache type, you can only use {}".format(list(cls._PROTOCOL_MAPPING.keys()))
) from e

instance = cache_class.__new__(cache_class, **kwargs)
instance.__init__(**kwargs)
Expand Down
15 changes: 8 additions & 7 deletions tests/ut/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test_create_cache_with_everything():

class TestCache:
def test_cache_types(self):
assert Cache.MEMORY == 'memory'
assert Cache.REDIS == 'redis'
assert Cache.MEMCACHED == 'memcached'
assert Cache.MEMORY == "memory"
assert Cache.REDIS == "redis"
assert Cache.MEMCACHED == "memcached"

@pytest.mark.parametrize("cache_type", [Cache.MEMORY, Cache.REDIS, Cache.MEMCACHED])
def test_new(self, cache_type):
Expand All @@ -53,17 +53,18 @@ def test_new_defaults_to_memory(self):

def test_new_invalid_cache_raises(self):
with pytest.raises(InvalidCacheType) as e:
Cache('file')
assert str(e.value) == \
"Invalid cache type, you can only use ['memory', 'redis', 'memcached']"
Cache("file")
assert str(e.value) == "Invalid cache type, you can only use {}".format(
list(Cache._PROTOCOL_MAPPING.keys())
)

@pytest.mark.parametrize("protocol", [Cache.MEMORY, Cache.REDIS, Cache.MEMCACHED])
def test_get_protocol_class(self, protocol):
assert Cache.get_protocol_class(protocol) == Cache._PROTOCOL_MAPPING[protocol]

def test_get_protocol_class_invalid(self):
with pytest.raises(KeyError):
Cache.get_protocol_class('http')
Cache.get_protocol_class("http")


class TestCacheHandler:
Expand Down

0 comments on commit fb181e6

Please sign in to comment.