Skip to content

Commit

Permalink
Update alias config when config already exists (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcugat authored and argaen committed Mar 20, 2018
1 parent cb2acb1 commit c0186d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions aiocache/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ def set_config(self, config):
All keys in the config are optional, if they are not passed the defaults
for the specified class will be used.
If a config key already exists, it will be updated with the new values.
"""
if "default" not in config:
raise ValueError("default config must be provided")
for config_name in config.keys():
self._caches.pop(config_name, None)
self._config = config


Expand Down
37 changes: 36 additions & 1 deletion tests/ut/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from aiocache import SimpleMemoryCache, RedisCache, caches
from aiocache.factory import _class_from_string, _create_cache
from aiocache.serializers import PickleSerializer
from aiocache.serializers import JsonSerializer, PickleSerializer
from aiocache.plugins import TimingPlugin, HitMissRatioPlugin


Expand Down Expand Up @@ -212,6 +212,41 @@ def test_set_empty_config(self):
with pytest.raises(ValueError):
caches.set_config({})

def test_set_config_updates_existing_values(self):
assert not isinstance(caches.get('default').serializer, JsonSerializer)
caches.set_config({
'default': {
'cache': "aiocache.SimpleMemoryCache",
'serializer': {
'class': "aiocache.serializers.JsonSerializer"
}
}
})
assert isinstance(caches.get('default').serializer, JsonSerializer)

def test_set_config_removes_existing_caches(self):
caches.set_config({
'default': {
'cache': "aiocache.SimpleMemoryCache",
},
'alt': {
'cache': "aiocache.SimpleMemoryCache",
},
})
caches.get('default')
caches.get('alt')
assert len(caches._caches) == 2

caches.set_config({
'default': {
'cache': "aiocache.SimpleMemoryCache",
},
'alt': {
'cache': "aiocache.SimpleMemoryCache",
},
})
assert caches._caches == {}

def test_set_config_no_default(self):
with pytest.raises(ValueError):
caches.set_config({
Expand Down

0 comments on commit c0186d0

Please sign in to comment.