Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/modules/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,27 @@ impl SimpleMap {
.collect();
Ok(Value::Array(vec![Value::from_static_str("0"), Value::Array(keys)]))
}

/// Perform an `MGET` operation.
pub fn mget(&self, args: Vec<Value>) -> Result<Value, Error> {
let guard = self.values.lock();
let mut results = Vec::with_capacity(args.len());

for arg in args.into_iter() {
let key: Key = arg.try_into()?;
let value = guard.get(&key).cloned().unwrap_or(Value::Null);
results.push(value);
}

Ok(Value::Array(results))
}
}

impl Mocks for SimpleMap {
fn process_command(&self, command: MockCommand) -> Result<Value, Error> {
match &*command.cmd {
"GET" => self.get(command.args),
"MGET" => self.mget(command.args),
"SET" => self.set(command.args),
"DEL" => self.del(command.args),
"SCAN" => self.scan(command.args),
Expand Down Expand Up @@ -414,6 +429,39 @@ mod tests {
assert_eq!(actual, "bar");
}

#[tokio::test]
async fn should_use_simple_map_mget_mock() {
let (client, _) = create_mock_client(Arc::new(SimpleMap::new())).await;

// Set multiple values
client
.set::<(), _, _>("key1", "value1", None, None, false)
.await
.expect("Failed to call SET");
client
.set::<(), _, _>("key2", "value2", None, None, false)
.await
.expect("Failed to call SET");
client
.set::<(), _, _>("key3", "value3", None, None, false)
.await
.expect("Failed to call SET");

// Test MGET with existing and non-existing keys
let actual: Vec<Option<String>> = client
.mget(vec!["key1", "nonexistent", "key2", "key3"])
.await
.expect("Failed to call MGET");

let expected = vec![
Some("value1".to_string()),
None,
Some("value2".to_string()),
Some("value3".to_string()),
];
assert_eq!(actual, expected);
}

#[tokio::test]
async fn should_use_buffer_mock() {
let buffer = Arc::new(Buffer::new());
Expand Down