1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use testcontainers::{core::WaitFor, Image};

const NAME: &str = "redis/redis-stack-server";
const TAG: &str = "7.2.0-v8";

/// Module to work with [`Redis Stack`] inside of tests.
///
/// Starts an instance of Redis Stack based on the official [`Redis Stack docker image`].
///
/// By default Redis is exposed on Port 6379 ([`REDIS_PORT`]) and has no access control. Please refer to the [`Redis reference guide`] for more informations on how to interact with the API.
///
/// # Example
/// ```
/// use redis::JsonCommands;
/// use serde_json::json;
/// use testcontainers::clients;
/// use testcontainers_modules::redis::{RedisStack, REDIS_PORT};
///
/// let docker = clients::Cli::default();
/// let redis_instance = docker.run(RedisStack);
/// let host_port = redis_instance.get_host_port_ipv4(REDIS_PORT);
///
/// let url = format!("redis://127.0.0.1:{host_port}");
/// let client = redis::Client::open(url.as_ref()).unwrap();
/// let mut con = client.get_connection().unwrap();
///
/// con.json_set::<_,_,_,()>("my_key", "$", &json!({ "number": 42 })).unwrap();
/// let result: String = con.json_get("my_key", "$..number").unwrap();
/// ```
///
/// [`Redis Stack`]: https://redis.io/docs/about/about-stack/
/// [`Redis Stack docker image`]: https://hub.docker.com/r/redis/redis-stack-server
/// [`Redis reference guide`]: https://redis.io/docs/interact/
/// [`REDIS_PORT`]: super::REDIS_PORT
#[derive(Debug, Default)]
pub struct RedisStack;

impl Image for RedisStack {
    type Args = ();

    fn name(&self) -> String {
        NAME.to_owned()
    }

    fn tag(&self) -> String {
        TAG.to_owned()
    }

    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 testcontainers::clients;

    use crate::redis::{RedisStack, REDIS_PORT};

    #[test]
    fn redis_fetch_an_integer_in_json() {
        let _ = pretty_env_logger::try_init();
        let docker = clients::Cli::default();
        let node = docker.run(RedisStack);
        let host_port = node.get_host_port_ipv4(REDIS_PORT);
        let url = format!("redis://127.0.0.1:{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);
    }
}