testcontainers_modules/cratedb/
mod.rs1use std::{borrow::Cow, collections::HashMap};
2
3use testcontainers::{core::WaitFor, Image};
4
5const NAME: &str = "crate";
6const TAG: &str = "5.10.6";
7
8#[derive(Debug, Clone)]
42pub struct CrateDB {
43 env_vars: HashMap<String, String>,
44}
45
46impl CrateDB {
47 pub fn with_heap_size(mut self, ram_gb: usize) -> Self {
51 self.env_vars
52 .insert("CRATE_HEAP_SIZE".to_string(), format!("{ram_gb}g"));
53 self
54 }
55}
56impl Default for CrateDB {
57 fn default() -> Self {
58 let mut env_vars = HashMap::new();
59 env_vars.insert("CRATE_HEAP_SIZE".to_string(), "1g".to_string());
60 Self { env_vars }
61 }
62}
63
64impl Image for CrateDB {
65 fn name(&self) -> &str {
66 NAME
67 }
68
69 fn tag(&self) -> &str {
70 TAG
71 }
72
73 fn ready_conditions(&self) -> Vec<WaitFor> {
74 vec![WaitFor::message_on_stdout("started")]
75 }
76
77 fn env_vars(
78 &self,
79 ) -> impl IntoIterator<Item = (impl Into<Cow<'_, str>>, impl Into<Cow<'_, str>>)> {
80 &self.env_vars
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::CrateDB;
87 use crate::testcontainers::{runners::SyncRunner, ImageExt};
88
89 #[test]
90 fn cratedb_one_pls_one() -> Result<(), Box<dyn std::error::Error + 'static>> {
91 let crate_image = CrateDB::default();
92
93 let node = crate_image.start()?;
94 let connection_string = &format!(
95 "postgres://crate@{}:{}/postgres",
96 node.get_host()?,
97 node.get_host_port_ipv4(5432)?
98 );
99
100 let mut conn = postgres::Client::connect(connection_string, postgres::NoTls).unwrap();
101 let rows = conn.query("SELECT 1 + 1", &[]).unwrap();
102 assert_eq!(rows.len(), 1);
103
104 let first_row = &rows[0];
105 let first_column: i32 = first_row.get(0);
106 assert_eq!(first_column, 2);
107
108 Ok(())
109 }
110
111 #[test]
112 fn cratedb_custom_version() -> Result<(), Box<dyn std::error::Error + 'static>> {
113 let node = CrateDB::default().with_tag("5.4.3").start()?;
114
115 let connection_string = &format!(
116 "postgres://crate:crate@{}:{}/postgres",
117 node.get_host()?,
118 node.get_host_port_ipv4(5432)?
119 );
120 let mut conn = postgres::Client::connect(connection_string, postgres::NoTls).unwrap();
121
122 let rows = conn.query("SELECT version()", &[]).unwrap();
123 assert_eq!(rows.len(), 1);
124
125 let first_row = &rows[0];
126 let first_column: String = first_row.get(0);
127 assert!(first_column.contains("5.4.3"));
128 Ok(())
129 }
130}