Skip to content

Commit 9692780

Browse files
committed
feat,fix: Fix testcontainer startup and listening. Add test for insertion into table and select
1 parent cdbb4b9 commit 9692780

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

src/lib.rs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,30 +294,83 @@ pub mod pg_test {
294294
}
295295
}
296296

297+
// use serde::{Deserialize, Serialize};
298+
// #[derive(PostgresType, Serialize, Deserialize, Debug, Eq, PartialEq)]
299+
// struct KVStruct {
300+
// pub key: String,
301+
// pub value: String,
302+
// }
303+
297304
#[pg_schema]
298305
#[cfg(any(test, feature = "pg_test"))]
299306
mod tests {
307+
use std::time::Duration;
308+
300309
use super::*;
301-
use serde::{Deserialize, Serialize};
302310
use testcontainers::{
303311
core::{IntoContainerPort, WaitFor},
304312
runners::SyncRunner,
305313
GenericImage, ImageExt,
306314
};
307315

308-
#[derive(PostgresType, Serialize, Deserialize, Debug, Eq, PartialEq)]
309-
struct KVStruct {
310-
key: String,
311-
value: String,
312-
}
316+
const CMD: [&'static str; 5] = [
317+
"/usr/local/bin/etcd",
318+
"--listen-client-urls",
319+
"http://0.0.0.0:2379",
320+
"--advertise-client-urls",
321+
"http://0.0.0.0:2379",
322+
];
313323

314324
#[pg_test]
315325
fn test_create_table() {
316326
let container = GenericImage::new("quay.io/coreos/etcd", "v3.6.4")
317327
.with_exposed_port(2379.tcp())
318-
// .with_wait_for(WaitFor::message_on_stdout("ready to serve client requests"))
319-
.with_wait_for(WaitFor::seconds(20))
320-
.with_network("bridge")
328+
.with_wait_for(WaitFor::message_on_either_std(
329+
"ready to serve client requests",
330+
))
331+
.with_privileged(true)
332+
.with_cmd(CMD)
333+
.with_startup_timeout(Duration::from_secs(90))
334+
.start()
335+
.expect("An etcd image was supposed to be started");
336+
337+
let host = container
338+
.get_host()
339+
.expect("Host-address should be available");
340+
341+
let port = container
342+
.get_host_port_ipv4(2379.tcp())
343+
.expect("Exposed host port should be available");
344+
345+
let url = format!("{}:{}", host, port);
346+
dbg!("Testing FDW on container at {}", &url);
347+
348+
// Create our fdw
349+
Spi::run("CREATE FOREIGN DATA WRAPPER etcd_fdw handler etcd_fdw_handler validator etcd_fdw_validator;").expect("FDW should have been created");
350+
351+
// Create a server
352+
Spi::run(
353+
format!(
354+
"CREATE SERVER etcd_test_server FOREIGN DATA WRAPPER etcd_fdw options(connstr '{}')",
355+
url
356+
)
357+
.as_str(),
358+
)
359+
.expect("Server should have been created");
360+
361+
// Create a foreign table
362+
Spi::run("CREATE FOREIGN TABLE test (key text, value text) server etcd_test_server options (rowid_column 'key')").expect("Test table should have been created");
363+
}
364+
#[pg_test]
365+
fn test_insert_select() {
366+
let container = GenericImage::new("quay.io/coreos/etcd", "v3.6.4")
367+
.with_exposed_port(2379.tcp())
368+
.with_wait_for(WaitFor::message_on_either_std(
369+
"ready to serve client requests",
370+
))
371+
.with_cmd(CMD)
372+
.with_privileged(true)
373+
.with_startup_timeout(Duration::from_secs(90))
321374
.start()
322375
.expect("An etcd image was supposed to be started");
323376

@@ -347,5 +400,18 @@ mod tests {
347400

348401
// Create a foreign table
349402
Spi::run("CREATE FOREIGN TABLE test (key text, value text) server etcd_test_server options (rowid_column 'key')").expect("Test table should have been created");
403+
404+
// Insert into the foreign table
405+
Spi::run("INSERT INTO test (key, value) VALUES ('foo','bar'),('bar','baz')")
406+
.expect("INSERT should work");
407+
408+
let query_result = Spi::get_two::<String, String>("SELECT * FROM test WHERE key='foo'")
409+
.expect("Select should work");
410+
411+
assert_eq!((Some(format!("foo")), Some(format!("bar"))), query_result);
412+
let query_result = Spi::get_two::<String, String>("SELECT * FROM test WHERE key='bar'")
413+
.expect("Select should work");
414+
415+
assert_eq!((Some(format!("bar")), Some(format!("baz"))), query_result);
350416
}
351417
}

0 commit comments

Comments
 (0)