def test_retrieve_cache_new_instance(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.10", "port": 6378, "serializer": { "class": "pycached.serializers.PickleSerializer", "encoding": "encoding", }, "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], } }) cache = caches.create("default") assert isinstance(cache, RedisCache) assert cache.endpoint == "127.0.0.10" assert cache.port == 6378 assert isinstance(cache.serializer, PickleSerializer) assert cache.serializer.encoding == "encoding" assert len(cache.plugins) == 2
def alt_cache(): # This generates a new instance every time! You can also use `caches.create('alt')` # or even `caches.create('alt', namespace="test", etc...)` to override extra args cache = caches.create(**caches.get_alias_config('redis_alt')) cache.set("key", "value") assert cache.get("key") == "value" assert isinstance(cache, RedisCache) assert isinstance(cache.serializer, PickleSerializer) assert len(cache.plugins) == 2 assert cache.endpoint == "127.0.0.1" assert cache.timeout == 1 assert cache.port == 6379 cache.close()
def test_alias_config_is_reusable(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.10", "port": 6378, "serializer": { "class": "pycached.serializers.PickleSerializer" }, "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], }, "alt": { "cache": "pycached.SimpleMemoryCache" }, }) default = caches.create(**caches.get_alias_config("default")) alt = caches.create(**caches.get_alias_config("alt")) assert isinstance(default, RedisCache) assert default.endpoint == "127.0.0.10" assert default.port == 6378 assert isinstance(default.serializer, PickleSerializer) assert len(default.plugins) == 2 assert isinstance(alt, SimpleMemoryCache)
def test_create_extra_args(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.9", "db": 10, "port": 6378, } }) cache = caches.create("default", namespace="whatever", endpoint="127.0.0.10", db=10) assert cache.namespace == "whatever" assert cache.endpoint == "127.0.0.10" assert cache.db == 10
def test_ensure_plugins_order(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], } }) cache = caches.get("default") assert isinstance(cache.plugins[0], HitMissRatioPlugin) cache = caches.create("default") assert isinstance(cache.plugins[0], HitMissRatioPlugin)
def test_create_cache_ensure_alias_or_cache(self): with pytest.raises(TypeError): caches.create()
def test_create_cache_class_no_alias(self): cache = caches.create(cache=RedisCache) assert isinstance(cache, RedisCache) assert cache.endpoint == "127.0.0.1" assert cache.port == 6379
def test_create_not_reuse(self): assert caches.create("default") is not caches.create("default")
def test_get_wrong_alias(self): with pytest.raises(KeyError): caches.get("wrong_cache") with pytest.raises(KeyError): caches.create("wrong_cache")