|
1 | 1 | use etcd_client::{Client, DeleteOptions, GetOptions, KeyValue, PutOptions}; |
2 | 2 | use pgrx::pg_sys::panic::ErrorReport; |
3 | 3 | use pgrx::PgSqlErrorCode; |
| 4 | +use pgrx::*; |
4 | 5 | use supabase_wrappers::prelude::*; |
5 | 6 | use thiserror::Error; |
6 | 7 |
|
@@ -279,3 +280,72 @@ impl ForeignDataWrapper<EtcdFdwError> for EtcdFdw { |
279 | 280 | Ok(()) |
280 | 281 | } |
281 | 282 | } |
| 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