Skip to content

Commit 706d504

Browse files
authored
Improve MockRedis _encode(): so it will work on all types of value (sonic-net#179)
1 parent 64c93a1 commit 706d504

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

tests/mock_tables/dbconnector.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import mockredis
77
import redis
8-
import swsssdk
98
from swsssdk import SonicV2Connector
109
from swsssdk import SonicDBConfig
1110
from swsssdk.interface import DBInterface
@@ -16,7 +15,6 @@
1615
long = int
1716
xrange = range
1817
basestring = str
19-
from functools import reduce
2018

2119
def clean_up_config():
2220
# Set SonicDBConfig variables to initial state
@@ -111,20 +109,14 @@ def __init__(self, *args, **kwargs):
111109

112110
# Patch mockredis/mockredis/client.py
113111
# The offical implementation assume decode_responses=False
114-
# Here we detect the option first and only encode when decode_responses=False
112+
# Here we detect the option and decode after doing encode
115113
def _encode(self, value):
116114
"Return a bytestring representation of the value. Taken from redis-py connection.py"
117-
if isinstance(value, bytes):
118-
return value
119-
elif isinstance(value, (int, long)):
120-
value = str(value).encode('utf-8')
121-
elif isinstance(value, float):
122-
value = repr(value).encode('utf-8')
123-
elif not isinstance(value, basestring):
124-
value = str(value).encode('utf-8')
125-
elif not self.decode_responses:
126-
value = value.encode('utf-8', 'strict')
127-
return value
115+
116+
value = super(SwssSyncClient, self)._encode(value)
117+
118+
if self.decode_responses:
119+
return value.decode('utf-8')
128120

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

0 commit comments

Comments
 (0)