From 706d504784916a57e66b5d90e45123120dea24c9 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Mon, 23 Nov 2020 15:49:45 -0800 Subject: [PATCH] Improve MockRedis _encode(): so it will work on all types of value (#179) --- tests/mock_tables/dbconnector.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tests/mock_tables/dbconnector.py b/tests/mock_tables/dbconnector.py index 27472bf8e6c6..b88cf1fef5a6 100644 --- a/tests/mock_tables/dbconnector.py +++ b/tests/mock_tables/dbconnector.py @@ -5,7 +5,6 @@ import mockredis import redis -import swsssdk from swsssdk import SonicV2Connector from swsssdk import SonicDBConfig from swsssdk.interface import DBInterface @@ -16,7 +15,6 @@ long = int xrange = range basestring = str - from functools import reduce def clean_up_config(): # Set SonicDBConfig variables to initial state @@ -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 '/'