Skip to content

Commit

Permalink
Support decode_responses for string ops
Browse files Browse the repository at this point in the history
Addresses locationlabs#97
  • Loading branch information
Roman Levin authored and PCManticore committed May 17, 2018
1 parent ba41d90 commit 42277ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mockredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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):
"""
Expand Down
5 changes: 5 additions & 0 deletions mockredis/tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 42277ea

Please sign in to comment.