Skip to content

Commit

Permalink
Improve MockRedis _encode(): so it will work on all types of value (#179
Browse files Browse the repository at this point in the history
)
  • Loading branch information
qiluo-msft committed Nov 23, 2020
1 parent 64c93a1 commit 706d504
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions tests/mock_tables/dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import mockredis
import redis
import swsssdk
from swsssdk import SonicV2Connector
from swsssdk import SonicDBConfig
from swsssdk.interface import DBInterface
Expand All @@ -16,7 +15,6 @@
long = int
xrange = range
basestring = str
from functools import reduce

def clean_up_config():
# Set SonicDBConfig variables to initial state
Expand Down Expand Up @@ -111,20 +109,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, long)):
value = str(value).encode('utf-8')
elif isinstance(value, float):
value = repr(value).encode('utf-8')
elif not isinstance(value, basestring):
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 706d504

Please sign in to comment.