use testcontainers::{core::WaitFor, Image};
const NAME: &str = "redis/redis-stack-server";
const TAG: &str = "7.2.0-v8";
#[derive(Debug, Default, Clone)]
pub struct RedisStack {
_priv: (),
}
impl Image for RedisStack {
fn name(&self) -> &str {
NAME
}
fn tag(&self) -> &str {
TAG
}
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("Ready to accept connections")]
}
}
#[cfg(test)]
mod tests {
use redis::JsonCommands;
use serde_json::json;
use crate::{
redis::{RedisStack, REDIS_PORT},
testcontainers::runners::SyncRunner,
};
#[test]
fn redis_fetch_an_integer_in_json() -> Result<(), Box<dyn std::error::Error + 'static>> {
let _ = pretty_env_logger::try_init();
let node = RedisStack::default().start()?;
let host_ip = node.get_host()?;
let host_port = node.get_host_port_ipv4(REDIS_PORT)?;
let url = format!("redis://{host_ip}:{host_port}");
let client = redis::Client::open(url.as_ref()).unwrap();
let mut con = client.get_connection().unwrap();
assert_eq!(
con.json_set("my_key", "$", &json!({ "number": 42 })),
Ok(true)
);
let result: String = con.json_get("my_key", "$..number").unwrap();
assert_eq!("[42]", result);
Ok(())
}
}