Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Jul 9, 2024
1 parent 587e4f5 commit 9a819c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Error {
NegativeNumbers,
}

#[derive(Debug)]
pub enum PgError {
SqidsError(sqids::Error),
CustomError(Error),
Expand Down
57 changes: 57 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>> = 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<Vec<i64>>> = 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![]
}
}

0 comments on commit 9a819c5

Please sign in to comment.