def test_loads_with_none(self): assert PickleSerializer().loads(None) is None
def test_dumps_and_loads(self): obj = Dummy(1, 2) serializer = PickleSerializer() assert serializer.loads(serializer.dumps(obj)) == obj
def test_dumps_with_none(self): assert isinstance(PickleSerializer().dumps(None), bytes)
def test_loads(self): assert PickleSerializer().loads(b"\x80\x03X\x02\x00\x00\x00hiq\x00.") == "hi"
def test_dumps(self): assert PickleSerializer().dumps("hi") == b"\x80\x03X\x02\x00\x00\x00hiq\x00."
def test_set_types(self, obj): serializer = PickleSerializer() assert serializer.loads(serializer.dumps(obj)) == obj
def test_init(self): serializer = PickleSerializer() assert isinstance(serializer, BaseSerializer) assert serializer.DEFAULT_ENCODING is None assert serializer.encoding is None
from collections import namedtuple from pycached import cached, RedisCache from pycached.serializers import PickleSerializer Result = namedtuple('Result', "content, status") @cached(ttl=10, cache=RedisCache, key="key", serializer=PickleSerializer(), port=6379, namespace="main") def cached_call(): return Result("content", 200) def test_cached(): cache = RedisCache(endpoint="127.0.0.1", port=6379, namespace="main") cached_call() assert cache.exists("key") is True cache.delete("key") cache.close() if __name__ == "__main__": test_cached()
from collections import namedtuple from pycached import Cache from pycached.serializers import PickleSerializer MyObject = namedtuple("MyObject", ["x", "y"]) cache = Cache(Cache.REDIS, serializer=PickleSerializer(), namespace="main") def complex_object(): obj = MyObject(x=1, y=2) cache.set("key", obj) my_object = cache.get("key") assert my_object.x == 1 assert my_object.y == 2 def test_python_object(): complex_object() cache.delete("key") cache.close() if __name__ == "__main__": test_python_object()