Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Shoham Elias <[email protected]>
  • Loading branch information
shohamazon committed Aug 11, 2024
1 parent f6e79d9 commit d5acd12
Showing 1 changed file with 142 additions and 1 deletion.
143 changes: 142 additions & 1 deletion glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,11 @@ pub(crate) fn convert_to_expected_type(

for inner_item in outer_array {
if let Value::Map(map) = converted_map.clone() {
if let Some((key, running_script_value)) = map.get(0) {
if let Some((key, running_script_value)) = map.first() {
if key == &Value::BulkString(b"running_script".into()) && running_script_value != &Value::Nil {
// we have found a response with an active running script
// the server can only run a single script / function at a time
// we can return this nodes response
break;
}
}
Expand Down Expand Up @@ -2464,6 +2467,144 @@ mod tests {
);
}

#[test]
fn convert_function_stats_standalone_response() {
assert!(matches!(
expected_type_for_cmd(redis::cmd("FUNCTION").arg("STATS")),
Some(ExpectedReturnType::FunctionStatsReturnType)
));

let resp2_response = Value::Array(vec![
Value::Array(vec![
Value::BulkString(b"running_script".into()),
Value::Array(vec![
Value::BulkString(b"name".into()),
Value::BulkString(b"<function name>".into()),
Value::BulkString(b"command".into()),
Value::Array(vec![
Value::BulkString(b"fcall".into()),
Value::BulkString(b"<function name>".into()),
Value::BulkString(b"... rest `fcall` args ...".into()),
]),
Value::BulkString(b"duration_ms".into()),
Value::Int(24529),
]),
Value::BulkString(b"engines".into()),
Value::Array(vec![
Value::BulkString(b"LUA".into()),
Value::Array(vec![
Value::BulkString(b"libraries_count".into()),
Value::Int(3),
Value::BulkString(b"functions_count".into()),
Value::Int(5),
]),
]),
]),
Value::Array(vec![
Value::BulkString(b"running_script".into()),
Value::Nil,
Value::BulkString(b"engines".into()),
Value::Array(vec![
Value::BulkString(b"LUA".into()),
Value::Array(vec![
Value::BulkString(b"libraries_count".into()),
Value::Int(3),
Value::BulkString(b"functions_count".into()),
Value::Int(5),
]),
]),
]),
]);

let resp3_response = Value::Map(vec![
(
Value::BulkString(b"running_script".into()),
Value::Map(vec![
(
Value::BulkString(b"name".into()),
Value::BulkString(b"<function name>".into()),
),
(
Value::BulkString(b"command".into()),
Value::Array(vec![
Value::BulkString(b"fcall".into()),
Value::BulkString(b"<function name>".into()),
Value::BulkString(b"... rest `fcall` args ...".into()),
]),
),
(Value::BulkString(b"duration_ms".into()), Value::Int(24529)),
]),
),
(
Value::BulkString(b"engines".into()),
Value::Map(vec![(
Value::BulkString(b"LUA".into()),
Value::Map(vec![
(Value::BulkString(b"libraries_count".into()), Value::Int(3)),
(Value::BulkString(b"functions_count".into()), Value::Int(5)),
]),
)]),
),
]);

let conversion_type = Some(ExpectedReturnType::FunctionStatsReturnType);
assert_eq!(
convert_to_expected_type(resp2_response.clone(), conversion_type),
Ok(resp3_response.clone())
);

let resp3_map_response = Value::Array(vec![
Value::Map(vec![
(Value::BulkString(b"running_script".into()), Value::Nil),
(
Value::BulkString(b"engines".into()),
Value::Map(vec![(
Value::BulkString(b"LUA".into()),
Value::Map(vec![
(Value::BulkString(b"libraries_count".into()), Value::Int(3)),
(Value::BulkString(b"functions_count".into()), Value::Int(5)),
]),
)]),
),
]),
Value::Map(vec![
(
Value::BulkString(b"running_script".into()),
Value::Map(vec![
(
Value::BulkString(b"name".into()),
Value::BulkString(b"<function name>".into()),
),
(
Value::BulkString(b"command".into()),
Value::Array(vec![
Value::BulkString(b"fcall".into()),
Value::BulkString(b"<function name>".into()),
Value::BulkString(b"... rest `fcall` args ...".into()),
]),
),
(Value::BulkString(b"duration_ms".into()), Value::Int(24529)),
]),
),
(
Value::BulkString(b"engines".into()),
Value::Map(vec![(
Value::BulkString(b"LUA".into()),
Value::Map(vec![
(Value::BulkString(b"libraries_count".into()), Value::Int(3)),
(Value::BulkString(b"functions_count".into()), Value::Int(5)),
]),
)]),
),
]),
]);

assert_eq!(
convert_to_expected_type(resp3_map_response.clone(), conversion_type),
Ok(resp3_response.clone())
);
}

#[test]
fn convert_smismember() {
assert!(matches!(
Expand Down

0 comments on commit d5acd12

Please sign in to comment.