Skip to content

Commit cdbb4b9

Browse files
committed
feat: Add testing and a test to create a test foreign table
1 parent 6cce769 commit cdbb4b9

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ pgrx = {version="=0.16.0"}
2727
supabase-wrappers = {git="https://github.com/supabase/wrappers.git", branch="main",default-features = false}
2828
thiserror = "2.0.16"
2929
tokio = { version = "1.47.1", features = ["full"] }
30+
testcontainers = { version = "0.25.0", features = ["blocking"] }
31+
serde = { version = "1.0.226", features = ["derive"] }
3032

3133
[dev-dependencies]
3234
pgrx-tests = "=0.16.0"
35+
testcontainers = { version = "0.25.0", features = ["blocking"] }
36+
serde = { version = "1.0.226", features = ["derive"] }
3337

3438
[profile.dev]
3539
panic = "unwind"

src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use etcd_client::{Client, DeleteOptions, GetOptions, KeyValue, PutOptions};
22
use pgrx::pg_sys::panic::ErrorReport;
33
use pgrx::PgSqlErrorCode;
4+
use pgrx::*;
45
use supabase_wrappers::prelude::*;
56
use thiserror::Error;
67

@@ -279,3 +280,72 @@ impl ForeignDataWrapper<EtcdFdwError> for EtcdFdw {
279280
Ok(())
280281
}
281282
}
283+
284+
#[cfg(test)]
285+
pub mod pg_test {
286+
287+
pub fn setup(_options: Vec<&str>) {
288+
// perform one-off initialization when the pg_test framework starts
289+
}
290+
291+
pub fn postgresql_conf_options() -> Vec<&'static str> {
292+
// return any postgresql.conf settings that are required for your tests
293+
vec![]
294+
}
295+
}
296+
297+
#[pg_schema]
298+
#[cfg(any(test, feature = "pg_test"))]
299+
mod tests {
300+
use super::*;
301+
use serde::{Deserialize, Serialize};
302+
use testcontainers::{
303+
core::{IntoContainerPort, WaitFor},
304+
runners::SyncRunner,
305+
GenericImage, ImageExt,
306+
};
307+
308+
#[derive(PostgresType, Serialize, Deserialize, Debug, Eq, PartialEq)]
309+
struct KVStruct {
310+
key: String,
311+
value: String,
312+
}
313+
314+
#[pg_test]
315+
fn test_create_table() {
316+
let container = GenericImage::new("quay.io/coreos/etcd", "v3.6.4")
317+
.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")
321+
.start()
322+
.expect("An etcd image was supposed to be started");
323+
324+
let host = container
325+
.get_host()
326+
.expect("Host-address should be available");
327+
328+
let port = container
329+
.get_host_port_ipv4(2379.tcp())
330+
.expect("Exposed host port should be available");
331+
332+
let url = format!("{}:{}", host, port);
333+
dbg!("Testing FDW on container at {}", &url);
334+
335+
// Create our fdw
336+
Spi::run("CREATE FOREIGN DATA WRAPPER etcd_fdw handler etcd_fdw_handler validator etcd_fdw_validator;").expect("FDW should have been created");
337+
338+
// Create a server
339+
Spi::run(
340+
format!(
341+
"CREATE SERVER etcd_test_server FOREIGN DATA WRAPPER etcd_fdw options(connstr '{}')",
342+
url
343+
)
344+
.as_str(),
345+
)
346+
.expect("Server should have been created");
347+
348+
// Create a foreign table
349+
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");
350+
}
351+
}

0 commit comments

Comments
 (0)