Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decode_responses for string ops #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')