unit testing - How to mock a redis client in Python? -


i found bunch of unit tests failing, due developer hasn't mocked out dependency redis client within test. i'm trying give hand in matter have difficulties myself.

the method writes redis client:

redis_client = get_redis_client() redis_client.set('temp-facility-data', cpickle.dumps(df)) 

later in assert result retrieved:

res = cpickle.loads(get_redis_client().get('temp-facility-data')) expected = pd.series([set([1, 2, 3, 4, 5])], index=[1]) assert_series_equal(res.variation_pks, expected) 

i managed patch redis client's get() , set() successfully.

@mock.patch('redis.strictredis.get') @mock.patch('redis.strictredis.set') def test_identical(self, mock_redis_set, mock_redis_get):     mock_redis_get.return_value = ???     f2 = deepcopy(self.f)     f3 = deepcopy(self.f)     f2.pk = 2     f3.pk = 3     self.one_row(f2, f3) 

but don't know how set return_value of get() set() set in code, test pass.

right line fails test:

res = cpickle.loads(get_redis_client().get('temp-facility-data')) typeerror: must string, not magicmock 

any advice please?

think can use side effect set , value in local dict

data = {} def set(key, val):     data[key] = val  def get(key):     return data[key]  mock_redis_set.side_effect = set mock_redis_get.side_effect = 

not tested think should want


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -