@@ -214,3 +214,60 @@ fn sqids_decode_with_alphabet_min_length_blocklist(
214214
215215 Ok ( sqids. decode ( id) . iter ( ) . map ( |n| * n as i64 ) . collect ( ) )
216216}
217+
218+ #[ cfg( any( test, feature = "pg_test" ) ) ]
219+ #[ pg_schema]
220+ mod tests {
221+ use pgrx:: prelude:: * ;
222+ use std:: collections:: HashMap ;
223+
224+ // @NOTE since this extension uses `https://crates.io/crates/sqids`, this repo does not include the standard Sqids unit tests (which are included
225+ // in the external Rust library). Therefore, we are testing the SQL functions only.
226+
227+ #[ pg_test]
228+ fn test_sqids_encode ( ) {
229+ let tests: HashMap < & str , Option < String > > = HashMap :: from ( [
230+ ( "SELECT sqids_encode(1, 2, 3)" , Some ( "86Rf07" . to_string ( ) ) ) ,
231+ ( "SELECT sqids_encode(10::smallint, 1, 2, 3)" , Some ( "86Rf07xd4z" . to_string ( ) ) ) ,
232+ ( "SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 1, 2, 3)" , Some ( "XRKUdQ" . to_string ( ) ) ) ,
233+ ( "SELECT sqids_encode(array['86Rf07'], 1, 2, 3)" , Some ( "se8ojk" . to_string ( ) ) ) ,
234+ ( "SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, 1, 2, 3)" , Some ( "XRKUdQVBzg" . to_string ( ) ) ) ,
235+ ( "SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', array['XRKUdQ'], 1, 2, 3)" , Some ( "WyXQfF" . to_string ( ) ) ) ,
236+ ( "SELECT sqids_encode(10::smallint, array['86Rf07'], 1, 2, 3)" , Some ( "se8ojkCQvX" . to_string ( ) ) ) ,
237+ ( "SELECT sqids_encode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, array['XRKUdQVBzg'], 1, 2, 3)" , Some ( "WyXQfFQ21T" . to_string ( ) ) ) ,
238+ ] ) ;
239+
240+ for ( sql, expected) in tests {
241+ let result = Spi :: get_one ( sql) . expect ( "Query failed" ) ;
242+ assert_eq ! ( result, expected, "Failed on query: {}" , sql) ;
243+ }
244+ }
245+
246+ #[ pg_test]
247+ fn test_sqids_decode ( ) {
248+ let tests: HashMap < & str , Option < Vec < i64 > > > = HashMap :: from ( [
249+ ( "SELECT sqids_decode('86Rf07')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
250+ ( "SELECT sqids_decode(10::smallint, '86Rf07xd4z')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
251+ ( "SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 'XRKUdQ')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
252+ ( "SELECT sqids_decode(array['86Rf07'], 'se8ojk')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
253+ ( "SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, 'XRKUdQVBzg')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
254+ ( "SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', array['XRKUdQ'], 'WyXQfF')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
255+ ( "SELECT sqids_decode(10::smallint, array['86Rf07'], 'se8ojkCQvX')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
256+ ( "SELECT sqids_decode('k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt', 10::smallint, array['XRKUdQVBzg'], 'WyXQfFQ21T')" , Some ( vec ! [ 1 , 2 , 3 ] ) ) ,
257+ ] ) ;
258+
259+ for ( sql, expected) in tests {
260+ let result = Spi :: get_one ( sql) . expect ( "Query failed" ) ;
261+ assert_eq ! ( result, expected, "Failed on query: {}" , sql) ;
262+ }
263+ }
264+ }
265+
266+ #[ cfg( test) ]
267+ pub mod pg_test {
268+ pub fn setup ( _options : Vec < & str > ) { }
269+
270+ pub fn postgresql_conf_options ( ) -> Vec < & ' static str > {
271+ vec ! [ ]
272+ }
273+ }
0 commit comments