def test_init(self, strict_redis, conn_pool): host = "127.0.0.1" port = 6379 redis_base.ClientBase() conn_pool.assert_called_with(host=host, port=port) self.assertIsNotNone(redis_base.ClientBase.read_connection_pool) self.assertIsNotNone(redis_base.ClientBase.write_connection_pool)
def test_delete_field(self, strict_redis): rc = redis_base.ClientBase(use_master=True) mock_client = rc._client = mock.MagicMock() rc.delete_field("1.000000000002", "test_field_name") mock_client.hdel.assert_called_once_with("1.000000000002", "test_field_name")
def test_vif_key(self, *args, **kwargs): client = redis_base.ClientBase() device_id = str(uuid.uuid4()) mac_address = netaddr.EUI("AA:BB:CC:DD:EE:FF") redis_key = client.vif_key(device_id, mac_address.value) expected = "%s.%s" % (device_id, "aabbccddeeff") self.assertEqual(expected, redis_key)
def test_sentinel_connection_bad_format_raises(self, strict_redis, master_for, sentinel_pool): sentinels = "" master_label = "master" with self._stubs(True, sentinels, master_label): with self.assertRaises(TypeError): redis_base.ClientBase(is_master=True)
def test_set_field(self, strict_redis): rc = redis_base.ClientBase(use_master=True) mock_client = rc._client = mock.MagicMock() dummy_data = {"dummy_data": "foo"} rc.set_field("1.000000000002", "test_field_name", dummy_data) mock_client.hset.assert_called_once_with("1.000000000002", "test_field_name", json.dumps(dummy_data))
def test_get_field(self, strict_redis): rc = redis_base.ClientBase() mock_client = rc._client = mock.MagicMock() mock_client.hget.return_value = "returned hash field" r = rc.get_field("1.000000000002", "test_field_name") mock_client.hget.assert_called_once_with("1.000000000002", "test_field_name") self.assertEqual(r, "returned hash field")
def test_vif_keys_hget_string_key_returned(self, strict_redis): rc = redis_base.ClientBase() keys = '1.000000000002' mock_client = rc._client = mock.MagicMock() mock_client.hget.return_value = "returned hash field" mock_client.keys.return_value = keys r = rc.vif_keys(field="test_field_name") mock_client.hget.assert_called_once_with("1.000000000002", "test_field_name") self.assertEqual(r, [keys])
def test_sentinel_connection(self, strict_redis, master_for, sentinel_pool, sentinel_mock): host = "127.0.0.1" port = 6379 sentinels = ["%s:%s" % (host, port)] master_label = "master" sentinel_mock.return_value = sentinels with self._stubs(True, sentinels, master_label): redis_base.ClientBase(use_master=True) sentinel_pool.assert_called_with(master_label, sentinels, check_connection=True, is_master=True)
def test_vif_keys_hget(self, strict_redis): rc = redis_base.ClientBase() keys = ['1.000000000002', '2.000000000003'] mock_client = rc._client = mock.MagicMock() mock_client.hget.return_value = "returned hash field" mock_client.keys.return_value = keys r = rc.vif_keys(field="test_field_name") mock_client.hget.assert_has_calls([ mock.call("1.000000000002", "test_field_name"), mock.call("2.000000000003", "test_field_name") ]) self.assertFalse(mock_client.hgetall.called) self.assertEqual(r, keys)
def test_vif_keys_hgetall(self, strict_redis): rc = redis_base.ClientBase() keys = ['1.000000000002', '2.000000000003'] mock_client = rc._client = mock.MagicMock() mock_client.hgetall.return_value = { "returned hash field1": "returned hash value1", "returned hash field2": "returned hash value2" } mock_client.keys.return_value = keys r = rc.vif_keys() mock_client.hgetall.assert_has_calls( [mock.call("1.000000000002"), mock.call("2.000000000003")]) self.assertFalse(mock_client.hget.called) self.assertEqual(r, keys)
def test_get_fields(self, strict_redis): mock_redis = mock.MagicMock() mock_pipeline = mock.MagicMock() strict_redis.return_value = mock_redis mock_redis.pipeline.return_value = mock_pipeline mock_pipeline.execute.return_value = "returned executed" rc = redis_base.ClientBase() r = rc.get_fields([ "1.000000000002", "1.000000000002", "5.000000000006", "7.000000000008" ], "test_field_name") mock_pipeline.hget.assert_has_calls([ mock.call("1.000000000002", "test_field_name"), mock.call("1.000000000002", "test_field_name"), mock.call("5.000000000006", "test_field_name"), mock.call("7.000000000008", "test_field_name") ], any_order=True) mock_pipeline.execute.assert_called_once_with() self.assertEqual(r, "returned executed")
def test_client_connection_fails_gracefully(self, conn_pool): conn_err = redis.ConnectionError with mock.patch("redis.StrictRedis") as redis_mock: redis_mock.side_effect = conn_err with self.assertRaises(q_exc.RedisConnectionFailure): redis_base.ClientBase(use_master=True)