Skip to content

Commit

Permalink
Improve MockRedis _encode(): so it will work on all types of value (s…
Browse files Browse the repository at this point in the history
…onic-net#1265)

The original code only works on `str` value
  • Loading branch information
qiluo-msft authored Nov 24, 2020
1 parent 8427caf commit a7f39b0
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions tests/mock_tables/dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,14 @@ def __init__(self, *args, **kwargs):

# Patch mockredis/mockredis/client.py
# The offical implementation assume decode_responses=False
# Here we detect the option first and only encode when decode_responses=False
# Here we detect the option and decode after doing encode
def _encode(self, value):
"Return a bytestring representation of the value. Taken from redis-py connection.py"
if isinstance(value, bytes):
return value
elif isinstance(value, int):
value = str(value).encode('utf-8')
elif isinstance(value, float):
value = repr(value).encode('utf-8')
elif not isinstance(value, str):
value = str(value).encode('utf-8')
elif not self.decode_responses:
value = value.encode('utf-8', 'strict')
return value

value = super(SwssSyncClient, self)._encode(value)

if self.decode_responses:
return value.decode('utf-8')

# Patch mockredis/mockredis/client.py
# The official implementation will filter out keys with a slash '/'
Expand Down

0 comments on commit a7f39b0

Please sign in to comment.