From a7f39b04eb4b14f56f14f9851d59b81ecd5285f8 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Tue, 24 Nov 2020 09:54:47 -0800 Subject: [PATCH] Improve MockRedis _encode(): so it will work on all types of value (#1265) The original code only works on `str` value --- tests/mock_tables/dbconnector.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/mock_tables/dbconnector.py b/tests/mock_tables/dbconnector.py index 1886715deaa8..ae945618688d 100644 --- a/tests/mock_tables/dbconnector.py +++ b/tests/mock_tables/dbconnector.py @@ -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 '/'