Skip to content

Commit a7f39b0

Browse files
authoredNov 24, 2020
Improve MockRedis _encode(): so it will work on all types of value (sonic-net#1265)
The original code only works on `str` value
1 parent 8427caf commit a7f39b0

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed
 

‎tests/mock_tables/dbconnector.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,14 @@ def __init__(self, *args, **kwargs):
116116

117117
# Patch mockredis/mockredis/client.py
118118
# The offical implementation assume decode_responses=False
119-
# Here we detect the option first and only encode when decode_responses=False
119+
# Here we detect the option and decode after doing encode
120120
def _encode(self, value):
121121
"Return a bytestring representation of the value. Taken from redis-py connection.py"
122-
if isinstance(value, bytes):
123-
return value
124-
elif isinstance(value, int):
125-
value = str(value).encode('utf-8')
126-
elif isinstance(value, float):
127-
value = repr(value).encode('utf-8')
128-
elif not isinstance(value, str):
129-
value = str(value).encode('utf-8')
130-
elif not self.decode_responses:
131-
value = value.encode('utf-8', 'strict')
132-
return value
122+
123+
value = super(SwssSyncClient, self)._encode(value)
124+
125+
if self.decode_responses:
126+
return value.decode('utf-8')
133127

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

0 commit comments

Comments
 (0)
Please sign in to comment.