diff --git a/src/error.rs b/src/error.rs index eb9d289..61ff017 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,7 @@ pub enum Error { NegativeNumbers, } +#[derive(Debug)] pub enum PgError { SqidsError(sqids::Error), CustomError(Error), diff --git a/src/lib.rs b/src/lib.rs index 1fd28e1..d58c6af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -214,3 +214,60 @@ fn sqids_decode_with_alphabet_min_length_blocklist( Ok(sqids.decode(id).iter().map(|n| *n as i64).collect()) } + +#[cfg(any(test, feature = "pg_test"))] +#[pg_schema] +mod tests { + use pgrx::prelude::*; + use std::collections::HashMap; + + // @NOTE since this extension uses `https://crates.io/crates/sqids`, this repo does not include the standard Sqids unit tests (which are included + // in the external Rust library). Therefore, we are testing the SQL functions only. + + #[pg_test] + fn test_sqids_encode() { + let tests: HashMap<&str, Option> = HashMap::from([ + ("SELECT sqids_encode(1, 2, 3)", Some("86Rf07".to_string())), + ("SELECT sqids_encode(10::smallint, 1, 2, 3)", Some("86Rf07xd4z".to_string())), + ("SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 1, 2, 3)", Some("XRKUdQ".to_string())), + ("SELECT sqids_encode(array['86Rf07'], 1, 2, 3)", Some("se8ojk".to_string())), + ("SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, 1, 2, 3)", Some("XRKUdQVBzg".to_string())), + ("SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', array['XRKUdQ'], 1, 2, 3)", Some("WyXQfF".to_string())), + ("SELECT sqids_encode(10::smallint, array['86Rf07'], 1, 2, 3)", Some("se8ojkCQvX".to_string())), + ("SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, array['XRKUdQVBzg'], 1, 2, 3)", Some("WyXQfFQ21T".to_string())), + ]); + + for (sql, expected) in tests { + let result = Spi::get_one(sql).expect("Query failed"); + assert_eq!(result, expected, "Failed on query: {}", sql); + } + } + + #[pg_test] + fn test_sqids_decode() { + let tests: HashMap<&str, Option>> = HashMap::from([ + ("SELECT sqids_decode('86Rf07')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode(10::smallint, '86Rf07xd4z')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 'XRKUdQ')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode(array['86Rf07'], 'se8ojk')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, 'XRKUdQVBzg')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', array['XRKUdQ'], 'WyXQfF')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode(10::smallint, array['86Rf07'], 'se8ojkCQvX')", Some(vec![1, 2, 3])), + ("SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, array['XRKUdQVBzg'], 'WyXQfFQ21T')", Some(vec![1, 2, 3])), + ]); + + for (sql, expected) in tests { + let result = Spi::get_one(sql).expect("Query failed"); + assert_eq!(result, expected, "Failed on query: {}", sql); + } + } +} + +#[cfg(test)] +pub mod pg_test { + pub fn setup(_options: Vec<&str>) {} + + pub fn postgresql_conf_options() -> Vec<&'static str> { + vec![] + } +}