From 42277ea915682084d2fb1d0557bc018fc98845bb Mon Sep 17 00:00:00 2001 From: Roman Levin Date: Mon, 13 Feb 2017 16:18:55 +0100 Subject: [PATCH] Support `decode_responses` for string ops Addresses #97 --- mockredis/client.py | 7 ++++++- mockredis/tests/test_string.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mockredis/client.py b/mockredis/client.py index 926e048..3e9e8c3 100644 --- a/mockredis/client.py +++ b/mockredis/client.py @@ -40,6 +40,7 @@ def __init__(self, load_lua_dependencies=True, blocking_timeout=1000, blocking_sleep_interval=0.01, + decode_responses=False, **kwargs): """ Initialize as either StrictRedis or Redis. @@ -51,6 +52,7 @@ def __init__(self, self.load_lua_dependencies = load_lua_dependencies self.blocking_timeout = blocking_timeout self.blocking_sleep_interval = blocking_sleep_interval + self.decode_responses = decode_responses # The 'Redis' store self.redis = defaultdict(dict) self.redis_config = defaultdict(dict) @@ -294,7 +296,10 @@ def dbsize(self): def get(self, key): key = self._encode(key) - return self.redis.get(key) + value = self.redis.get(key) + if self.decode_responses and isinstance(value, bytes): + value = value.decode() + return value def __getitem__(self, name): """ diff --git a/mockredis/tests/test_string.py b/mockredis/tests/test_string.py index 4934921..f579785 100644 --- a/mockredis/tests/test_string.py +++ b/mockredis/tests/test_string.py @@ -309,3 +309,8 @@ def test_setbit(self): for x in range(16, 32, 2): eq_(0, self.redis.setbit("setbit_key", x, 1)) eq_(b"\xaa\xaa\xaa\xaa", self.redis.get("setbit_key")) + + def test_decode_responses(self): + self.redis.decode_responses = True + self.redis.set('key', 'value') + eq_(self.redis.get('key'), 'value')